当前位置: 首页 > news >正文

WPF学习笔记(26)CommunityToolkit.Mvvm与MaterialDesignThemes

CommunityToolkit.Mvvm与界面库

  • 一、 CommunityToolkit.Mvvm框架
    • 1. 常用的MVVM框架
    • 2. CommunityToolkit.Mvvm概述
    • 3. CommunityToolkit.Mvvm详解
  • 二、 CommunityToolkit.Mvvm源生成器
    • 1. 源生成器概述
    • 2. 示例
  • 三、界面库MaterialDesignThemes
    • 1. 概述
    • 2. 示例


一、 CommunityToolkit.Mvvm框架

1. 常用的MVVM框架

在这里插入图片描述

2. CommunityToolkit.Mvvm概述

官方文档:https://learn.microsoft.com/zh-cn/dotnet/communitytoolkit/mvvm/
在这里插入图片描述
在这里插入图片描述

3. CommunityToolkit.Mvvm详解

在这里插入图片描述
以上文中的LoginViewModel为例,将自写的BaseViewModel类改为继承自CommunityToolkit.Mvvm中的ObservableObject
在这里插入图片描述
在这里插入图片描述

将自写的MainWindowViewModel类改为继承自CommunityToolkit.Mvvm中的ObservableObject
在这里插入图片描述
在这里插入图片描述

二、 CommunityToolkit.Mvvm源生成器

1. 源生成器概述

官方文档:https://learn.microsoft.com/zh-cn/dotnet/communitytoolkit/mvvm/generators/overview

在这里插入图片描述
在这里插入图片描述

2. 示例

源生成器不支持 .net framework,本次使用 .net6.0
在这里插入图片描述
将上文中的View、ViewModel、Model文件夹及其内容分别放到新建立的解决方案中。
重新配置App.xaml和App.xaml.cs

<Application x:Class="_077.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:_077"ShutdownMode ="OnExplicitShutdown"><!--不设置StartupUri,通过OnStartup 指定启动 --><!--这里OnExplicitShutdown是手动调用Shutdown, 否则程序不会自动关闭程序。--><Application.Resources></Application.Resources>
</Application>
public partial class App : Application{//重写方法protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);//先弹出登录窗口Login login = new Login();bool? ret = login.ShowDialog();if (ret == true){//成功登录显示主窗口MainWindow mainWindow = new MainWindow();mainWindow.ShowDialog();//关闭程序App.Current.Shutdown();}}}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Model;
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.Input;namespace ViewModel
{//批注了之后,源生成器 会将 ViewModel 自动实现 INotifyPropertyChanged[ObservableObject]internal partial class LoginViewModel{//构造函数public LoginViewModel(){}//源生成器自动将首字母大写生成属性[ObservableProperty]private string name = "admin";//源生成器自动将首字母大写生成属性[ObservableProperty]private string? pwd = null;//属性对应,界面上报错Label控件  [ObservableProperty]private string error = "暂无信息";//命令属性,对应界面上的登录事件//源生成器,自动生成Login + Command的类型[RelayCommand]public void Login (object parameter){//Execute方法中实现命令处理逻辑if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Pwd)){Error = "用户名密码不能为空!";return;}//检查登录信息User u = dal.FindUserByName(Name);if (u != null && u.Pwd == Pwd){Window? w = parameter as Window;w.DialogResult = true;//关闭窗口}elseError = "用户名密码错误!";}//数据访问层public DAL dal = new DAL();}
}
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace ViewModel
{//批注了之后,源生成器  会将 ViewModel 自动实现 INotifyPropertyChanged[ObservableObject]internal partial class MainWindowViewModel{//数据访问层public DAL dal = new DAL();public MainWindowViewModel(){Books = dal.GetBookList();}//对应界面上的  ListView显示的数据//ObservableCollection 可以通知界面更新的//源生成器自动将首字母大写生成属性[ObservableProperty]private ObservableCollection<Book> books;//删除命令 ,对应界面的删除按钮的点击操作//源生成器,会自动生成   (方法名)Del + Command 的命令类型,实现ICommand[RelayCommand]public void Del(object parameter){Book b = parameter as Book;//删除执行的内容Books.Remove(b);}}
}

三、界面库MaterialDesignThemes

1. 概述

在这里插入图片描述

官方链接:https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit
在这里插入图片描述
演示app:https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/releases
在这里插入图片描述

2. 示例

在这里插入图片描述

在这里插入图片描述
内容来自链接的演示内容

<Application x:Class="Example.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" /><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign2.Defaults.xaml" /> </ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>

在这里插入图片描述
在这里插入图片描述
演示效果如下:
在这里插入图片描述

http://www.dtcms.com/a/268183.html

相关文章:

  • 如何正确规范的开发术语自己的TYPECHO插件
  • AI做美观PPT:3步流程+工具测评+避坑指南
  • LeetCode 算法题解:链表与二叉树相关问题 打打卡
  • ubuntu 20.04 安装中文输入法 (sougou pin yin)
  • std::forward作用
  • day53
  • 微服务负载均衡全解析:从原理到实践
  • 【Note】《Kafka: The Definitive Guide》第三章: Kafka 生产者深入解析:如何高效写入 Kafka 消息队列
  • HarmonyOS学习6 --- 数据存储
  • windows系统安装mongoDB且创建集合植入初始化数据
  • vue事件处理-按键修饰符
  • 闲庭信步使用图像验证平台加速FPGA的开发:第一课——由测试平台到验证平台
  • CSS06:字体样式
  • 数据结构---链表结构体、指针深入理解(三)
  • Petalinux工程如何离线编译
  • C++ 中左值和右值
  • 论文评价指标之(n-gram、BLEU、MRR、ANLS)
  • python库 maya 库的各种案例的使用详解(人性化的日期时间处理)
  • 使用Python将PDF转换成word、PPT
  • SSL 终结(SSL Termination)深度解析:从原理到实践的全维度指南
  • 电商系统二次开发找谁做?ZKmall开源商城前后端分离技术更易升级迭代
  • leetcode 每日一题 1865. 找出和为指定值的下标对
  • python学习打卡:DAY 21 常见的降维算法
  • 红宝书学习笔记
  • 多级缓存如何应用
  • YOLO目标检测数据集类别:分类与应用
  • Oracle使用SQL一次性向表中插入多行数据
  • NLP之文本纠错开源大模型:兼看语音大模型总结
  • 李宏毅genai笔记:推理
  • Maven引入第三方JAR包实战指南