网站建设售后服务承诺,做网站文件夹都起什么名字,凡科商城小程序怎么样,大连ui设计MVVM简介 
在WPF应用程序开发中#xff0c;MVVM#xff08;Model-View-ViewModel#xff09;是一种非常流行的架构模式。它为应用程序的设计提供了良好的分层结构和可扩展性。 
结构分为下列三部分 Model#xff1a;定义了应用程序的数据模型 就是系统中的对象#xff0c;…MVVM简介 
在WPF应用程序开发中MVVMModel-View-ViewModel是一种非常流行的架构模式。它为应用程序的设计提供了良好的分层结构和可扩展性。 
结构分为下列三部分 Model定义了应用程序的数据模型 就是系统中的对象可包含属性和行为是一个class实体是对现实中事物的抽象开发过程中涉及到的事物都可以抽象为Model例如用户的账号、密码、电话等负责从数据源中获取数据并将其提供给ViewModel。  ViewModel封装了应用程序的业务逻辑通过View类的DataContext属性绑定到View负责将数据从Model传递到View并将用户交互事件传递回Model。显示数据对应ViewMode中的Property执行命令对应ViewModel中的Command。  View用xaml实现的界面接收用户输入把数据展现给用户并与ViewModel交互以便进行数据绑定和命令绑定。  
在MVVM模式中ViewModel的主要职责是将数据从Model传递到View并响应View的用户交互事件。ViewModel通过命令绑定和数据绑定与View进行交互。 
MVVM优点 
低耦合视图View可以独立于Model变化和修改一个ViewModel可以绑定到不同的”View”上当View变化的时候Model可以不变当Model变化的时候View也可以不变。灵活扩展可以把一些视图逻辑放在一个ViewModel里面让很多view重用这段视图逻辑易测试可以针对ViewModel来写测试用例独立开发开发人员可以专注于业务逻辑和数据的开发ViewModel设计人员可以专注于xaml页面设计。 
MVVM示例 
以下是一个简单的WPF MVVM登录示例 
底层通用实体 
ViewModelBase  
添加一个viewmodelbase方便后续数据双向绑定更新 
using System.ComponentModel;
using System.Runtime.CompilerServices;namespace YourProjectName.Comm
{public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void PC(string propertyName){this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}public void PCEH([CallerMemberName] string propertyName  null){this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
} 
CommandBase  
添加一个继承ICommand实体方便后续绑定command 
using System;
using System.Windows.Input;namespace YourProjectName.Comm
{public class CommandBase : ICommand{private readonly Actionobject _execute;private readonly Funcobject, bool _canExecute;public event EventHandler CanExecuteChanged{add{CommandManager.RequerySuggested  value;}remove{CommandManager.RequerySuggested - value;}}public CommandBase(Actionobject execute): this(execute, null){}public CommandBase(Actionobject execute, Funcobject, bool canExecute){_execute  execute ?? throw new ArgumentNullException(execute);_canExecute  canExecute ?? ((Funcobject, bool)((object x)  true));}public bool CanExecute(object parameter){return _canExecute(parameter);}public void Execute(object parameter){_execute(parameter);}public void Refresh(){CommandManager.InvalidateRequerySuggested();}}
} 
ViewModel及Model 
在ViewModel文件夹下创建一个名为LoginViewModel的类。 因为只有两个字段测试所以未新建Model实际开发中注意新建 
using System.Windows.Input;
using YourProjectName.Comm;namespace YourProjectName.ViewModel 
{public class LoginViewModel : ViewModelBase {private string _username;private string _password;private bool _rememberMe;public string Username {get { return _username; }set { _username value; PCEH();}}public string Password {get { return _password; }set { _password value; PCEH();}}public bool RememberMe {get { return _rememberMe; }set { _rememberMe value; PCEH();}}public ICommand LoginCommand;private void Login() {LoginCommand  new CommandBase(async l {// TODO: Add login logic.
});}}
}View 
在View文件夹下创建一个名为LoginView的XAML和CS文件。 
Window x:ClassYourProjectName.View.LoginViewxmlnshttp://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:YourProjectName.Viewxmlns:vmclr-namespace:YourProjectName.ViewModelmc:IgnorabledTitleLoginView Height300 Width400GridGrid.RowDefinitionsRowDefinition HeightAuto /RowDefinition HeightAuto /RowDefinition HeightAuto /RowDefinition HeightAuto /RowDefinition HeightAuto //Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition /ColumnDefinition //Grid.ColumnDefinitionsLabel Grid.Row0 Grid.Column0 ContentUsername: /TextBox Grid.Row0 Grid.Column1 Text{Binding Username} /Label Grid.Row1 Grid.Column0 ContentPassword: /PasswordBox Grid.Row1 Grid.Column1 Password{Binding Password, UpdateSourceTriggerPropertyChanged} /CheckBox Grid.Row2 Grid.Column1 ContentRemember me IsChecked{Binding RememberMe} /Button Grid.Row3 Grid.Column0 Grid.ColumnSpan2 ContentLogin Command{Binding LoginCommand} /TextBlock Grid.Row4 Grid.Column0 Grid.ColumnSpan2 TextDont have an account? Sign up. //Grid
/Window初始化DataContext 也可在页面中初始化 
using System.Windows;namespace YourProjectName.View 
{public partial class LoginView : Window {private readonly LoginViewModel model;public LoginView() {InitializeComponent();DataContext  model  new();}}
}启动应用程序并测试登录页面。在“TODO”注释的位置添加实际的登录代码。 
全局异常处理 
在WPF应用程序中全局异常处理非常重要。全局异常处理可以帮助我们捕获应用程序中的所有未处理异常防止程序异常崩溃并提供更好的用户体验。在WPF中可以通过在应用程序的App.xaml.cs文件中添加以下代码来实现全局异常处理 
异常处理示例 
public partial class App : Application
{protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);AppDomain.CurrentDomain.UnhandledException  CurrentDomain_UnhandledException;DispatcherUnhandledException  App_DispatcherUnhandledException;TaskScheduler.UnobservedTaskException  TaskScheduler_UnobservedTaskException;}private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e){//处理UI线程上的未处理异常e.Handled  true;MessageBox.Show(发生错误  e.Exception.Message, 错误, MessageBoxButton.OK, MessageBoxImage.Error);}private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e){//处理非UI线程上的未处理异常Exception ex  (Exception)e.ExceptionObject;MessageBox.Show(发生错误  ex.Message, 错误, MessageBoxButton.OK, MessageBoxImage.Error);}/// summary/// Task线程内未捕获异常处理事件/// /summary/// param namesender/param/// param namee/paramprivate void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs ex){MessageBox.Show(Task线程异常  ex.Message, 错误, MessageBoxButton.OK, MessageBoxImage.Error);//设置该异常已察觉这样处理后就不会引起程序崩溃e.SetObserved();}
}以上代码会在应用程序启动时为当前域的未处理异常事件和UI线程上的未处理异常事件及task线程异常注册处理程序。在发生异常时会弹出一个消息框来通知用户并不会造成程序崩溃。 总结 
MVVM模式是WPF应用程序开发中非常重要的主题。通过理解和实战练习我们可以更好地开发出强大和稳定的应用程序。 
全局异常处理是程序中必不可少的一步能保障程序在异常发生过程中正常运行。