【问题标题】:How to open a new View in WPF using MVVM architecture?如何使用 MVVM 架构在 WPF 中打开新视图?
【发布时间】:2020-03-29 20:53:26
【问题描述】:

我有一个项目,其中有各种库类。我有一个视图库类和一个模型视图库类。在 Views Library 类中,我有与 wpf 视图对应的 .xaml 文件。在 ModelViews 库类中,我在视图中使用的命令。 现在我想做的是在用户登录时调用一个新视图,但我不知道该怎么做。 我有这样的登录代码:

 public void Login()
        {
            try
            {
                USUARIO usuario = bl.EncontrarUsuarioPorUsername(Usuario);
                string savedPasswordHash = usuario.PASSWORD;
                /* Extract the bytes */
                byte[] hashBytes = Convert.FromBase64String(savedPasswordHash);
                /* Get the salt */
                byte[] salt = new byte[16];
                Array.Copy(hashBytes, 0, salt, 0, 16);
                /* Compute the hash on the password the user entered */
                var pbkdf2 = new Rfc2898DeriveBytes(Password, salt, 10000);
                byte[] hash = pbkdf2.GetBytes(20);
                /* Compare the results */
                for (int i = 0; i < 20; i++)
                    if (hashBytes[i + 16] != hash[i])
                    {
                        throw new UnauthorizedAccessException();
                    }
                MessageBox.Show("Login exitoso!");
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Contrasena Incorrecta");
            }
            catch(NullReferenceException)
            {
                MessageBox.Show("Nombre de usuario incorrecto");
            }


        }

现在,我想要做的是当日志记录成功时,关闭登录窗口并打开一个 ListUsersView.xaml,在 MessageBox 中显示消息“Login exitoso”。我尝试过各种事情,比如创建服务和助手,但我什么都做不了。我该如何解决这个问题?如何调用或引用 ModelView 类库中的 View?

【问题讨论】:

  • 你的看法是什么?这些是 Windows / UserControls 吗?

标签: c# wpf visual-studio mvvm


【解决方案1】:

如何调用或引用 ModelView 类库中的视图?

您不应该引用视图。这不仅会破坏 MVVM 模式,还会导致项目之间的循环依赖。

您应该做的是在ModelViews 项目中定义一个接口。你可以称它为IWindowService。然后在Views 项目中实现这个接口。

代码示例请参考我的回答here

【讨论】:

  • 你能给我看一些 IWindowService 类的代码示例吗?
【解决方案2】:

现在,我想要做的是当日志记录成功时,关闭登录窗口并打开一个 ListUsersView.xaml,在 MessageBox 中显示消息“Login exitoso”。我尝试过各种事情,比如创建服务和助手,但我什么都做不了。我该如何解决这个问题?如何调用或引用 ModelView 类库中的 View?

你可以试试下面的代码。

定义一个 isvalidated 属性以检查用户是否已授权。

    try
        {
            USUARIO usuario = bl.EncontrarUsuarioPorUsername(Usuario);
            string savedPasswordHash = usuario.PASSWORD;
            /* Extract the bytes */
            byte[] hashBytes = Convert.FromBase64String(savedPasswordHash);
            /* Get the salt */
            byte[] salt = new byte[16];
            Array.Copy(hashBytes, 0, salt, 0, 16);
            /* Compute the hash on the password the user entered */
            var pbkdf2 = new Rfc2898DeriveBytes(Password, salt, 10000);
            byte[] hash = pbkdf2.GetBytes(20);
            /* Compare the results */
            bool isvalidated = true;
            for (int i = 0; i < 20; i++)
            {
                if (hashBytes[i + 16] != hash[i])
                {
                    isvalidated = false;
                    break;

                }
            }
            if (isvalidated == false)
            {
                throw new UnauthorizedAccessException();
            }
            else
            {
                //MessageBox.Show("Login exitoso!");
                //shou your ListUsersView.xaml call it from your Views Library Class and set datacontext.

                 WpfCustomControlLibrary1.ListUsersView listviewsc = new WpfCustomControlLibrary1.ListUsersView();
                 listviewsc.Show();

                  //you can use the Application.Current.MainWindow method to find the MainWindow. Then, hide
                  BtnWindowsForm window = (BtnWindowsForm)Application.Current.MainWindow;
                  window.Close();//hide

            }
        }
        catch (UnauthorizedAccessException)
        {
            MessageBox.Show("Contrasena Incorrecta");
        }

此外,您还可以使用 mvvm light 或 prism 等 MVVM 框架,它们提供了对开发人员更友好的方式来使用 icommand。

【讨论】:

  • 但是我可以在 ViewModel LibraryClass 中引用我的 View LibraryClass。或者你指的是什么WpfCustomControlLibrary1
【解决方案3】:

你所要做的就是:

如果你想打开一个新的 WPF 窗口:

Window newWindow = new Window();
            newWindow.Show();

如果您想使用 UserControl 将新视图分配给 ViewModel(连接 View 和 ViewModel):

在 XAML 中定义:

<UserControl Content="{Binding CurrentView}"/>

在 ViewModel 中定义:

private UserControl currentView;
    public UserControl CurrentView
    {
        get { return currentView; }
        set { currentView = value; OnPropertyChanged(nameof(CurrentView)); }
    }

在构造函数中:

CurrentView= new ExampleViewModel();

【讨论】:

  • 这是违反 MVVM 的完美示例。
  • 你能详细解释一下你的答案吗?
  • MVVM 是关于分离视图和视图模型。如果视图模型知道视图,则不再有分离。更糟糕的是,如果视图模型保存在内存中并且视图模型保存视图 - 即使视图关闭,视图的内存也永远不会被回收。
猜你喜欢
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多