当前位置:网站首页>(04). Net Maui actual MVVM
(04). Net Maui actual MVVM
2022-06-30 03:48:00 【Dotnet cross platform】
1. Summary
This chapter will explain how to MAUI Use the simple MVVM Pattern development “ListView” Content addition and deletion function ,MVVM stay MAUI The same applies to .

Microsoft.Toolkit.Mvvm
Before we study, let's learn about nuget package , It can help us save some code development time . package Microsoft.Toolkit.Mvvm (aka MVVM Toolkit) It's a modern 、 Fast 、 Modular MVVM library . This package is for .NET Standard, In order to use it on any application platform :UWP、WinForms、WPF、Xamarin、Uno etc. ; At any run time :.NET Native、.NET Core、.NET Framework or Mono. It runs on all . In all cases ,API The surfaces are the same , Therefore, it is very suitable for generating shared libraries . In Solution Explorer , Right click on the item , And then choose “ management NuGet package ”. Search for Microsoft.Toolkit.Mvvm And install it .

2. The detailed content
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 Need to inherit
public class MainViewModel : ObservableObject
{
private string _test;
private ObservableCollection<MainModel> _temps;
private ICommand addCommand;
private ICommand deleteCommand;
// Data notification collection
public ObservableCollection<MainModel> Temps { get => _temps; set => _temps = value; }
// command
public ICommand AddCommand { get => addCommand ?? (addCommand = new RelayCommand(AddCallback));}
public ICommand DeleteCommand { get => deleteCommand ?? (deleteCommand = new RelayCommand(DeleteCallback)); }
// Data notification fields
public string Test { get => _test; set => SetProperty(ref _test , value); }
public MainViewModel()
{
// initialization
Temps = new ObservableCollection<MainModel>();
Temps.Add(new MainModel { Name = "zhangsan" });
Temps.Add(new MainModel { Name = "zhangsan" });
}
// Command execution content
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

边栏推荐
- Geometric objects in shapely
- 如何通过进程启动来分析和解决EasyCVR内核端口报错问题?
- Hudi record
- LitJson解析 生成json文件 读取json文件中的字典
- Arrangement of language resources of upgraded version
- Litjson parses the generated JSON file and reads the dictionary in the JSON file
- [0x0] 校长留的开放问题作业
- 第2章 控制结构和函数(编程题)
- [punch in - Blue Bridge Cup] day 1 --% 7F format output
- 1151_ Makefile learning_ Static matching pattern rules in makefile
猜你喜欢

unity input system 使用记录(实例版)

关于智能视觉组上的机械臂

【模糊神经网络预测】基于模糊神经网络实现水质预测含Matlab源码

1152_ Makefile learning_ Pattern matching rules

Node-RED系列(二八):基于OPC UA节点与西门子PLC进行通讯

MySQL performance optimization (5): principle and implementation of master-slave synchronization

The jupyter notebook kernel hangs up frequently and needs to be restarted

Number of students from junior college to Senior College (4)

Interface test tool postman

Half a year after joining the company, I was promoted to a management post
随机推荐
I have published a book, "changing life against the sky - the way for programmers to become gods", which is open source. I have been working for ten years. There are 100 life suggestions from technica
laravel9本地安装
利用反射整合ViewBinding和ViewHolder
[FAQ] page cross domain and interface Cross Domain
Redis is used in Windows system
毕业设计EMS办公管理系统(B/S结构)+J2EE+SQLserver8.0
共124篇!墨天轮“高可用架构”干货文档分享(含Oracle、MySQL、PG)
Feign 坑
What are the defaults for Binding. Mode=Default for WPF controls?
[note] Introduction to data analysis on June 7, 2022
MySQL + JSON = King fried
【论文阅读|深读】DANE:Deep Attributed Network Embedding
【作业】2022.5.25 MySQL 查操作2
dotnet-exec 0.5.0 released
实用调试技巧
What does the hyphen mean for a block in Twig like in {% block body -%}?
Geometric objects in shapely
RPC correction
Is the largest layoff and salary cut on the internet coming?
1151_ Makefile learning_ Static matching pattern rules in makefile