gpt4 book ai didi

mvvm-light - 通知 ViewModel 上的所有属性都已更改

转载 作者:行者123 更新时间:2023-12-04 15:15:13 29 4
gpt4 key购买 nike

我正在使用 MVVM Light Toolkit 的 V3 SP1 开发 Silverlight 应用程序。

我的申请完全是法语/英语。所有 UI 元素(按钮、标签等)和所有数据(模型)。我需要动态语言切换,这是完全实现的,可以处理来自资源文件的任何内容。我正在苦苦挣扎的是 ViewModels。

模型具有特定于语言的属性(DescriptionEn、DescriptionFr)和一个附加的属性调用 LocalizedDescription,它使用当前的文化来返回调用特定于语言的属性。

当语言更改(通过单击按钮)时,我会引发并广播(通过 Messenger)一个属性更改事件。

在我的每个 ViewModel 中,我注册以接收语言交换的属性更改消息。

我想通知 ViewModel 的所有属性发生了变化。

发件人:http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.



但是,由于工具包使用 RaisePropertyChanged(...) 抽象了更改事件的引发,因此我无法使其正常工作。我还检查了 takeit 的来源,发现 RaisePropertyChanged 调用了 VerifyPropertyName(..),它反过来返回一个错误,即该属性不属于 ViewModel。我还注意到,VerifyPropertyName 方法归因于 Conditional("DEBUG"),但即使我选择了 Release 配置,仍然会引发 ArgumentException("Property not found")。

除了为 ViewModel 的每个属性手动调用 RaisePropertyChanged 之外,有没有人知道使用工具包使其工作的方法?

跟进:

根据 Simon 的评论,我尝试创建自己的类来扩展 ViewModelBase。我查看了 CodePlex 上的源代码并决定创建一个名为 RaiseAllPropertyChanged() 的方法。它只是 RaisePropertyChanged(string propertyName) 的副本,但没有参数,也没有调用 VerifyPropertyName(...)。我无法让它工作。这是我所拥有的。
public class ViewModelBaseExtended : ViewModelBase
{
protected void RaiseAllPropertyChanged()
{
var handler = this.PropertyChanged;

if (handler != null)
{
handler(this, new PropertyChangedEventArgs(String.Empty));
}
}
}

但是我得到一个编译器错误: The event 'GalaSoft.MvvmLight.ViewModelBase.PropertyChanged' can only appear on the left hand side of += or -= .这是在 ViewModelBase 中使用的代码的副本。

有人可以就如何使其发挥作用提供一些建议吗?

解决方案:

我将 ViewModelBase 中的所有代码复制到一个新类中。然后我添加了方法 RaisePropertyChanged()上面提到的实例化了 PropertyChangedEventArgsString.Empty 一起上课.这现在是我的 ViewModel 的新子类。

再次感谢西蒙带路!

最佳答案

如果您在 2016 年阅读本文,您可以使用 ObservableObject 并通过执行以下操作通知所有属性已更改:

RaisePropertyChanged(string.Empty);

关于mvvm-light - 通知 ViewModel 上的所有属性都已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074178/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com