注册
|
登录
发帖
热搜
活动
交友
discuz
论坛
BBS
翼度工具
翼度网址导航
开发工具
Linux命令速查
网页设计配色表
在线制作icon
颜色代码选取器
翼度科技
»
论坛
›
编程开发
›
.net
›
查看内容
返回列表
发新帖
【C#/.NET】MAUI上的依赖注入
好的一塌糊涂
好的一塌糊涂
当前离线
积分
15
5
主题
5
帖子
15
积分
新手上路
新手上路, 积分 15, 距离下一级还需 35 积分
新手上路, 积分 15, 距离下一级还需 35 积分
积分
15
发消息
显示全部楼层
引言
在移动应用开发中,依赖注入是一项非常重要的技术,它可以帮助我们简化代码结构、提高可维护性并增加测试覆盖率。在最新的.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】 我们会及时删除侵权内容,谢谢合作!
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
上一篇:
【C#/.NET】MAUI上的依赖注入
下一篇:
盘点C#中感叹号"!"的作用
发表于 2023-6-30 21:11:03
举报
回复
使用道具
分享
返回列表
发新帖
本版积分规则
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
快速回复
快速回复
返回顶部
返回顶部
返回列表
返回列表