福冈咸鱼 发表于 2023-6-30 21:06:40

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

​引言

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

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

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

        首先创建好一个.NET MAUI项目之后,需要有以下前提条件
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage> 
namespace QuickCalc.App.ViewModels;public class LabelViewModel{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>public string LabelText { get; set; } = "Hello World";} 
        我们通过依赖注入将LabelText属性绑定到Label的Text上。
 

var builder = MauiApp.CreateBuilder();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>builder<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>.UseMauiApp()<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>.ConfigureFonts(fonts =><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>});#if DEBUG<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>builder.Logging.AddDebug();#endif<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>return builder.Build(); 
第一步安装Microsoft.Extensions.DependencyInjection

Install-Package Microsoft.Extensions.DependencyInjection 
第二步打开MauiProgram.cs

public static MauiApp CreateMauiApp()<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>var builder = MauiApp.CreateBuilder();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>builder<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>.UseMauiApp()<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>.ConfigureFonts(fonts =><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>});<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>//服务注册<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>builder.Services.AddSingleton();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>builder.Services.AddSingleton();#if DEBUG<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>builder.Logging.AddDebug();#endif<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>return builder.Build();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>} 
        增加的两句服务注册
builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton<LabelViewModel>(); 
第三步修改App.xaml.cs

public partial class App : Application{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>public App(MainPage mainPage)<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>InitializeComponent();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>MainPage = mainPage;<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage>}} 
        增加了MainPage的构造函数注入
第四步修改MainPage.xaml.cs

public partial class MainPage : ContentPage {<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage> public MainPage(LabelViewModel labelViewModel)<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage> {<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage> InitializeComponent();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage> BindingContext = labelViewModel;<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="QuickCalc.App.MainPage">

    <Label VerticalTextAlignment="Center"
         HorizontalTextAlignment="Center"
         Text="{Binding LabelText}"/>

</ContentPage> } } 
        增加了LabelViewModel的构造函数注入以及BindingContext的赋值。
第五步运行程序

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

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

来源:https://www.cnblogs.com/xuyd/archive/2023/06/30/17517620.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【C#/.NET】MAUI上的依赖注入