通过数据绑定调用

通过数据绑定调用

本文介绍了SQL 2 LINQ查询(通过数据绑定调用)完全冻结WPF应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候,我被追捕一个非常奇怪的错误......我终于追查
下降到什么似乎会造成问题。

Earlier today I was hunting down a very weird bug... I finally traced itdown to what seems to be causing the problem.

原报告可以在这里找到:原题

The original report can be found here: original question

的细节已经改变足以保证一个新的问题。

The details have changed enough to warrant a new question.

这似乎是我的应用程序有时,NOT所有的时间,冻结
,当它到达以下LINQ查询:

It would seem my application sometimes, NOT ALL OF THE TIME, freezeswhen it reaches the following LINQ query:

using (NetworkDatabaseContext db = new

NetworkDatabaseContext(UISession.ConnectionString))
{
   Ballast ballast = db.Ballasts.FirstOrDefault(b => b.NetworkId == UISession.NetworkId &&
          b.ShortAddress == this.innerBallast.ShortAddress && b.ControllerSerial == this.controllerSerial);

这是什么样子:

在大多数情况下这工作得很好......但每一个现在,然后它会锁定。
此代码是一个BallastListItem类的一部分。此类项目被绑定到一个列表框的网页上:

Most of the time this works just fine... but every now and then it will lock up.This code is part of a BallastListItem class. Items of this class are bound to a ListBox on the Page:

<ListView Name="lstBallasts" Margin="5" DockPanel.Dock="Top" MinHeight="100"><!-- The MinHeight is used to get a good view in the designer -->
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Address" DisplayMemberBinding="{Binding InnerBallast.ShortAddress}" Width="70"/>
                <GridViewColumn Header="Name" Width="300">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="txtBallastDisplayName" Text="{Binding DisplayName}" Width="270" MaxWidth="270" MaxLength="100"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Type" DisplayMemberBinding="{Binding DeviceType}" Width="150"/>
                <GridViewColumn Header="Status" DisplayMemberBinding="{Binding InnerBallast.StandardVersion}" Width="150"/>
            </GridView>
        </ListView.View>
    </ListView>



中的代码是DisplayName属性的getter的一部分:

The code is part of the DisplayName property getter:

public string DisplayName
    {
        get
        {
            using (NetworkDatabaseContext db = new NetworkDatabaseContext(UISession.ConnectionString))
            {
                Ballast ballast = db.Ballasts.FirstOrDefault(b => b.NetworkId == UISession.NetworkId &&
                   b.ShortAddress == this.innerBallast.ShortAddress && b.ControllerSerial == this.controllerSerial);



所以,当数据绑定发生此代码被调用时,应该在此之前实现。
我还是不知道为什么这有时会导致问题...

So this code is called when databinding occurs, should have realized that before.Still I have no idea why this would sometimes cause problems...

更新

在这两种情况下(当应用程序冻结,当它不)
连接的状态是封闭只是查询代码之前,因为我可以看到:

In both cases (when the application freezes and when it does not)the state of the connection is "Closed" just before the query code, as I can see from:

db.Connection.State.ToString()

更新2
我忘了提我感动的是码出吸气,
,因此它只能提取一次。必须同时实施
这个第一次在睡觉。现在,该代码工作正常,但在XP ...每一个现在
,然后,但往往更无问题仍然存在。

UPDATE 2I forgot to mention I moved that code out of the getter,so it is only fetched once. Must have been sleeping while implementingthis the first time. The code works fine now, although on XP... every nowand then, but much less often the problem still occurs.

*更新3 *
只是要清楚,我使用的是SQL Compact数据库用很少的数据。

* UPDATE 3 *Just to be clear, I'm using an SQL Compact database with very little data

推荐答案

有一个属性打开一个数据库连接,并运行一个查询是不是一个好模式。

Having a property open a database connection and run a query is not a good pattern.

有一个更好的办法是从LINQ查询一组对象到SQL并绑定那些WPF控件来代替。

A better approach would be to query a set of objects from LINQ to SQL and bind those to the WPF control instead.

这篇关于SQL 2 LINQ查询(通过数据绑定调用)完全冻结WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:57