简体   繁体   English

MVVM绑定与公开属性

[英]MVVM Binding with exposed property

I have a View that is hooked to a ViewModel. 我有一个连接到ViewModel的View。 I have a singleton class that's exposed by foo.Current. 我有一个由foo.Current公开的单例类。 I have a property on that class that will be changing as the result of a button click command calling a foo.Current function. 我在该类上有一个属性,它将通过调用foo.Current函数的按钮单击命令进行更改。 I want to be able to bind a textbox's text to that value and when that value updates the textbox will update. 我希望能够将文本框的文本绑定到该值,当该值更新时,文本框将更新。 My ViewModel does implement INotifyPropertyChanged and I have other properties on the view model that update controls fine. 我的ViewModel确实实现了INotifyPropertyChanged,我在视图模型上有其他属性可以更新控件。 What is the best way I can make that singleton class update my ViewModel with the new value? 我可以使用单值类用新值更新ViewModel的最佳方法是什么? I'm thinking I need to figure out a way to make the Singleton class update the ViewModel. 我想我需要想办法让Singleton类更新ViewModel。

There's no "best" way in current situation, options are as follows: 目前的情况没有“最佳”方式,选项如下:

  1. Define a property in your viewmodel and pass that viewmodel to your singleton. 在viewmodel中定义一个属性,并将该viewmodel传递给您的单例。 Singleton should change the relevant property along with it's own. Singleton应该改变相关的属性和它自己的属性。
  2. Move properties from singleton to viewmodel or include singleton in the viewmodel itself 将属性从singleton移动到viewmodel或在viewmodel本身中包含singleton
  3. Create an event for changing the property on the singleton 创建一个事件来更改单例上的属性
  4. Inherit singleton from INotifyPropertyChanged and in your VM attach to PropertyChanged event of singleton. 从INotifyPropertyChanged继承单例,并在VM中附加到singleton的PropertyChanged事件。 And monitor your property name. 并监控您的财产名称。
  5. ... ...

Easiest one would be the first option though there might be some architectural inconsistencies in your app that should be treated. 尽管您的应用中可能存在一些应该被处理的架构不一致,但最简单的方法是第一个选项。

如果我理解你,我会使用Observer模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM