使用OnPropertyChanged事件要求应用程序做某事是正确的吗?

您所在的位置:网站首页 warns怎么念 使用OnPropertyChanged事件要求应用程序做某事是正确的吗?

使用OnPropertyChanged事件要求应用程序做某事是正确的吗?

2023-04-12 12:39| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

My MVVM application contains two views:

AllStrategiesView StrategyView

When user click certain strategy in AllStrategiesView StrategyView with this strategy is created. I use such code to notify application that StrategyView should be created:

............. public void OpenStrategyView() { OnPropertyChanged("OpenStrategy"); } ................. private void OnWorkspacePropertyChanged(object sender, PropertyChangedEventArgs e) { const string openStrategyString = "OpenStrategy"; if (e.PropertyName == openStrategyString) { AllStrategiesViewModel vm = (sender as AllStrategiesViewModel); OpenStrategy(vm.SelectedStrategy); } }

However another part of the program shows error message because there are no such property "OpenStrategy":

/// /// Warns the developer if this object does not have /// a public property with the specified name. This /// method does not exist in a Release build. /// [Conditional("DEBUG")] [DebuggerStepThrough] public void VerifyPropertyName(string propertyName) { // Verify that the property name matches a real, // public, instance property on this object. if (TypeDescriptor.GetProperties(this)[propertyName] == null) { string msg = "Invalid property name: " + propertyName; if (this.ThrowOnInvalidPropertyName) throw new Exception(msg); else Debug.Fail(msg); }

The question is:

Is it right or wrong to use OnPropertyChanged to notify application that something need to be done? Should I rewrite my code to not to use OnPropertyChanged or should I disable VerifyPropertyName code?

推荐答案

This is bad practice. The PropertyChanged event on INotifyPropertyChanged should be used to notify subscribers that a property on the object instance has changed. This is typically used in WPF to notify the UI that it needs to update itself with the new property value.

In MVVM, you should use some kind of commanding or alternative viewmodel/view communication mechanism to invoke verbs (methods) on your view model from the view. The commanding provided by WPF has limitations, so I would recommend using an MVVM framework and the mechanisms that they provide.

其他推荐答案

Well it depends on what you want it to do. In your case it looks like you have a property "Workspace" which indicates which VM you should be looking at. This doesn't seem too bad of a usage IMHO.

If you were doing something completely unrelated to the property that was changed then it might work, but it's certainly not what I'd expect it to do (see Principle of Least Astonishment). OnPropertyChanged is intended to indicate that a property that has been bound to has changed and should be re-fetched.

You can of course just have another event on your ViewModel, like:

public event Action OpenStrategy;

One more thing... This code is completely redundant:

const string openStrategyString = "OpenStrategy"; if (e.PropertyName == openStrategyString)

the following is exactly the same, from the compiler's perspective, and much more readable:

if (e.PropertyName == "OpenStrategy") 其他推荐答案

There's nothing wrong in asking your application to do something in the PropertyChanged event, however do not raise a PropertyChanged event just to ask the application to do something.

PropertyChanged is used to indicate that a property has changed, and should be used for that only.

Devdigital's answer gives a good example, that the UI uses the PropertyChange notification to know when it should update. Other objects can also subscribe to receive change notifications, and they should only be notified when a value changes, not when you want to run some application code.

Using your example, I would rewrite it like this:

public void OpenStrategyView() { OpenStrategy(this.SelectedStrategy); } private void OnWorkspacePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "SelectedStrategy") { OpenStrategyView(); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3