这些成员应该成为模特吗?如果是这样,怎么办?

时间:2018-09-28 11:39:38

标签: c# .net wpf prism prism-6

我是WPF和Prism的新手,因此,我现在正在尝试解决一些非常基本的问题。我的小实验在运行时如下所示:

enter image description here

我有一个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时,EmailPasswordPasswordConfirmation应该进入模型吗?如果要进行模型化,该如何布线?我找不到任何例子。

3 个答案:

答案 0 :(得分:0)

您的实现对我来说看起来都很不错,即您绑定到视图模型的属性。

有时候,人们倾向于将视图模型称为“模型”,但是在这种情况下,实际模型将由执行实际注册的服务来表示。

您可以使用该服务实现的接口注入视图模型,然后在Register()方法中通过该接口调用服务的方法,例如:

public class RegistrationViewModel : BindableBase
{
    ...
    private readonly IRegistrationService _registrationService;
    public RegistrationViewModel(IRegistrationService registrationService)
    {
        _registrationService = registrationService ?? throw new ArgumentNullException(nameof(registrationService));
        RegisterCommand = new DelegateCommand(Register);
    }

    private void Register()
    {
        _registrationService.Register(...);
    }
}

答案 1 :(得分:0)

由于使用的是MVVM,因此属性应在模型中。 然后,在ViewModel中创建该模型的属性,使其从INotifyPropertyChanged调用PropertyChanged-Event。

然后在视图中,从ViewModel绑定模型属性的元素名称。

然后,您应该决定是在使模型实现INotifyPropertyChanged还是其他方法之后。

答案 2 :(得分:-1)

您的ViewModel和XAML都很好。是的,您应该将电子邮件,密码等作为ViewModel的一部分公开。

您的示例中缺少的部分是ViewModel与UI的绑定。可以通过在您的XAML或您可能具有的代码隐藏页的构造函数中声明它来实现此目的

public Registration(RegistrationViewModel model)
{
    DataContext = model;
}

您要查询什么接线,因为这一切对我来说都很不错。