c# - 这些成员应该在模型中吗?如果是这样,如何?
问题描述
我是 WPF 和 Prism 的新手,所以,我目前正试图弄清楚非常基本的事情。我的小实验在运行时是这样的:
我有一个Views\Registration.xaml
看起来像这样的:
<UserControl x:Class="Configurator.Views.Registration"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="Email:" HorizontalContentAlignment="Right" Margin="6"/>
<TextBox Grid.Column="1" Grid.Row="0" x:Name="email" Margin="6" Text="{Binding Email}"/>
<Label Grid.Column="0" Grid.Row="1" Content="Password:" HorizontalContentAlignment="Right" Margin="6"/>
<PasswordBox Grid.Column="1" Grid.Row="1" x:Name="password" Margin="6" />
<Label Grid.Column="0" Grid.Row="2" Content="Password Confirmation:" HorizontalContentAlignment="Right" Margin="6"/>
<PasswordBox Grid.Column="1" Grid.Row="2" x:Name="passwordConfirmation" Margin="6"/>
<Button Grid.Column="1" Grid.Row="3" Content="Register" HorizontalAlignment="Left" Margin="6" Padding="6,2" Command="{Binding RegisterCommand}"/>
</Grid>
</UserControl>
然后一个ViewModels\RegistrationViewModel.cs
看起来像这样:
namespace Configurator.ViewModels {
public class RegistrationViewModel : BindableBase {
private string _email;
public string Email {
get { return _email; }
set { SetProperty(ref _email, value); }
}
private string _password;
public string Password {
get { return _password; }
set { SetProperty(ref _password, value); }
}
private string _passwordConfirmation;
public string PasswordConfirmation {
get { return _passwordConfirmation; }
set { SetProperty(ref _passwordConfirmation, value); }
}
public DelegateCommand RegisterCommand { get; private set; }
public RegistrationViewModel() {
Console.WriteLine("RegistrationViewModel");
RegisterCommand = new DelegateCommand(Register);
}
private void Register() {
Console.WriteLine("Registering");
Console.WriteLine($"Email: {Email}");
Console.WriteLine($"Password: {Password}");
Console.WriteLine($"Password Confirmation: {PasswordConfirmation}");
}
}
}
跟随 MVVM 时应该Email
,Password
和PasswordConfirmation
进入模型吗?如果它应该进入模型,布线是如何完成的?我找不到任何例子。
解决方案
由于您使用的是 MVVM,因此属性应该在模型中。然后,您在 ViewModel 中创建该模型的属性,使其从 INotifyPropertyChanged 调用 PropertyChanged-Event。
然后在视图中绑定来自 ViewModel 的模型属性的元素名称。
然后,您应该决定是让您的模型实现 INotifyPropertyChanged 还是找到另一种方法。
推荐阅读
- javascript - 如何使用 TypeScript 接口检查这三个值的类型?
- ios - 静态 UIView 在本地化为法语时更改宽度
- asp.net-core-mvc - 在 Orchard 中更改管理仪表板
- excel - 检查列中是否存在值,如果没有,则在最后一行之后添加新行
- git - 如何在 GitHub 上使用 master 更新分支
- kubernetes - 如何更改 Kubernetes pod 命名空间
- python - 检测附加到图像上其他东西的小方块
- apache-superset - 在 Apache Superset 中启用电子邮件
- c++ - EOF 有更安全的替代方案吗?在我的情况下它不起作用
- javascript - 无法将 Pig Latin JavaScript 函数转换为 GUI