【发布时间】:2018-07-16 13:01:15
【问题描述】:
我无法真正解决以下问题:
我在应用程序中只有一个textbox 用于用户输入,一个button 用于对该输入执行后台计算,以及一个textblock。想象一下,我必须使用 MVVM,所以我有 view、viewmodel 和 model 类。
我将视图中的控件(textbox、button 和textblock)绑定到相应属性和命令上的viewmodel。但是,我不确定viewmodel 功能应该在哪里结束。例如,以下是构建应用程序的一种方式吗?
型号:
public class Model
{
public string Input { get; set; }
public string Output { get; set; }
public void FancyMethod ()
{
// Use input to calculate output
}
}
视图模型:
public class ViewModel
{
public string Input {get; set;}
public string Output {get; set;}
public ICommand command {get; set;}
public Model model {get; set;}
public ViewModel()
{
model = new Model();
}
// When the button is pressed, model.input = Input and then execute model.FancyMethod()
}
【问题讨论】:
-
也许这篇文章会对你有所帮助:stackoverflow.com/questions/23648832/…
-
Model 是您的数据库表的表示形式,因此您的
FancyMethod需要在您的模型类之外。将其移至您的 ViewModel 类,并在您单击按钮或您想要的任何操作/事件时调用它。 -
FancyMethod 真的会产生值吗?
-
它从输入计算值,然后将计算值分配给输出