翼度科技»论坛 编程开发 .net 查看内容

【C#/.NET】MAUI上的依赖注入

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
引言

        在移动应用开发中,依赖注入是一项非常重要的技术,它可以帮助我们简化代码结构、提高可维护性并增加测试覆盖率。在最新的.NET跨平台框架MAUI中,我们也可以利用依赖注入来构建高效的应用程序架构。本文将详细介绍在MAUI上如何使用依赖注入,旨在帮助开发者更好地理解和应用这一技术。
什么是依赖注入?

        依赖注入是一种设计模式,它通过将对象的创建和依赖关系的管理交给容器来简化应用程序的开发。依赖注入有助于解耦组件之间的依赖关系,使得代码更加灵活、可扩展并且易于测试。
为什么在MAUI上使用依赖注入?

        在MAUI中,应用程序需要处理各种不同的服务、组件和资源,而这些依赖关系的管理可能会变得非常复杂。使用依赖注入可以有效地解耦这些依赖关系,使得我们能够更加专注于应用程序的业务逻辑,而无需关注底层的实现细节。
如何在MAUI上使用依赖注入?

        首先创建好一个.NET MAUI项目之后,需要有以下前提条件
  1. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              x:Class="QuickCalc.App.MainPage">
  4.     <Label VerticalTextAlignment="Center"
  5.            HorizontalTextAlignment="Center"
  6.            Text="{Binding LabelText}"/>
  7. </ContentPage>
复制代码
 
  1. namespace QuickCalc.App.ViewModels;public class LabelViewModel{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              x:Class="QuickCalc.App.MainPage">
  4.     <Label VerticalTextAlignment="Center"
  5.            HorizontalTextAlignment="Center"
  6.            Text="{Binding LabelText}"/>
  7. </ContentPage>public string LabelText { get; set; } = "Hello World";}
复制代码
 
        我们通过依赖注入将LabelText属性绑定到Label的Text上。
 
  1. var builder = MauiApp.CreateBuilder();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              x:Class="QuickCalc.App.MainPage">
  4.     <Label VerticalTextAlignment="Center"
  5.            HorizontalTextAlignment="Center"
  6.            Text="{Binding LabelText}"/>
  7. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  8.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  9.              x:Class="QuickCalc.App.MainPage">
  10.     <Label VerticalTextAlignment="Center"
  11.            HorizontalTextAlignment="Center"
  12.            Text="{Binding LabelText}"/>
  13. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  14.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  15.              x:Class="QuickCalc.App.MainPage">
  16.     <Label VerticalTextAlignment="Center"
  17.            HorizontalTextAlignment="Center"
  18.            Text="{Binding LabelText}"/>
  19. </ContentPage>builder<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  20.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  21.              x:Class="QuickCalc.App.MainPage">
  22.     <Label VerticalTextAlignment="Center"
  23.            HorizontalTextAlignment="Center"
  24.            Text="{Binding LabelText}"/>
  25. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  26.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  27.              x:Class="QuickCalc.App.MainPage">
  28.     <Label VerticalTextAlignment="Center"
  29.            HorizontalTextAlignment="Center"
  30.            Text="{Binding LabelText}"/>
  31. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  32.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  33.              x:Class="QuickCalc.App.MainPage">
  34.     <Label VerticalTextAlignment="Center"
  35.            HorizontalTextAlignment="Center"
  36.            Text="{Binding LabelText}"/>
  37. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  38.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  39.              x:Class="QuickCalc.App.MainPage">
  40.     <Label VerticalTextAlignment="Center"
  41.            HorizontalTextAlignment="Center"
  42.            Text="{Binding LabelText}"/>
  43. </ContentPage>.UseMauiApp()<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  44.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  45.              x:Class="QuickCalc.App.MainPage">
  46.     <Label VerticalTextAlignment="Center"
  47.            HorizontalTextAlignment="Center"
  48.            Text="{Binding LabelText}"/>
  49. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  50.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  51.              x:Class="QuickCalc.App.MainPage">
  52.     <Label VerticalTextAlignment="Center"
  53.            HorizontalTextAlignment="Center"
  54.            Text="{Binding LabelText}"/>
  55. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  56.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  57.              x:Class="QuickCalc.App.MainPage">
  58.     <Label VerticalTextAlignment="Center"
  59.            HorizontalTextAlignment="Center"
  60.            Text="{Binding LabelText}"/>
  61. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  62.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  63.              x:Class="QuickCalc.App.MainPage">
  64.     <Label VerticalTextAlignment="Center"
  65.            HorizontalTextAlignment="Center"
  66.            Text="{Binding LabelText}"/>
  67. </ContentPage>.ConfigureFonts(fonts =><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  68.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  69.              x:Class="QuickCalc.App.MainPage">
  70.     <Label VerticalTextAlignment="Center"
  71.            HorizontalTextAlignment="Center"
  72.            Text="{Binding LabelText}"/>
  73. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  74.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  75.              x:Class="QuickCalc.App.MainPage">
  76.     <Label VerticalTextAlignment="Center"
  77.            HorizontalTextAlignment="Center"
  78.            Text="{Binding LabelText}"/>
  79. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  80.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  81.              x:Class="QuickCalc.App.MainPage">
  82.     <Label VerticalTextAlignment="Center"
  83.            HorizontalTextAlignment="Center"
  84.            Text="{Binding LabelText}"/>
  85. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  86.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  87.              x:Class="QuickCalc.App.MainPage">
  88.     <Label VerticalTextAlignment="Center"
  89.            HorizontalTextAlignment="Center"
  90.            Text="{Binding LabelText}"/>
  91. </ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  92.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  93.              x:Class="QuickCalc.App.MainPage">
  94.     <Label VerticalTextAlignment="Center"
  95.            HorizontalTextAlignment="Center"
  96.            Text="{Binding LabelText}"/>
  97. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  98.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  99.              x:Class="QuickCalc.App.MainPage">
  100.     <Label VerticalTextAlignment="Center"
  101.            HorizontalTextAlignment="Center"
  102.            Text="{Binding LabelText}"/>
  103. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  104.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  105.              x:Class="QuickCalc.App.MainPage">
  106.     <Label VerticalTextAlignment="Center"
  107.            HorizontalTextAlignment="Center"
  108.            Text="{Binding LabelText}"/>
  109. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  110.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  111.              x:Class="QuickCalc.App.MainPage">
  112.     <Label VerticalTextAlignment="Center"
  113.            HorizontalTextAlignment="Center"
  114.            Text="{Binding LabelText}"/>
  115. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  116.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  117.              x:Class="QuickCalc.App.MainPage">
  118.     <Label VerticalTextAlignment="Center"
  119.            HorizontalTextAlignment="Center"
  120.            Text="{Binding LabelText}"/>
  121. </ContentPage>fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  122.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  123.              x:Class="QuickCalc.App.MainPage">
  124.     <Label VerticalTextAlignment="Center"
  125.            HorizontalTextAlignment="Center"
  126.            Text="{Binding LabelText}"/>
  127. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  128.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  129.              x:Class="QuickCalc.App.MainPage">
  130.     <Label VerticalTextAlignment="Center"
  131.            HorizontalTextAlignment="Center"
  132.            Text="{Binding LabelText}"/>
  133. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  134.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  135.              x:Class="QuickCalc.App.MainPage">
  136.     <Label VerticalTextAlignment="Center"
  137.            HorizontalTextAlignment="Center"
  138.            Text="{Binding LabelText}"/>
  139. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  140.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  141.              x:Class="QuickCalc.App.MainPage">
  142.     <Label VerticalTextAlignment="Center"
  143.            HorizontalTextAlignment="Center"
  144.            Text="{Binding LabelText}"/>
  145. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  146.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  147.              x:Class="QuickCalc.App.MainPage">
  148.     <Label VerticalTextAlignment="Center"
  149.            HorizontalTextAlignment="Center"
  150.            Text="{Binding LabelText}"/>
  151. </ContentPage>fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  152.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  153.              x:Class="QuickCalc.App.MainPage">
  154.     <Label VerticalTextAlignment="Center"
  155.            HorizontalTextAlignment="Center"
  156.            Text="{Binding LabelText}"/>
  157. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  158.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  159.              x:Class="QuickCalc.App.MainPage">
  160.     <Label VerticalTextAlignment="Center"
  161.            HorizontalTextAlignment="Center"
  162.            Text="{Binding LabelText}"/>
  163. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  164.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  165.              x:Class="QuickCalc.App.MainPage">
  166.     <Label VerticalTextAlignment="Center"
  167.            HorizontalTextAlignment="Center"
  168.            Text="{Binding LabelText}"/>
  169. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  170.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  171.              x:Class="QuickCalc.App.MainPage">
  172.     <Label VerticalTextAlignment="Center"
  173.            HorizontalTextAlignment="Center"
  174.            Text="{Binding LabelText}"/>
  175. </ContentPage>});#if DEBUG<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  176.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  177.              x:Class="QuickCalc.App.MainPage">
  178.     <Label VerticalTextAlignment="Center"
  179.            HorizontalTextAlignment="Center"
  180.            Text="{Binding LabelText}"/>
  181. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  182.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  183.              x:Class="QuickCalc.App.MainPage">
  184.     <Label VerticalTextAlignment="Center"
  185.            HorizontalTextAlignment="Center"
  186.            Text="{Binding LabelText}"/>
  187. </ContentPage>builder.Logging.AddDebug();#endif<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  188.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  189.              x:Class="QuickCalc.App.MainPage">
  190.     <Label VerticalTextAlignment="Center"
  191.            HorizontalTextAlignment="Center"
  192.            Text="{Binding LabelText}"/>
  193. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  194.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  195.              x:Class="QuickCalc.App.MainPage">
  196.     <Label VerticalTextAlignment="Center"
  197.            HorizontalTextAlignment="Center"
  198.            Text="{Binding LabelText}"/>
  199. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  200.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  201.              x:Class="QuickCalc.App.MainPage">
  202.     <Label VerticalTextAlignment="Center"
  203.            HorizontalTextAlignment="Center"
  204.            Text="{Binding LabelText}"/>
  205. </ContentPage>return builder.Build();
复制代码
 
第一步安装Microsoft.Extensions.DependencyInjection
  1. Install-Package Microsoft.Extensions.DependencyInjection
复制代码
 
第二步打开MauiProgram.cs
  1.   public static MauiApp CreateMauiApp()<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              x:Class="QuickCalc.App.MainPage">
  4.     <Label VerticalTextAlignment="Center"
  5.            HorizontalTextAlignment="Center"
  6.            Text="{Binding LabelText}"/>
  7. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  8.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  9.              x:Class="QuickCalc.App.MainPage">
  10.     <Label VerticalTextAlignment="Center"
  11.            HorizontalTextAlignment="Center"
  12.            Text="{Binding LabelText}"/>
  13. </ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  14.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  15.              x:Class="QuickCalc.App.MainPage">
  16.     <Label VerticalTextAlignment="Center"
  17.            HorizontalTextAlignment="Center"
  18.            Text="{Binding LabelText}"/>
  19. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  20.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  21.              x:Class="QuickCalc.App.MainPage">
  22.     <Label VerticalTextAlignment="Center"
  23.            HorizontalTextAlignment="Center"
  24.            Text="{Binding LabelText}"/>
  25. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  26.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  27.              x:Class="QuickCalc.App.MainPage">
  28.     <Label VerticalTextAlignment="Center"
  29.            HorizontalTextAlignment="Center"
  30.            Text="{Binding LabelText}"/>
  31. </ContentPage>var builder = MauiApp.CreateBuilder();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  32.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  33.              x:Class="QuickCalc.App.MainPage">
  34.     <Label VerticalTextAlignment="Center"
  35.            HorizontalTextAlignment="Center"
  36.            Text="{Binding LabelText}"/>
  37. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  38.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  39.              x:Class="QuickCalc.App.MainPage">
  40.     <Label VerticalTextAlignment="Center"
  41.            HorizontalTextAlignment="Center"
  42.            Text="{Binding LabelText}"/>
  43. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  44.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  45.              x:Class="QuickCalc.App.MainPage">
  46.     <Label VerticalTextAlignment="Center"
  47.            HorizontalTextAlignment="Center"
  48.            Text="{Binding LabelText}"/>
  49. </ContentPage>builder<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  50.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  51.              x:Class="QuickCalc.App.MainPage">
  52.     <Label VerticalTextAlignment="Center"
  53.            HorizontalTextAlignment="Center"
  54.            Text="{Binding LabelText}"/>
  55. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  56.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  57.              x:Class="QuickCalc.App.MainPage">
  58.     <Label VerticalTextAlignment="Center"
  59.            HorizontalTextAlignment="Center"
  60.            Text="{Binding LabelText}"/>
  61. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  62.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  63.              x:Class="QuickCalc.App.MainPage">
  64.     <Label VerticalTextAlignment="Center"
  65.            HorizontalTextAlignment="Center"
  66.            Text="{Binding LabelText}"/>
  67. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  68.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  69.              x:Class="QuickCalc.App.MainPage">
  70.     <Label VerticalTextAlignment="Center"
  71.            HorizontalTextAlignment="Center"
  72.            Text="{Binding LabelText}"/>
  73. </ContentPage>.UseMauiApp()<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  74.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  75.              x:Class="QuickCalc.App.MainPage">
  76.     <Label VerticalTextAlignment="Center"
  77.            HorizontalTextAlignment="Center"
  78.            Text="{Binding LabelText}"/>
  79. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  80.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  81.              x:Class="QuickCalc.App.MainPage">
  82.     <Label VerticalTextAlignment="Center"
  83.            HorizontalTextAlignment="Center"
  84.            Text="{Binding LabelText}"/>
  85. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  86.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  87.              x:Class="QuickCalc.App.MainPage">
  88.     <Label VerticalTextAlignment="Center"
  89.            HorizontalTextAlignment="Center"
  90.            Text="{Binding LabelText}"/>
  91. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  92.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  93.              x:Class="QuickCalc.App.MainPage">
  94.     <Label VerticalTextAlignment="Center"
  95.            HorizontalTextAlignment="Center"
  96.            Text="{Binding LabelText}"/>
  97. </ContentPage>.ConfigureFonts(fonts =><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  98.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  99.              x:Class="QuickCalc.App.MainPage">
  100.     <Label VerticalTextAlignment="Center"
  101.            HorizontalTextAlignment="Center"
  102.            Text="{Binding LabelText}"/>
  103. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  104.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  105.              x:Class="QuickCalc.App.MainPage">
  106.     <Label VerticalTextAlignment="Center"
  107.            HorizontalTextAlignment="Center"
  108.            Text="{Binding LabelText}"/>
  109. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  110.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  111.              x:Class="QuickCalc.App.MainPage">
  112.     <Label VerticalTextAlignment="Center"
  113.            HorizontalTextAlignment="Center"
  114.            Text="{Binding LabelText}"/>
  115. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  116.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  117.              x:Class="QuickCalc.App.MainPage">
  118.     <Label VerticalTextAlignment="Center"
  119.            HorizontalTextAlignment="Center"
  120.            Text="{Binding LabelText}"/>
  121. </ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  122.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  123.              x:Class="QuickCalc.App.MainPage">
  124.     <Label VerticalTextAlignment="Center"
  125.            HorizontalTextAlignment="Center"
  126.            Text="{Binding LabelText}"/>
  127. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  128.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  129.              x:Class="QuickCalc.App.MainPage">
  130.     <Label VerticalTextAlignment="Center"
  131.            HorizontalTextAlignment="Center"
  132.            Text="{Binding LabelText}"/>
  133. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  134.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  135.              x:Class="QuickCalc.App.MainPage">
  136.     <Label VerticalTextAlignment="Center"
  137.            HorizontalTextAlignment="Center"
  138.            Text="{Binding LabelText}"/>
  139. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  140.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  141.              x:Class="QuickCalc.App.MainPage">
  142.     <Label VerticalTextAlignment="Center"
  143.            HorizontalTextAlignment="Center"
  144.            Text="{Binding LabelText}"/>
  145. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  146.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  147.              x:Class="QuickCalc.App.MainPage">
  148.     <Label VerticalTextAlignment="Center"
  149.            HorizontalTextAlignment="Center"
  150.            Text="{Binding LabelText}"/>
  151. </ContentPage>fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  152.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  153.              x:Class="QuickCalc.App.MainPage">
  154.     <Label VerticalTextAlignment="Center"
  155.            HorizontalTextAlignment="Center"
  156.            Text="{Binding LabelText}"/>
  157. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  158.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  159.              x:Class="QuickCalc.App.MainPage">
  160.     <Label VerticalTextAlignment="Center"
  161.            HorizontalTextAlignment="Center"
  162.            Text="{Binding LabelText}"/>
  163. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  164.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  165.              x:Class="QuickCalc.App.MainPage">
  166.     <Label VerticalTextAlignment="Center"
  167.            HorizontalTextAlignment="Center"
  168.            Text="{Binding LabelText}"/>
  169. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  170.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  171.              x:Class="QuickCalc.App.MainPage">
  172.     <Label VerticalTextAlignment="Center"
  173.            HorizontalTextAlignment="Center"
  174.            Text="{Binding LabelText}"/>
  175. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  176.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  177.              x:Class="QuickCalc.App.MainPage">
  178.     <Label VerticalTextAlignment="Center"
  179.            HorizontalTextAlignment="Center"
  180.            Text="{Binding LabelText}"/>
  181. </ContentPage>fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  182.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  183.              x:Class="QuickCalc.App.MainPage">
  184.     <Label VerticalTextAlignment="Center"
  185.            HorizontalTextAlignment="Center"
  186.            Text="{Binding LabelText}"/>
  187. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  188.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  189.              x:Class="QuickCalc.App.MainPage">
  190.     <Label VerticalTextAlignment="Center"
  191.            HorizontalTextAlignment="Center"
  192.            Text="{Binding LabelText}"/>
  193. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  194.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  195.              x:Class="QuickCalc.App.MainPage">
  196.     <Label VerticalTextAlignment="Center"
  197.            HorizontalTextAlignment="Center"
  198.            Text="{Binding LabelText}"/>
  199. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  200.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  201.              x:Class="QuickCalc.App.MainPage">
  202.     <Label VerticalTextAlignment="Center"
  203.            HorizontalTextAlignment="Center"
  204.            Text="{Binding LabelText}"/>
  205. </ContentPage>});<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  206.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  207.              x:Class="QuickCalc.App.MainPage">
  208.     <Label VerticalTextAlignment="Center"
  209.            HorizontalTextAlignment="Center"
  210.            Text="{Binding LabelText}"/>
  211. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  212.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  213.              x:Class="QuickCalc.App.MainPage">
  214.     <Label VerticalTextAlignment="Center"
  215.            HorizontalTextAlignment="Center"
  216.            Text="{Binding LabelText}"/>
  217. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  218.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  219.              x:Class="QuickCalc.App.MainPage">
  220.     <Label VerticalTextAlignment="Center"
  221.            HorizontalTextAlignment="Center"
  222.            Text="{Binding LabelText}"/>
  223. </ContentPage>//服务注册<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  224.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  225.              x:Class="QuickCalc.App.MainPage">
  226.     <Label VerticalTextAlignment="Center"
  227.            HorizontalTextAlignment="Center"
  228.            Text="{Binding LabelText}"/>
  229. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  230.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  231.              x:Class="QuickCalc.App.MainPage">
  232.     <Label VerticalTextAlignment="Center"
  233.            HorizontalTextAlignment="Center"
  234.            Text="{Binding LabelText}"/>
  235. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  236.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  237.              x:Class="QuickCalc.App.MainPage">
  238.     <Label VerticalTextAlignment="Center"
  239.            HorizontalTextAlignment="Center"
  240.            Text="{Binding LabelText}"/>
  241. </ContentPage>builder.Services.AddSingleton();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  242.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  243.              x:Class="QuickCalc.App.MainPage">
  244.     <Label VerticalTextAlignment="Center"
  245.            HorizontalTextAlignment="Center"
  246.            Text="{Binding LabelText}"/>
  247. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  248.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  249.              x:Class="QuickCalc.App.MainPage">
  250.     <Label VerticalTextAlignment="Center"
  251.            HorizontalTextAlignment="Center"
  252.            Text="{Binding LabelText}"/>
  253. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  254.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  255.              x:Class="QuickCalc.App.MainPage">
  256.     <Label VerticalTextAlignment="Center"
  257.            HorizontalTextAlignment="Center"
  258.            Text="{Binding LabelText}"/>
  259. </ContentPage>builder.Services.AddSingleton();#if DEBUG<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  260.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  261.              x:Class="QuickCalc.App.MainPage">
  262.     <Label VerticalTextAlignment="Center"
  263.            HorizontalTextAlignment="Center"
  264.            Text="{Binding LabelText}"/>
  265. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  266.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  267.              x:Class="QuickCalc.App.MainPage">
  268.     <Label VerticalTextAlignment="Center"
  269.            HorizontalTextAlignment="Center"
  270.            Text="{Binding LabelText}"/>
  271. </ContentPage>builder.Logging.AddDebug();#endif<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  272.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  273.              x:Class="QuickCalc.App.MainPage">
  274.     <Label VerticalTextAlignment="Center"
  275.            HorizontalTextAlignment="Center"
  276.            Text="{Binding LabelText}"/>
  277. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  278.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  279.              x:Class="QuickCalc.App.MainPage">
  280.     <Label VerticalTextAlignment="Center"
  281.            HorizontalTextAlignment="Center"
  282.            Text="{Binding LabelText}"/>
  283. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  284.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  285.              x:Class="QuickCalc.App.MainPage">
  286.     <Label VerticalTextAlignment="Center"
  287.            HorizontalTextAlignment="Center"
  288.            Text="{Binding LabelText}"/>
  289. </ContentPage>return builder.Build();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  290.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  291.              x:Class="QuickCalc.App.MainPage">
  292.     <Label VerticalTextAlignment="Center"
  293.            HorizontalTextAlignment="Center"
  294.            Text="{Binding LabelText}"/>
  295. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  296.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  297.              x:Class="QuickCalc.App.MainPage">
  298.     <Label VerticalTextAlignment="Center"
  299.            HorizontalTextAlignment="Center"
  300.            Text="{Binding LabelText}"/>
  301. </ContentPage>}
复制代码
 
        增加的两句服务注册
  1. builder.Services.AddSingleton<MainPage>();
  2. builder.Services.AddSingleton<LabelViewModel>();
复制代码
 
第三步修改App.xaml.cs
  1.   public partial class App : Application  {<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              x:Class="QuickCalc.App.MainPage">
  4.     <Label VerticalTextAlignment="Center"
  5.            HorizontalTextAlignment="Center"
  6.            Text="{Binding LabelText}"/>
  7. </ContentPage>  public App(MainPage mainPage)<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  8.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  9.              x:Class="QuickCalc.App.MainPage">
  10.     <Label VerticalTextAlignment="Center"
  11.            HorizontalTextAlignment="Center"
  12.            Text="{Binding LabelText}"/>
  13. </ContentPage>  {<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  14.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  15.              x:Class="QuickCalc.App.MainPage">
  16.     <Label VerticalTextAlignment="Center"
  17.            HorizontalTextAlignment="Center"
  18.            Text="{Binding LabelText}"/>
  19. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  20.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  21.              x:Class="QuickCalc.App.MainPage">
  22.     <Label VerticalTextAlignment="Center"
  23.            HorizontalTextAlignment="Center"
  24.            Text="{Binding LabelText}"/>
  25. </ContentPage>  InitializeComponent();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  26.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  27.              x:Class="QuickCalc.App.MainPage">
  28.     <Label VerticalTextAlignment="Center"
  29.            HorizontalTextAlignment="Center"
  30.            Text="{Binding LabelText}"/>
  31. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  32.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  33.              x:Class="QuickCalc.App.MainPage">
  34.     <Label VerticalTextAlignment="Center"
  35.            HorizontalTextAlignment="Center"
  36.            Text="{Binding LabelText}"/>
  37. </ContentPage>  MainPage = mainPage;<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  38.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  39.              x:Class="QuickCalc.App.MainPage">
  40.     <Label VerticalTextAlignment="Center"
  41.            HorizontalTextAlignment="Center"
  42.            Text="{Binding LabelText}"/>
  43. </ContentPage>  }  }
复制代码
 
        增加了MainPage的构造函数注入
第四步修改MainPage.xaml.cs
  1. public partial class MainPage : ContentPage {<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              x:Class="QuickCalc.App.MainPage">
  4.     <Label VerticalTextAlignment="Center"
  5.            HorizontalTextAlignment="Center"
  6.            Text="{Binding LabelText}"/>
  7. </ContentPage> public MainPage(LabelViewModel labelViewModel)<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  8.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  9.              x:Class="QuickCalc.App.MainPage">
  10.     <Label VerticalTextAlignment="Center"
  11.            HorizontalTextAlignment="Center"
  12.            Text="{Binding LabelText}"/>
  13. </ContentPage> {<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  14.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  15.              x:Class="QuickCalc.App.MainPage">
  16.     <Label VerticalTextAlignment="Center"
  17.            HorizontalTextAlignment="Center"
  18.            Text="{Binding LabelText}"/>
  19. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  20.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  21.              x:Class="QuickCalc.App.MainPage">
  22.     <Label VerticalTextAlignment="Center"
  23.            HorizontalTextAlignment="Center"
  24.            Text="{Binding LabelText}"/>
  25. </ContentPage> InitializeComponent();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  26.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  27.              x:Class="QuickCalc.App.MainPage">
  28.     <Label VerticalTextAlignment="Center"
  29.            HorizontalTextAlignment="Center"
  30.            Text="{Binding LabelText}"/>
  31. </ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  32.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  33.              x:Class="QuickCalc.App.MainPage">
  34.     <Label VerticalTextAlignment="Center"
  35.            HorizontalTextAlignment="Center"
  36.            Text="{Binding LabelText}"/>
  37. </ContentPage> BindingContext = labelViewModel;<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  38.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  39.              x:Class="QuickCalc.App.MainPage">
  40.     <Label VerticalTextAlignment="Center"
  41.            HorizontalTextAlignment="Center"
  42.            Text="{Binding LabelText}"/>
  43. </ContentPage> } }
复制代码
 
        增加了LabelViewModel的构造函数注入以及BindingContext的赋值。
第五步运行程序

        至此,运行项目可以看到hello,World!已经在MAUI中继承了依赖​

 结论

        在MAUI上,依赖注入是一个非常有价值的技术,它可以帮助我们构建简洁、灵活和可测试的应用程序。通过合理地使用依赖注入,我们能够有效地管理和解耦组件之间的依赖关系,提高开发效率和代码质量。希望本文对您理解和应用MAUI上的依赖注入有所帮助!

来源:https://www.cnblogs.com/xuyd/archive/2023/06/30/17517620.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具