MVVM Prism WPF - 设置 public 属性 和 SetProperty
MVVM Prism WPF - Setting a public property and SetProperty
我是 MVVM 和 Prism 的新手。我有以下 ViewModel
using System.Collections.Generic;
using BasicMVVMQuickstart_Desktop.Model;
using Microsoft.Practices.Prism.Mvvm;
namespace BasicMVVMQuickstart_Desktop.ViewModels
{
public class QuestionnaireViewModel : BindableBase
{
private Questionnaire questionnaire;
public QuestionnaireViewModel()
{
this.Questionnaire = new Questionnaire();
this.AllColors = new[] { "Red", "Blue", "Green" };
}
public Questionnaire Questionnaire
{
get { return this.questionnaire; }
set {
SetProperty(ref this.questionnaire, value);
}
}
public IEnumerable<string> AllColors { get; private set; }
}
}
我正在尝试从另一个视图模型设置调查问卷并使用另一个视图模型的设置值更新 WPF UI。但不幸的是它不起作用。下面是另一个视图模型的代码。我在“private void OnSubmit(object obj)”中设置了 属性,但是当我 运行 申请并按下提交时,UI 没有得到更新。现在有人还需要为这项工作添加什么吗?如果我在 "public MainWindowViewModel()" 中设置 "this.QuestionnaireViewModel.Questionnaire.Age = 32;" ,那么 UI 会在 运行 时间更新,但是 OnSubmit 不起作用。请帮忙。
using System.Diagnostics;
using System.Text;
using System.Windows.Input;
using BasicMVVMQuickstart_Desktop.Model;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm;
namespace BasicMVVMQuickstart_Desktop.ViewModels
{
public class MainWindowViewModel : BindableBase
{
public MainWindowViewModel()
{
this.SubmitCommand = new DelegateCommand<object>(this.OnSubmit);
this.QuestionnaireViewModel = new QuestionnaireViewModel();
this.ResetCommand = new DelegateCommand(this.OnReset);
}
public ICommand SubmitCommand { get; private set; }
public ICommand ResetCommand { get; private set; }
public QuestionnaireViewModel QuestionnaireViewModel { get; set; }
private void OnSubmit(object obj)
{
Debug.WriteLine(this.BuildResultString());
this.QuestionnaireViewModel.Questionnaire.Age = 32;
}
private void OnReset()
{
this.QuestionnaireViewModel.Questionnaire = new Questionnaire();
}
private string BuildResultString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Name: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Name);
builder.Append("\nAge: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Age);
builder.Append("\nQuestion 1: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Quest);
builder.Append("\nQuestion 2: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.FavoriteColor);
return builder.ToString();
}
}
}
当您在代码中更改年龄 属性 时:
this.QuestionnaireViewModel.Questionnaire.Age = 32;
您更改 属性 问卷,但未更改 属性 QuestionnaireViewModel。因此,您需要从 BindableBase 继承问卷 class 并为 Age 属性:
创建类似的代码
private int _age;
public int Age
{
get { return _age; }
set { SetProperty(ref _age, value); }
}
我是 MVVM 和 Prism 的新手。我有以下 ViewModel
using System.Collections.Generic;
using BasicMVVMQuickstart_Desktop.Model;
using Microsoft.Practices.Prism.Mvvm;
namespace BasicMVVMQuickstart_Desktop.ViewModels
{
public class QuestionnaireViewModel : BindableBase
{
private Questionnaire questionnaire;
public QuestionnaireViewModel()
{
this.Questionnaire = new Questionnaire();
this.AllColors = new[] { "Red", "Blue", "Green" };
}
public Questionnaire Questionnaire
{
get { return this.questionnaire; }
set {
SetProperty(ref this.questionnaire, value);
}
}
public IEnumerable<string> AllColors { get; private set; }
}
}
我正在尝试从另一个视图模型设置调查问卷并使用另一个视图模型的设置值更新 WPF UI。但不幸的是它不起作用。下面是另一个视图模型的代码。我在“private void OnSubmit(object obj)”中设置了 属性,但是当我 运行 申请并按下提交时,UI 没有得到更新。现在有人还需要为这项工作添加什么吗?如果我在 "public MainWindowViewModel()" 中设置 "this.QuestionnaireViewModel.Questionnaire.Age = 32;" ,那么 UI 会在 运行 时间更新,但是 OnSubmit 不起作用。请帮忙。
using System.Diagnostics;
using System.Text;
using System.Windows.Input;
using BasicMVVMQuickstart_Desktop.Model;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm;
namespace BasicMVVMQuickstart_Desktop.ViewModels
{
public class MainWindowViewModel : BindableBase
{
public MainWindowViewModel()
{
this.SubmitCommand = new DelegateCommand<object>(this.OnSubmit);
this.QuestionnaireViewModel = new QuestionnaireViewModel();
this.ResetCommand = new DelegateCommand(this.OnReset);
}
public ICommand SubmitCommand { get; private set; }
public ICommand ResetCommand { get; private set; }
public QuestionnaireViewModel QuestionnaireViewModel { get; set; }
private void OnSubmit(object obj)
{
Debug.WriteLine(this.BuildResultString());
this.QuestionnaireViewModel.Questionnaire.Age = 32;
}
private void OnReset()
{
this.QuestionnaireViewModel.Questionnaire = new Questionnaire();
}
private string BuildResultString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Name: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Name);
builder.Append("\nAge: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Age);
builder.Append("\nQuestion 1: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Quest);
builder.Append("\nQuestion 2: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.FavoriteColor);
return builder.ToString();
}
}
}
当您在代码中更改年龄 属性 时:
this.QuestionnaireViewModel.Questionnaire.Age = 32;
您更改 属性 问卷,但未更改 属性 QuestionnaireViewModel。因此,您需要从 BindableBase 继承问卷 class 并为 Age 属性:
创建类似的代码private int _age;
public int Age
{
get { return _age; }
set { SetProperty(ref _age, value); }
}