c# - 关于 ViewModel 和 View 交互

标签 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 不应创建 View 。我该如何解决这个问题?

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"/>

最佳答案

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

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;
    }
}

关于c# - 关于 ViewModel 和 View 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27950062/

相关文章:

c# - 如何有效地将一个可观察集合中的范围选择到另一个可观察集合中

c# - 跟踪Windows服务状态并在WPF UI中显示状态

c# - MVVM 基金会 : Assertion Failed Error: Invalid Property Name

c# - 如何清理 Entity Framework 对象上下文?

c# - NLog如何写消息源

c# - System.Reflection.Emit 的线程安全性如何?

c# - Xamarin.Forms 使用 MVVM 在屏幕之间传输数据

c# - 声明泛型类的变量

c# - 在 WPF 中将窗口置于最前面

c# - 在 ListView 中加载并显示所有项目后会触发哪个事件?