当前位置:网站首页>(04).NET MAUI实战 MVVM
(04).NET MAUI实战 MVVM
2022-06-30 03:34:00 【dotNET跨平台】
1.概要
本章将讲解如何在MAUI中使用简单的MVVM模式开发“ListView”内容的增删功能,MVVM在MAUI中也同样适用。
Microsoft.Toolkit.Mvvm
在学习之前我们先了解一个nuget包,它可以帮助我们省去一些代码的开发时间。包Microsoft.Toolkit.Mvvm (aka MVVM Toolkit) 是一个现代、快速、模块化的 MVVM 库。此包面向 .NET Standard,以便在任何应用平台上使用它:UWP、WinForms、WPF、Xamarin、Uno 等;在任何运行时:.NET Native、.NET Core、.NET Framework或 Mono。它在所有上运行。在所有情况下,API 图面都是相同的,因此非常适合生成共享库。在解决方案资源管理器中,右键单击项目,然后选择“管理NuGet包”。搜索 Microsoft.Toolkit。Mvvm 并安装它。
2.详细内容
Project
View
<Grid RowDefinitions="500,50">
<ListView ItemsSource="{Binding Temps}"/>
<StackLayout Grid.Row="1">
<Button WidthRequest="100" HeightRequest="25" Text="add" Command="{Binding AddCommand}"/>
<Button WidthRequest="100" HeightRequest="25" Text="add" Command="{Binding DeleteCommand}"/>
</StackLayout>
</Grid>
ViewModel
//ViewModel需继承
public class MainViewModel : ObservableObject
{
private string _test;
private ObservableCollection<MainModel> _temps;
private ICommand addCommand;
private ICommand deleteCommand;
//数据通知集合
public ObservableCollection<MainModel> Temps { get => _temps; set => _temps = value; }
//命令
public ICommand AddCommand { get => addCommand ?? (addCommand = new RelayCommand(AddCallback));}
public ICommand DeleteCommand { get => deleteCommand ?? (deleteCommand = new RelayCommand(DeleteCallback)); }
//数据通知字段
public string Test { get => _test; set => SetProperty(ref _test , value); }
public MainViewModel()
{
//初始化
Temps = new ObservableCollection<MainModel>();
Temps.Add(new MainModel { Name = "zhangsan" });
Temps.Add(new MainModel { Name = "zhangsan" });
}
//命令执行内容
private void AddCallback()
{
Temps.Add(new MainModel { Name = DateTime.Now.ToString() });
}
private void DeleteCallback()
{
Temps.RemoveAt(0);
}
}
Model
public class MainModel
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
Run
边栏推荐
- Hudi record
- AppData文件夹下Local,Locallow和Roaming
- [punch in - Blue Bridge Cup] day 4--------- split ('') cannot be used. There is a space after the last number of test cases. Split ()
- Litjson parses the generated JSON file and reads the dictionary in the JSON file
- Personal PC installation software
- SDS understanding in redis
- MySQL performance optimization (6): read write separation
- Regular full match: the password consists of more than 8 digits, upper and lower case letters, and special characters
- Play with algorithm interview together, nanny level strategy (with high-definition codeless algorithm summary map), recommended collection
- 云原生——Web实时通信技术之Websocket
猜你喜欢
[ten minutes] manim installation 2022
OP diode limit swing
Redis高并发分布式锁(学习总结)
51单片机的室内环境监测系统,MQ-2烟雾传感器和DHT11温湿度传感器,原理图,C编程和仿真
[practical skills] how to write agile development documents
Redis is used in Windows system
Use of custom MVC
王爽-汇编语言 万字学习总结
From 2500 a month, no one wants to go to the programming road of the technical director of a large factory | my ten years
[Note] ab Test and Variance Analysis
随机推荐
How to view Tencent's 2022 school recruitment salary, the total contract of cabbage is 40W?
数据库的下一个变革方向——云原生数据库
If you can tell whether the external stock index futures trading platform I am trading is formal and safe?
Neo4j--- performance optimization
UML图与List集合
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
MySQL performance optimization (6): read write separation
Global and Chinese market for sensor screwdrivers 2022-2028: Research Report on technology, participants, trends, market size and share
[punch in - Blue Bridge Cup] day 3 --- slice in reverse order list[: -1]
专升本高数(四)
MySQL + JSON = King fried
LitJson解析 生成json文件 读取json文件中的字典
专升本语文资源整理
Reasons for MySQL master-slave database synchronization failure
[Note] ab Test and Variance Analysis
[frequently asked questions] modularization of browser environment and node environment
X书6.97版本shield-unidbg调用方式
【笔记】2022.6.7 数据分析概论
How to use Jersey to get the complete rest request body- How to get full REST request body using Jersey?
Tidb 6.0: making Tso more efficient tidb Book rush