选择邯郸网站制作,外链发布论坛,做网站对服务器要求,娃哈哈软文推广背景
MVVM 是一种软件架构模式#xff0c;用于创建用户界面。它将用户界面#xff08;View#xff09;、业务逻辑#xff08;ViewModel#xff09;和数据模型#xff08;Model#xff09;分离开来#xff0c;以提高代码的可维护性和可测试性。 MainWindow 类是 View用于创建用户界面。它将用户界面View、业务逻辑ViewModel和数据模型Model分离开来以提高代码的可维护性和可测试性。 MainWindow 类是 View视图负责用户界面的呈现和交互它是用户直接看到和操作的部分。
LoginVM 类是 ViewModel视图模型它充当了 View 和 Model 之间的中介处理了视图与数据模型之间的交互逻辑以及用户操作的响应逻辑。
LoginModel 类是 Model模型它包含了应用程序的数据和业务逻辑用于存储和处理用户的身份验证信息。
展示 代码
LoginModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WpfApp2
{public class LoginModel{private string _UserName;public string UserName{get { return _UserName; }set{_UserName value;}}private string _Password;public string Password{get { return _Password; }set{_Password value;}}}
}
LoginVM.cs
using Sys tem;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;namespace WpfApp2
{public class LoginVM : INotifyPropertyChanged{private MainWindow _main;public LoginVM(MainWindow main){_main main;}public event PropertyChangedEventHandler PropertyChanged;private void RaisePropetyChanged(string propertyName){PropertyChangedEventHandler handler PropertyChanged;if (handler ! null){handler(this, new PropertyChangedEventArgs(propertyName));}}private LoginModel _LoginM new LoginModel();public string UserName{get { return _LoginM.UserName; }set{_LoginM.UserName value;RaisePropetyChanged(UserName);}}public string Password{get { return _LoginM.Password; }set{_LoginM.Password value;RaisePropetyChanged(Password);}}/// summary/// 登录方法/// /summaryvoid Loginfunc(){if (UserName wpf Password 666){MessageBox.Show(OK);Index index new Index();index.Show();//想办法拿到mainwindow_main.Hide();}else{MessageBox.Show(输入的用户名或密码不正确);UserName ;Password ;}}bool CanLoginExecute(){return true;}public ICommand LoginAction{get{return new RelayCommand(Loginfunc,CanLoginExecute);}}}
}
MainWindow.xaml
Window x:ClassWpfApp2.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp2mc:IgnorabledTitleMainWindow Height450 Width800GridGrid.RowDefinitionsRowDefinition Heightauto/RowDefinitionRowDefinition Heightauto/RowDefinitionRowDefinition Height1*/RowDefinitionRowDefinition Height9*/RowDefinition/Grid.RowDefinitionsTextBlock Grid.Row0 Grid.Column0 Text上海市-市图书馆 FontSize18 HorizontalAlignmentCenter/TextBlockStackPanel Grid.Row1 Grid.Column0 Background#0078d4TextBlock Text登录 FontSize22 HorizontalAlignmentCenter ForegroundWheat Margin5/TextBlock /StackPanelGrid Grid.Row3 ShowGridLinesFalse HorizontalAlignmentCenterGrid.RowDefinitionsRowDefinition Height30/RowDefinitionRowDefinition Height30/RowDefinitionRowDefinition Height30/RowDefinitionRowDefinition Height30/RowDefinition/Grid.RowDefinitionsGrid.ColumnDefinitions ColumnDefinition Widthauto/ColumnDefinitionColumnDefinition Width200/ColumnDefinition/Grid.ColumnDefinitionsTextBlock Text用户名 Grid.Row0 Grid.Column0 VerticalAlignmentCenter/TextBlockTextBox Text{Binding UserName} Grid.Row0 Grid.Column1 Margin2 /TextBoxTextBlock Text密码 Grid.Row1 Grid.Column0 VerticalAlignmentCenter/TextBlockTextBox Text{Binding Password} Grid.Row1 Grid.Column1 Margin2/TextBoxCheckBox Grid.ColumnSpan2 Content记住密码 Grid.Row2/CheckBoxlocal:CustomButton ButtonCornerRadius5 BackgroundHoverRed BackgroundPressedGreen Foreground#FFFFFF Background#3C7FF8 Grid.Row3 Grid.Column0 Grid.ColumnSpan2 Command{Binding LoginAction} Height30 VerticalAlignmentTop登录/local:CustomButton/Grid/Grid
/Window
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp2
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{LoginVM loginVM;public MainWindow(){InitializeComponent();loginVM new LoginVM(this);this.DataContext loginVM;}}
}
RelayCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfApp2
{public class RelayCommand : ICommand{/// summary/// 命令是否能够执行/// /summaryreadonly Funcbool _canExecute;/// summary/// 命令需要执行的方法/// /summaryreadonly Action _exexute;public RelayCommand(Action exexute,Funcbool canExecute){_canExecute canExecute;_exexute exexute;}public bool CanExecute(object parameter){if (_canExecute null){return true;}return _canExecute();}public void Execute(object parameter){_exexute();}public event EventHandler CanExecuteChanged{add {if (_canExecute ! null){CommandManager.RequerySuggested value;}}remove{if (_canExecute ! null){CommandManager.RequerySuggested - value;}}}}
}
自定义按钮CustomButton App.xaml.cs
Application x:ClassWpfApp2.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp2StartupUriMainWindow.xamlApplication.ResourcesResourceDictionaryResourceDictionary.MergedDictionariesResourceDictionary SourceCustomButtonStyles.xaml/ResourceDictionary/ResourceDictionary.MergedDictionaries/ResourceDictionary/Application.Resources
/Application
CustomButton.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;namespace WpfApp2
{public class CustomButton:Button{//依赖属性public CornerRadius ButtonCornerRadius{get { return (CornerRadius)GetValue(ButtonCornerRadiusProperty); }set { SetValue(ButtonCornerRadiusProperty, value); }}// Using a DependencyProperty as the backing store for ButtonCornerRadius. This enables animation, styling, binding, etc...public static readonly DependencyProperty ButtonCornerRadiusProperty DependencyProperty.Register(ButtonCornerRadius, typeof(CornerRadius), typeof(CustomButton));public Brush BackgroundHover{get { return (Brush)GetValue(BackgroundHoverProperty); }set { SetValue(BackgroundHoverProperty, value); }}// Using a DependencyProperty as the backing store for BackgroundHover. This enables animation, styling, binding, etc...public static readonly DependencyProperty BackgroundHoverProperty DependencyProperty.Register(BackgroundHover, typeof(Brush), typeof(CustomButton));public Brush BackgroundPressed{get { return (Brush)GetValue(BackgroundPressedProperty); }set { SetValue(BackgroundPressedProperty, value); }}// Using a DependencyProperty as the backing store for BackgroundPressed. This enables animation, styling, binding, etc...public static readonly DependencyProperty BackgroundPressedProperty DependencyProperty.Register(BackgroundPressed, typeof(Brush), typeof(CustomButton));}
}
数据字典 CustombuttonStyles.xaml
ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:bbclr-namespace:WpfApp2Style TargetType{x:Type bb:CustomButton}Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type bb:CustomButton}Border x:NamebuttonBorder Background{TemplateBinding Background} CornerRadius{TemplateBinding ButtonCornerRadius}TextBlock Text{TemplateBinding Content} HorizontalAlignment{TemplateBinding HorizontalContentAlignment} VerticalAlignment{TemplateBinding VerticalContentAlignment}/TextBlock/Border!--触发器--ControlTemplate.TriggersTrigger PropertyIsMouseOver ValueTrueSetter TargetNamebuttonBorder PropertyBackground Value{Binding BackgroundHover,RelativeSource{RelativeSource TemplatedParent}}/Setter/TriggerTrigger PropertyIsPressed ValueTrueSetter TargetNamebuttonBorder PropertyBackground Value{Binding BackgroundPressed,RelativeSource{RelativeSource TemplatedParent}}/Setter/Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style
/ResourceDictionary