关于ViewModel和View交互

时间:2015-01-14 18:35:35

标签: c# wpf mvvm view viewmodel

我有一个关于它是如何工作的问题,目前我远离大学,我无法向教授询问这个问题,希望你们能帮助我。

所以,我有一个用xaml制作的登录面板 (由于安全原因,密码事物有一个类,http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html如果感兴趣的话) 我的问题是,每当我在我的2个文本框中写东西时,我希望它能够进入下一个Window / Xaml表单。我已经尝试在VkiewModel中创建表单的新实例(Form2 form = new Form2,然后使用form.show())但是根据教授所说的MVVM模式,ViewModel不应该创建Views。我该如何解决这个问题?

xmlns:vm="clr-namespace:SchoolManagement.ViewModels"
xmlns:ff="clr-namespace:SchoolManagement.Extras"
<Window.DataContext>
    <vm:LoginViewModel />
</Window.DataContext>


 <Label Content="Email" HorizontalAlignment="Left" Margin="338,125,0,0" VerticalAlignment="Top"/>
 <TextBox Text="{Binding Email}" HorizontalAlignment="Left" Height="25" Margin="338,155,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250" TextAlignment="Center" />

 <Label Content="Password" HorizontalAlignment="Left" Margin="338,185,0,0" VerticalAlignment="Top"/>
 <PasswordBox ff:PasswordBoxAssistant.BindPassword="true" ff:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MaxLength="25" HorizontalAlignment="Left" Margin="338,215,0,0" VerticalAlignment="Top" Width="250"/>

 <Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" Margin="513,243,0,0" VerticalAlignment="Top" Width="75" Command="{Binding LoginCommand}" Height="22"/>

1 个答案:

答案 0 :(得分:2)

如果您打算在没有外部框架的情况下执行此操作,则需要在viewmodel中创建视图可以订阅的事件。

<强> SomeViewModel.cs

public class GenericViewRequestedEventArgs : EventArgs
{
    public GenericViewModel ViewModel { get; private set; }

    public GenericViewRequestedEventArgs(GenericViewModel viewModel)
    {
        ViewModel = viewModel;
    }
}

public class SomeViewModel : ViewModelBase
{
    private RelayCommand _loginCommand;

    public ICommand LoginCommand
    {
        get
        {
            if (_loginCommand == null)
                _loginCommand = new RelayCommand(x => Login());

            return _loginCommand;
        }
    }

    public EventHandler<GenericViewRequestedEventArgs> GenericViewRequested;

    private void OnGenericViewRequested(GenericViewModel viewModel)
    {
        var handler = GenericViewRequested;
        if (handler != null)
            handler(this, new GenericViewRequestedEventArgs(viewModel));
    }

    private void Login()
    {
        // Do login stuff, authenticate etc.

        // Open new window.
        OnGenericViewRequested(_specificViewModel);
    }
}

<强> SomeWindow.xaml.cs

public partial class SomeWindow : Window
{
    private void OnGenericViewRequested(object sender, GenericViewRequestedEventArgs e)
    {
        GenericWindow window = new GenericWindow(e.ViewModel);
        window.Owner = this;
        window.ShowDialog();
    }

    public SomeWindow()
    {
        InitializeComponent();

        var viewModel = new SomeViewModel();
        viewModel.GenericViewRequested += OnGenericViewRequested;

        this.DataContext = viewModel;
    }
}