如何使用MVVM体系结构在WPF中打开新视图?

时间:2019-12-05 00:16:41

标签: c# wpf visual-studio mvvm

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

 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?

3 个答案:

答案 0 :(得分:1)

  

如何在ModelView类库中调用或引用View?

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

您应该做的是在ModelViews项目中定义一个接口。您可以将其命名为IWindowService。然后,您在Views项目中实现此接口。

有关代码示例,请参阅我的答案here

答案 1 :(得分:0)

  

现在,我要执行的操作是在日志记录成功后关闭登录窗口并打开ListUsersView.xaml,以在MessageBox中显示消息“ Login exitoso”。我曾尝试过各种服务,例如创建服务和帮助程序,但是我无法做任何事情。我该如何解决?如何在ModelView类库中调用或引用View?

您可以尝试下面的代码。

定义已验证属性以检查用户是否已授权。

    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框架(例如mvvm light或棱镜),这些框架为开发人员提供了更多使用icommand的友好方式。

答案 2 :(得分:-1)

您需要做的就是这个:

如果要打开新的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();