使用MVVM登录WPF [英] Login on WPF using MVVM

查看:111
本文介绍了使用MVVM登录WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiya,

我对WPF还是很陌生,之前已经在Win Forms中完成了一些开发工作,并且想知道如何使用WPF和MVVC来完成登录过程?

我要做的是出现一个登录对话框,如果登录过程成功,则应该将用户重定向到主应用程序.如果不是,则应该可以再次输入详细信息或关闭应用程序.

非常感谢您提供任何帮助!

Hiya,

I am very new to WPF having done some development work in Win Forms previously, and was wondering, how am I able to do a login procedure using WPF with MVVC?

What I want to do is have a login dialogue appear and if the login procedure is successful, then the user should be redirected to the main application. If they are not, they should simply be able to either enter their details again, or close the application.

Any help is very much appreciated!

推荐答案

尝试具有MVVM模式的简单WPF登录演示 [ ^ ]
这是该搜索的第一个链接,它与源一起.
Try Google[^] first.
Got About 216,000 results in 0.53 seconds!!

Simple WPF Login demo, with MVVM pattern[^]
This is the first link from that search, it is with source.


这是我的登录xaml文件(查看):

This is my login xaml file (View):

<page x:class="WolfswoodInCinch.Login" xmlns:x="#unknown">
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:CinchV2="clr-namespace:Cinch;assembly=Cinch.WPF"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
	 xmlns:views="clr-namespace:WolfswoodInCinch.Views"
        Title="Wolfswood Coach Booking System" Height="350" Width="525"
        xmlns:meffed="http:\\www.codeplex.com\MEFedMVVM"
        meffed:ViewModelLocator.ViewModel="LoginViewModel">

    <grid height="193" width="236">
        <label content="Wolfswood Coach Booking Application" height="28" horizontalalignment="Left" margin="10,10,0,0" name="label1" verticalalignment="Top" />
        <label content="Login" height="28" horizontalalignment="Left" margin="10,31,0,0" name="label2" verticalalignment="Top" />
        <textblock text="{Binding Path=Error, Mode=TwoWay}" foreground="Red"></textblock>
        <label content="Username: " height="28" horizontalalignment="Left" margin="10,78,0,0" name="label3" verticalalignment="Top" />
        <label content="Password: " height="28" horizontalalignment="Left" margin="10,112,0,0" name="label4" verticalalignment="Top" />

        <textbox height="23" horizontalalignment="Left" margin="82,83,0,0" name="textBox1" verticalalignment="Top" width="120" text="{Binding UserName.DataValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}">
            <i:interaction.behaviors xmlns:i="#unknown">
                <cinchv2:textboxfocusbehavior isusingdatawrappers="True" xmlns:cinchv2="#unknown" />
                <cinchv2:textboxfocusbehavior xmlns:cinchv2="#unknown" />
            </i:interaction.behaviors>
        </textbox>

        <textbox height="23" horizontalalignment="Left" margin="82,117,0,0" name="passwordBox1" verticalalignment="Top" width="120" text="{Binding Password.DataValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}">
            <i:interaction.behaviors xmlns:i="#unknown">
                <cinchv2:textboxfocusbehavior isusingdatawrappers="True" xmlns:cinchv2="#unknown" />
                <cinchv2:textboxfocusbehavior xmlns:cinchv2="#unknown" />
            </i:interaction.behaviors>
        </textbox>

        <button content="Login" isdefault="True" height="23" horizontalalignment="Left" margin="149,158,0,0" name="button1" command="{Binding LoginViewFiredCommand}" verticalalignment="Top" width="75" click="button1_Click">
        </button>
        <textblock text="{Binding Path=loginResult}" horizontalalignment="Left" margin="12,146,0,12" name="textBlock1" width="131" />
    </grid>
</page>



这是我的MV课:



And this is my MV class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.ComponentModel;
using WolfswoodInCinch.Views;
using WolfswoodInCinch.Classes;
using WolfswoodInCinch.Commands;
using MEFedMVVM.ViewModelLocator;
using Cinch;
using System.ComponentModel.Composition;
using System.Windows.Navigation;


namespace WolfswoodInCinch.ViewModels
{
    [ExportViewModel("LoginViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class LoginViewModel : ViewModelBase
    {
        private DataWrapper<string> userName;
        private DataWrapper<string> password;
        private IEnumerable<datawrapperbase> cachedListOfDataWrappers;
        //public IUIVisualizerService uiVisualizer;

        public LoginWindow Parent { get; set; }

        [ImportingConstructor]
        public LoginViewModel(/*IUIVisualizerService uiVisualizer*/)
        {
            UserName = new DataWrapper<string>(this, userNameChangeArgs);
            UserName.IsEditable = true;

            Password = new DataWrapper<string>(this, passwordChangeArgs);
            Password.IsEditable = true;

            cachedListOfDataWrappers = DataWrapperHelper.GetWrapperProperties<loginviewmodel>(this);


            //this.uiVisualizer = uiVisualizer;
            LoginViewFiredCommand = new SimpleCommand<object,>(ExecuteLoginCommand);
        }
  
        /// <summary>
        /// Username
        /// </summary>
        /// 
        static PropertyChangedEventArgs userNameChangeArgs = ObservableHelper.CreateArgs<loginviewmodel>(y => y.UserName);

        public DataWrapper<string> UserName
        {
            get { return userName; }
            private set
            {
                userName = value;
                NotifyPropertyChanged(userNameChangeArgs);
            }
        }

        /// <summary>
        /// Password
        /// </summary>
        static PropertyChangedEventArgs passwordChangeArgs = ObservableHelper.CreateArgs<loginviewmodel>(x => x.Password);

        public DataWrapper<string> Password
        {
            get { return password; }
            private set
            {
                password = value;
                NotifyPropertyChanged(passwordChangeArgs);
            }
        }

        public SimpleCommand<object,> LoginViewFiredCommand { get; private set; }

        private void ExecuteLoginCommand(EventToCommandArgs args)
        {
            
            string username = UserName.DataValue;
            string password = Password.DataValue;

            loginResult = "Successful login";

            //((LoginWindow)args.Sender).DialogResult = true;

            
            //Home home = new Home();
            //Welcome welcome = new Welcome();
            //welcome.Visibility.Equals(true);
            //Welcome main = new Welcome();
            //this.
            //this.Close();
            //WorkspaceData workspace = new WorkspaceData(
            //Views.Add(home);
            //NavigationService.GetNavigationService(this).Navigate(home);
        }

        string result;

        public string loginResult
        {
            get { return result; }
            set
            {
                result = value;
                NotifyPropertyChanged(_loginResultChangeArgs);
            }
        }

        static PropertyChangedEventArgs _loginResultChangeArgs = ObservableHelper.CreateArgs<loginviewmodel>(x => x.loginResult);

    }
}

</loginviewmodel></string></loginviewmodel></string></loginviewmodel></loginviewmodel></string></string></datawrapperbase></string></string>



我想要做的是在ViewModel的ExecuteLoginCommand方法中,如果用户已成功登录,则关闭窗口,然后查看主应用程序.我赞赏目前没有任何数据验证,这将在基本原理实施后进行.



What I want to be able to do is in the ViewModel''s ExecuteLoginCommand method, close the window if the user has successfully logged in, and then view the main application. I appreciate at present there isn''t any data validation, this will come once the fundamentals have been implemented.


这篇关于使用MVVM登录WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆