绑定到由后台工作的线程创建的对象的属性

发布于 2025-01-06 09:29:45 字数 1838 浏览 4 评论 0原文

我在多线程 WPF 应用程序中使用 MVVM 模式。

现在在我的模型中(我跳过了 INotifyPropertyChanged 接口的明显实现以使其更清晰):

public class ShallowModel : INotifyPropertyChanged
{
  private string _dbState;
  public string DbState
  {
     get { return _dbState; }
     set 
     { 
       _dbState = value;
       OnPropertyChanged("DbState") // ofc there is implementation of this
     }
  }
  private InsideObject _inObject;
  public InsideObject InObject
  {
     get { return _inObject; }
     set 
     { 
       _inObject = value;
       OnPropertyChanged("InObject") 
     }
  }
}   

public class InsideModel : INotifyPropertyChanged
{
  private string _actState;
  public string ActState
  {
     get { return _actState; }
     set 
     { 
       _actState= value;
       OnPropertyChanged("ActState")
     }
  }
}

假设我在视图上有 TextBlocks:

<TextBlock Text={Binding ActObjectState}/>
<TextBlock Text={Binding DbState}/>

现在有一部分有问题的 ViewModel:

public class ViewModel : INotifyPropertyChanged
{
  private ShallowModel _model;
  public string ActObjectState
  {
     get 
     { 
       if(_model.InObject != null)
         return _model.InObject.ActState; 
       else
         return null;
     }
  }
  public string DbState
  {
     get 
     { 
       return _model.DbState;
     }
  }
}

问题是 ActObjectState 和 DbState 在以下情况下没有更新:后台线程正在更新 ShallowModel 和/或 InsideModel 的属性。我的问题是:

  1. 我应该将 Model 公共属性添加到 ViewModel 并像 {Binding Path=Model.DbState} 那样绑定视图吗?我认为它破坏了 MVVM 理念 - View 不应该了解 Model。

  2. ShallowModel 中的 InObject 是在用户单击 UI 上的按钮后由新线程创建的。因此,当创建ViewModel时,InObject为null。不知何故 - 在线程创建之后 - ActObjectState 不会更新。如何让它发挥作用?进行像 {Binding Path=Model.InObject.ActState} 这样的绑定吗?这意味着UI设计师需要了解模型知识。

谢谢,对不起我的英语

I use MVVM pattern in my multithreading WPF app.

Now in the model I have (I skipped the obvious implementation of INotifyPropertyChanged interface to make it more clear):

public class ShallowModel : INotifyPropertyChanged
{
  private string _dbState;
  public string DbState
  {
     get { return _dbState; }
     set 
     { 
       _dbState = value;
       OnPropertyChanged("DbState") // ofc there is implementation of this
     }
  }
  private InsideObject _inObject;
  public InsideObject InObject
  {
     get { return _inObject; }
     set 
     { 
       _inObject = value;
       OnPropertyChanged("InObject") 
     }
  }
}   

public class InsideModel : INotifyPropertyChanged
{
  private string _actState;
  public string ActState
  {
     get { return _actState; }
     set 
     { 
       _actState= value;
       OnPropertyChanged("ActState")
     }
  }
}

Say I have TextBlocks on the View:

<TextBlock Text={Binding ActObjectState}/>
<TextBlock Text={Binding DbState}/>

Now there's a part of problematic ViewModel:

public class ViewModel : INotifyPropertyChanged
{
  private ShallowModel _model;
  public string ActObjectState
  {
     get 
     { 
       if(_model.InObject != null)
         return _model.InObject.ActState; 
       else
         return null;
     }
  }
  public string DbState
  {
     get 
     { 
       return _model.DbState;
     }
  }
}

The problem is that ActObjectState and DbState are not being updated when background thread is updating properties of ShallowModel and/or InsideModel. My questions are:

  1. Should I add Model public property to ViewModel and bind view like {Binding Path=Model.DbState}? I think it disrupts the MVVM idea - View shouldnt know about Model.

  2. InObject in ShallowModel is created by a new thread after User click button on UI. Therefore when ViewModel is created InObject is null. Somehow - after it's created by the thread - ActObjectState is not updated. How to make it work? Make binding like {Binding Path=Model.InObject.ActState}? This implies the need of having knowledge about Model by UI designer.

Thx, sry for my english

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独孤求败 2025-01-13 09:29:45

我会接受你的建议 1.这是最简单的方法,我没有看到任何违反 MVVM 模式的情况。

如果这不适合您 - 您必须将视图相关逻辑放入视图模型中。因此,视图模型可能有必要监听模型的更改并将这些更改重新抛出(使用与视图相关的逻辑)视图。

使用线程时请注意调度程序的内容,但我认为您知道这一点

i would take your suggestion 1. its the easiest way and i dont see any violation of the MVVM pattern.

if this not works for you - you have to put the view related logic in your viewmodel. so maybe its necessary that the viewmodel listen to the changes of the model and rethrow(with the view related logic) these changes to the view.

when working with threads be aware of dispatcher stuff, but i think you know that

舞袖。长 2025-01-13 09:29:45

如果您的线程创建了 ShallowModel,您需要引发与视图模型上的相关的所有属性。您可以引发 String.Empty 来告诉数据绑定引擎获取所有绑定属性。但是因为您位于一个线程中,所以您不能只触发 PropertyChangedEvent,您需要分派它,以便 UI 线程可以处理它。我们所做的就是使用

If your thread created the ShallowModel you need to raise all properties that are related to that on the viewmodel. You can raise a String.Empty to tell the databinding engine to fetch all bound properties. But because you are in a thread you cant just fire the PropertyChangedEvent you need to dispatch it, so the UI Thread can handle it. What we did, was to make our Raise Method ui thread safe with the dispatcher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文