在WPF中使用MVVM在视图之间导航

在WPF中使用MVVM在视图之间导航,wpf,mvvm,mvvm-light,Wpf,Mvvm,Mvvm Light,我是WPF和MVVM的新手。 我正在尝试使用MVVM创建登录窗口,并成功创建。 这是Login.xmal代码 <Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" Margin="51,0,0,10" VerticalAlignment="Bottom" Width="124" Height="57" Grid.Column="1" CommandPara

我是WPF和MVVM的新手。 我正在尝试使用MVVM创建登录窗口,并成功创建。
这是Login.xmal代码

<Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" Margin="51,0,0,10" 
            VerticalAlignment="Bottom" Width="124" Height="57" Grid.Column="1" 
            CommandParameter="{Binding ElementName=txtPassword}" 
            Command="{Binding LoginCommand}"
            >           
    </Button>

    <Button x:Name="btnClose" Content="Close" HorizontalAlignment="Left" Margin="180,0,0,10" 
        VerticalAlignment="Bottom" Width="124" Height="57" Grid.Column="1"    Command="{Binding ExitCommand}">

    </Button>

    <Label Content="User Name" Margin="10,74,0,0" VerticalAlignment="Top" Height="49" 
           VerticalContentAlignment="Center" Grid.Column="1" HorizontalAlignment="Left" Width="130"/>

    <TextBox x:Name="txtUserName" HorizontalAlignment="Right" Height="49" Margin="0,74,10,0" 

             TextWrapping="Wrap"  VerticalAlignment="Top" Width="185" 
             VerticalContentAlignment="Center" Grid.Column="1" FontSize="18">
        <TextBox.Text>
            <Binding Path="Username" Mode="OneWayToSource">
                <Binding.ValidationRules>
                    <ExceptionValidationRule></ExceptionValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

    <Label Content="Password" Margin="10,128,0,0" VerticalAlignment="Top" Height="49" 
           VerticalContentAlignment="Center" Grid.Column="1" HorizontalAlignment="Left" Width="130"/>
    <PasswordBox x:Name="txtPassword" HorizontalAlignment="Right"
             Height="49" Margin="0,128,10,0"
          VerticalAlignment="Top" Width="185" 
        VerticalContentAlignment="Center" Grid.Column="1" FontSize="18">

    </PasswordBox>

在此之后,我创建了viewModeBase.cs类,在该类中我实现了INotifyPropertyChanged,它包含在LoginViewModel.cs中。。。 这是LoginViewModel.cs代码

public class LoginViewModel : ViewModelBase
{
    private string m_username;
    public string Username
    {
        get { return m_username; }
        set
        {
            m_username = value;
            OnPropertyChanged("Username");

        }
    }

    private string m_password;
    public string Password
    {
        get { return m_password; }
        set
        {
            m_password = value;
            OnPropertyChanged("Password");
        }
    }
    private DelegateCommand exitCommand;

    public ICommand ExitCommand
    {
        get
        {
            if (exitCommand == null)
            {
                exitCommand =new DelegateCommand(Exit);
            }
            return exitCommand;
        }
    }

    private void Exit()
    {
        Application.Current.Shutdown();
    }

    public LoginViewModel()
    {

    }

    private DelegateCommand<object> loginCommand;
    public ICommand LoginCommand
    {
        get
        {
            if (loginCommand == null)
            {
                loginCommand = new DelegateCommand<object>(Login);
            }
            return loginCommand;
        }
    }



    public void Login(object pPasswordBox)
    {
        try
        {
            if (string.IsNullOrEmpty(Username))
            {
                MessageBox.Show("Username cannot be blank.");                    
                return;
            }

            if (string.IsNullOrEmpty(((PasswordBox)pPasswordBox).Password))
            {
                MessageBox.Show("Password cannot be blank.");                
                return;
            }

            dlUsers odlUsers = new dlUsers();
            bool lResult = odlUsers.UserAuthentication(clsGymManagment.ConnectionString, Username, 
                ((((PasswordBox)pPasswordBox).Password)));
            if (lResult)
            {
                ///TODO: Need code to Hide Login Window and Open New XAML.....
            }
            else
            {
                MessageBox.Show("Username/Password is wrong.");                   
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }         
}
public类LoginViewModel:ViewModelBase
{
私有字符串m_用户名;
公共字符串用户名
{
获取{return m_username;}
设置
{
m_用户名=值;
OnPropertyChanged(“用户名”);
}
}
私有字符串m_密码;
公共字符串密码
{
获取{return m_password;}
设置
{
m_password=值;
OnPropertyChanged(“密码”);
}
}
私有DelegateCommand exitCommand;
公共ICOMAND ExitCommand
{
得到
{
if(exitCommand==null)
{
exitCommand=新的DelegateCommand(退出);
}
返回exitCommand;
}
}
私有无效退出()
{
Application.Current.Shutdown();
}
公共登录视图模型()
{
}
专用DelegateCommand登录命令;
公共ICommand登录命令
{
得到
{
if(loginCommand==null)
{
loginCommand=新的DelegateCommand(登录);
}
返回登录命令;
}
}
公共无效登录(对象pPasswordBox)
{
尝试
{
if(string.IsNullOrEmpty(用户名))
{
MessageBox.Show(“用户名不能为空。”);
返回;
}
if(string.IsNullOrEmpty(((PasswordBox)pPasswordBox.Password))
{
MessageBox.Show(“密码不能为空”);
返回;
}
dlUsers odlUsers=新的dlUsers();
bool lResult=odlUsers.UserAuthentication(clsgymmanagement.ConnectionString,Username,
(((密码箱)pPasswordBox.Password));
if(lResult)
{
///TODO:需要代码隐藏登录窗口并打开新的XAML。。。。。
}
其他的
{
MessageBox.Show(“用户名/密码错误”);
}
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}         
}
因为我想隐藏LOGIN.XAML文件并打开UI.XAML文件。。(UI.XAML,你可以考虑任何XAML窗口。)
另外,如果您能帮助我在UI.XAML上的Usercontrol之间导航,这将是完全有帮助的。您需要从单独的代码块控制登录窗口,例如App.XAML.cs。将app.xaml设置为调用代码而不是显示窗口

让应用程序启动创建LoginViewModel,新建表单,将表单的数据上下文设置为ViewModel并显示它

对表单的更新将更新ViewModel,当它关闭时,它将把控制权返回到调用代码

Login.xaml.cs

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {
        if (anything incorrect)
        {
            MessageBox.Show("Enter a username and password");
        }
        else
            DialogResult = true;
    }
App.xaml.cs

Login.DataContext = LoginViewModel;

if (Login.ShowDialog() ?? false)
{
   //Check the LoginViewModel for a correct password. 
}

幸运的是,在应用程序中通过不同页面时隐藏和显示不同控件的功能已经为您编写好了。看


导航窗口真的很强大,可以很容易地剥皮,提供非常完全不同的外观。请参见

我喜欢您刚才解释的想法,它帮助我编写代码……但需要确认的是,这是处理MVVM模式的好方法?是的,您看到的唯一一件事是一个小代码,它正在从对话框返回正确的值。我不知道还有其他方法可以设置该返回值。