WPF MVVM模式登录

实现的功能是客户打开登录界面,输入账户密码,加载revit插件并打开revit

创建Command建

public class LoginCommand : ICommand
    {
        readonly Func<Boolean> _canExecute;
        readonly Action _execute;
        public LoginCommand(Action execute) : this(execute, null) { }
        public LoginCommand(Action execute, Func<Boolean> canExecute)
        {
            if (execute == null)
               throw new ArgumentNullException("execute");
               _execute = execute;
              _canExecute = canExecute;
  
        }
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                     CommandManager.RequerySuggested += value;

            }

            remove
            {
                if (_canExecute != null)
                     CommandManager.RequerySuggested -= value;

            }
        }
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(object parameter)
        {
            _execute();
        }
    }

创建ViewModel

public class LoginViewModel: INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        public LoginViewModel()
        {
           
        }
        private string username;
        public string UserName 
        {
            get { return username; }
            set { username = value;RaisePropertyChanged("UserName"); }
        }
        private string userpass;
        public string UserPass
        {
            get { return userpass; }
            set { userpass = value; RaisePropertyChanged("UserPass"); }
        }
        private string tipaccount;

        public string Tipaccount
        {
            get { return tipaccount; }
            set { tipaccount = value; RaisePropertyChanged("Tipaccount"); }
        }

        void UpdateNameExecute()
        {
            if (this.UserName =="Admin" && this.UserPass=="123456")
            {
                MessageBox.Show("登录成功");
                string Name = GetIntallPathInfo("2019");
                Process pro = System.Diagnostics.Process.Start(Name);
                //  SetCache.SetCacheing();
                //Add();
            }
            //this.UserName = "黄昏前黎明后";
            //this.UserPass = "中软易通科技";
        }
        public static void Add()
        {
            try
            {
                Console.WriteLine("正在查找已经注册的程序.....");
                RegistryKey key = Registry.ClassesRoot;
                key.DeleteSubKeyTree(@"gitwms");
                Console.WriteLine("已经清除注册程序.....");
            }
            catch (Exception e)
            {
                Console.WriteLine("未找到注册的程序...");
            }

            /*===============================================*/
            Console.WriteLine("开始注册程序....");
            RegistryKey regWrite = Registry.ClassesRoot.CreateSubKey("gitwms");
            regWrite.SetValue("", "URL:自定义协议");
            regWrite.SetValue("URL Protocol", "URL Protocol");
            regWrite.Close();


            regWrite = Registry.ClassesRoot.CreateSubKey(@"gitwms\shell");
            regWrite.Close();

            regWrite = Registry.ClassesRoot.CreateSubKey(@"gitwms\shell\open");
            regWrite.Close();

            regWrite = Registry.ClassesRoot.OpenSubKey(@"gitwms\shell\open", true);
            RegistryKey aimdir = regWrite.CreateSubKey("command");

            string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string AppPath = Path.Combine(BaseDirectory, "D:\\Revit2019_SetUp\\Revit 2019\\Revit.exe");

            aimdir.SetValue(@"", "\"" + AppPath + "\" \" %1\"");

            regWrite.Close();
            aimdir.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Version"></param>
        /// <returns></returns>
        public static string GetIntallPathInfo(string Version)
        {
            try
            {
                string ExeName = "Revit.exe";
                string SubKeyName = string.Format(@"SOFTWARE\Autodesk\Revit\{0}\REVIT-05:0804", Version);
                //RegistryKey regKey =  Registry.LocalMachine;
                //RegistryKey regSubKey = regKey.OpenSubKey("SOFTWARE\\Autodesk\\Revit\\", false);
                //Registry.LocalMachine
                using (RegistryKey RegistryKey1 = Registry.LocalMachine.OpenSubKey(SubKeyName, false))
                {
                    if (RegistryKey1 == null)
                    {
                        return string.Empty;
                    }
                    if (RegistryKey1.GetSubKeyNames() == null)
                    {
                        return string.Empty;
                    }

                    //RegistryKey RegistryKey2 = RegistryKey1.OpenSubKey(SoftName, false);
                    //RegistryKey RegistryKey2 = RegistryKey1.OpenSubKey(SubKeyName, false);
                    //if (RegistryKey2 == null)
                    //{
                    //    return string.Empty;
                    //}

                    //获取软件名
                    string SoftwareName = RegistryKey1.GetValue("ProductName", "").ToString();
                    //安装地址信息
                    string InstallLocation = RegistryKey1.GetValue("InstallationLocation", "").ToString();

                    return System.IO.Path.Combine(InstallLocation, ExeName);
                }
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        bool CanUpdateNameExecute()
        {

            return true;

        }
        void UpdatatipaccountExecute()
        {
            if (this.UserName == null)
            {
                this.Tipaccount = "请输入邮箱或者手机号";
            }
            else
            {
               this.Tipaccount = "";
            }
            
        }
        bool CanUpdatatipaccountExecute()
        {

            return true;

        }
        public ICommand UpdateName
        {
            get { return new LoginCommand(UpdateNameExecute, CanUpdateNameExecute); }
        }
        public ICommand Updatatipaccount
        {
            get { return new LoginCommand(UpdatatipaccountExecute, CanUpdatatipaccountExecute); }
        }
    }

源码下载地址链接:https://pan.baidu.com/s/1_XgkLvixIvDZYURJP_TmEQ 
提取码:m8sk 
复制这段内容后打开百度网盘手机App,操作更方便哦--来自百度网盘超级会员V1的分享

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的WPF MVVM模式登录界面及代码: XAML代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" Title="Login" Height="250" Width="350"> <Window.DataContext> <local:LoginViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="Username:"/> <TextBox Grid.Row="0" Grid.Column="1" Width="200" Margin="5" Text="{Binding Username}" /> <Label Grid.Row="1" Grid.Column="0" Content="Password:"/> <PasswordBox Grid.Row="1" Grid.Column="1" Width="200" Margin="5" Password="{Binding Password}" /> <Button Grid.Row="2" Grid.Column="1" Width="100" Margin="5" Content="Login" Command="{Binding LoginCommand}" IsDefault="True"/> <Label Grid.Row="3" Grid.Column="1" Content="{Binding ErrorMessage}" Foreground="Red" HorizontalAlignment="Center"/> <CheckBox Grid.Row="4" Grid.Column="1" Content="Remember Me" Margin="5" IsChecked="{Binding RememberMe}"/> </Grid> </Window> ``` ViewModel代码: ```csharp using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; namespace WpfApp1 { public class LoginViewModel : INotifyPropertyChanged { private string _username; private string _password; private bool _rememberMe; private string _errorMessage; public string Username { get { return _username; } set { _username = value; OnPropertyChanged(); } } public string Password { get { return _password; } set { _password = value; OnPropertyChanged(); } } public bool RememberMe { get { return _rememberMe; } set { _rememberMe = value; OnPropertyChanged(); } } public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } } public ICommand LoginCommand { get; set; } public LoginViewModel() { LoginCommand = new RelayCommand(Login, CanLogin); } private bool CanLogin() { return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password); } private void Login() { if (Username == "admin" && Password == "password") { if (RememberMe) { // Save login information to file or database } MessageBox.Show("Login successful!"); } else { ErrorMessage = "Invalid username or password"; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } ``` 其中,`RelayCommand`是一个实现了`ICommand`接口的自定义命令类,用于绑定按钮的点击事件。你可以在网上找到相关实现代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值