简体   繁体   English

MVVM Light,使用 RelayCommand<t> with 方法返回数据</t>

[英]MVVM Light, use RelayCommand<T> with Methods returning data

I'm experimenting with MVVM Light for an application which acquires data from sensors.我正在试验一个从传感器获取数据的应用程序的 MVVM Light。 The sensor model exposes properties and methods.传感器 model 公开了属性和方法。 Some of these methods take arguments and return data.其中一些方法采用 arguments 并返回数据。 Example,例子,

public double GetVelocity(int AxisNo) 
{
     Do something;
     return double_data;
}

How do I use RelayCommand<T> to pass a parameter from a bound control and bind the return value to another control displaying it?如何使用RelayCommand<T>从绑定控件传递参数并将返回值绑定到显示它的另一个控件?

Thanks.谢谢。

You can use CommandParameter to pass a parameter to a command.您可以使用CommandParameter将参数传递给命令。

You can't return a value, but you can set a property that raises NotifyPropertyChanged , and bind something to that property.您不能返回值,但可以设置引发NotifyPropertyChanged的属性,并将某些内容绑定到该属性。

Returning a value would imply that the view has some intelligence.返回一个值意味着该视图具有一些智能。 You're better to think of the view as just displaying the state of the ViewModel, which translates the properties of the Model into values that are suitable for display.您最好将视图视为仅显示 ViewModel 的 state,它将 Model 的属性转换为适合显示的值。

Similarly, commands should live in the ViewModel, as they aren't intrinsic to the model.同样,命令应该存在于 ViewModel 中,因为它们不是 model 固有的。

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

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