• 适用于初学者的 .NET MAUI


    适用于初学者的 .NET MAUI | Microsoft Learn

    记录微软Learn中用到的代码。文章比较粗糙,大部分是项目代码粘贴。想详细学习的可到上面的链接学习,代码可以从这里复制后直接运行。

    练习中一共有两个页面:

    1、MainPage.xaml 用于添加列表中的内容。主要功能有向列表中添加一项;左滑删除该项;点击该选项进入详情页面。

    2、DetailPage.xaml 显示详细信息,实际上显示的很少。返回主界面。

    用到的MVVM包有:

    1、CommunityToolkit.Mvvm

    接下来是MainPage.xaml代码:

    1. "1.0" encoding="utf-8" ?>
    2. "http://schemas.microsoft.com/dotnet/2021/maui"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    4. x:Class="MauiAppDemo1.MainPage"
    5. xmlns:viewmode="clr-namespace:MauiAppDemo1.ViewModel"
    6. x:DataType="viewmode:MainViewModel">
    7. "100,Auto,*"
    8. ColumnDefinitions=".75*,.25*"
    9. Padding="10"
    10. RowSpacing="10"
    11. ColumnSpacing="10">
    12. "2"
    13. Source="dotnet_bot.png"
    14. Background="white"/>
    15. "Enter task"
    16. Text="{Binding Text}"
    17. Grid.Row="1"/>
    18. Command="{Binding AddCommand}"
    19. Grid.Row="1"
    20. Grid.Column="1"/>
    21. "2" Grid.ColumnSpan="2"
    22. ItemsSource="{Binding Items}"
    23. SelectionMode="None">
    24. "{x:Type x:String}">
    25. "Delete"
    26. BackgroundColor="Red"
    27. Command="{Binding Source={RelativeSource AncestorType={x:Type viewmode:MainViewModel}},Path=DeleteCommand}"
    28. CommandParameter="{Binding .}"/>
    29. "0,5">
    30. "{Binding Source={RelativeSource AncestorType={x:Type viewmode:MainViewModel}},Path=TapCommand}"
    31. CommandParameter="{Binding .}"/>
    32. FontSize="24"/>
    1. using MauiAppDemo1.ViewModel;
    2. namespace MauiAppDemo1;
    3. public partial class MainPage : ContentPage
    4. {
    5. int count = 0;
    6. public MainPage(MainViewModel vm)
    7. {
    8. InitializeComponent();
    9. BindingContext = vm;
    10. }
    11. }
    1. using MauiAppDemo1.ViewModel;
    2. namespace MauiAppDemo1;
    3. public static class MauiProgram
    4. {
    5. public static MauiApp CreateMauiApp()
    6. {
    7. var builder = MauiApp.CreateBuilder();
    8. builder
    9. .UseMauiApp()
    10. .ConfigureFonts(fonts =>
    11. {
    12. fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    13. fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    14. });
    15. builder.Services.AddSingleton();
    16. builder.Services.AddSingleton();
    17. builder.Services.AddTransient();
    18. builder.Services.AddSingleton();
    19. return builder.Build();
    20. }
    21. }
    1. namespace MauiAppDemo1;
    2. public partial class AppShell : Shell
    3. {
    4. public AppShell()
    5. {
    6. InitializeComponent();
    7. Routing.RegisterRoute(nameof(DetailPage),typeof(DetailPage));
    8. }
    9. }
    1. using CommunityToolkit.Mvvm.ComponentModel;
    2. using CommunityToolkit.Mvvm.Input;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Collections.ObjectModel;
    6. using System.ComponentModel;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. namespace MauiAppDemo1.ViewModel
    11. {
    12. ///
    13. /// CommunityToolkit.Mvvm
    14. ///
    15. public partial class MainViewModel : ObservableObject
    16. {
    17. public MainViewModel()
    18. {
    19. items = new ObservableCollection<string>();
    20. }
    21. [ObservableProperty]
    22. ObservableCollection<string> items;
    23. [ObservableProperty]
    24. string text;
    25. [RelayCommand]
    26. void Add()
    27. {
    28. if (string.IsNullOrWhiteSpace(Text)) return;
    29. items.Add(Text);
    30. Text = string.Empty;
    31. }
    32. [RelayCommand]
    33. void Delete(string s)
    34. {
    35. if (items.Contains(s))
    36. {
    37. items.Remove(s);
    38. }
    39. }
    40. [RelayCommand]
    41. async Task Tap(string s)
    42. {
    43. await Shell.Current.GoToAsync($"{nameof(DetailPage)}?Text={s}");
    44. }
    45. }
    46. ///
    47. /// 基本写法
    48. ///
    49. //public class MainViewModel : INotifyPropertyChanged
    50. //{
    51. // string text;
    52. // public string Text
    53. // {
    54. // get => text;
    55. // set
    56. // {
    57. // text=value;
    58. // OnPropertyChanged(nameof(Text));
    59. // }
    60. // }
    61. // public event PropertyChangedEventHandler PropertyChanged;
    62. // void OnPropertyChanged(string name)=>PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    63. //}
    64. }

    DetailPage.xaml

                    

    1. "1.0" encoding="utf-8" ?>
    2. "http://schemas.microsoft.com/dotnet/2021/maui"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    4. x:Class="MauiAppDemo1.DetailPage"
    5. xmlns:viewmodel="clr-namespace:MauiAppDemo1.ViewModel"
    6. x:DataType="viewmodel:DetailViewModel"
    7. Title="DetailPage">
    8. "20">
    9. Text="{Binding Text}"
    10. FontSize="25"
    11. VerticalOptions="Center"
    12. HorizontalOptions="Center" />
    13. Command="{Binding GoBackCommand}"/>
    1. using MauiAppDemo1.ViewModel;
    2. namespace MauiAppDemo1;
    3. public partial class DetailPage : ContentPage
    4. {
    5. public DetailPage(DetailViewModel vm)
    6. {
    7. InitializeComponent();
    8. BindingContext = vm;
    9. }
    10. }
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using CommunityToolkit.Mvvm.ComponentModel;
    7. using CommunityToolkit.Mvvm.Input;
    8. namespace MauiAppDemo1.ViewModel
    9. {
    10. [QueryProperty("Text","Text")]
    11. public partial class DetailViewModel:ObservableObject
    12. {
    13. [ObservableProperty]
    14. string text;
    15. [RelayCommand]
    16. async Task GoBack()
    17. {
    18. await Shell.Current.GoToAsync("..");
    19. }
    20. }
    21. }

  • 相关阅读:
    Cesium 空间量算——面积量算
    【LeetCode:1402. 做菜顺序 | 动态规划 + 贪心】
    1.1. Java简介与安装
    Docker 容器文件(数据)共享
    【已解决】使用Appium Inspector及uiautomatorviewer无法定位浮窗内元素
    IP-adapter masking
    大型客户关系管理系统源码CRM
    IO Watch:用 Arduino UNO 制造的可编程手表
    【Linux操作系统】——配置安装系统环境
    美创科技信创数据安全「利基者」!
  • 原文地址:https://blog.csdn.net/xingchengaiwei/article/details/134359742