.NET MAUI ContentView中如何解决依赖注入问题?
MAUI中ContentView访问DI单例服务问题
我试过某方法但无效,GetService返回null。
声明:这是最小可复现示例(MRE),请勿建议不使用服务的解决方案。
我需要在ContentView中访问已添加到依赖注入容器的单例服务,希望LuckyNumberText通过该服务生成随机数。但ContentView的构造函数必须无参,无法直接注入依赖。
以下是完整的最小可复现代码:
App.xaml
<?xml version = "1.0" encoding = "UTF-8" ?> <Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MauiMRE" x:Class="MauiMRE.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Styles/Colors.xaml" /> <ResourceDictionary Source="Resources/Styles/Styles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
App.xaml.cs
namespace MauiMRE; public partial class App : Application { public static IServiceProvider Services => ServiceProvider.Current; public App() { InitializeComponent(); MainPage = new AppShell(); } }
AppShell.xaml
<?xml version="1.0" encoding="UTF-8" ?> <Shell x:Class="MauiMRE.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MauiMRE" Shell.FlyoutBehavior="Disabled"> <ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> </Shell>
AppShell.xaml.cs
namespace MauiMRE; public partial class AppShell : Shell { public AppShell() { InitializeComponent(); } }
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiMRE.MainPage" xmlns:local="clr-namespace:MauiMRE" > <local:MyContentView> </local:MyContentView> </ContentPage>
MainPage.xaml.cs
namespace MauiMRE; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } }
MauiProgram.cs
using Microsoft.Extensions.Logging; namespace MauiMRE; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); #if DEBUG builder.Logging.AddDebug(); #endif builder.Services.AddSingleton<MyContentView>(); builder.Services.AddSingleton<MyContentViewModel>(); builder.Services.AddSingleton<MyService>(); return builder.Build(); } }
MyContentView.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiMRE.MyContentView"> <VerticalStackLayout> <Label Text="{Binding LuckyNumberText}" VerticalOptions="Center" HorizontalOptions="Center" /> </VerticalStackLayout> </ContentView>
MyContentView.xaml.cs
namespace MauiMRE; public partial class MyContentView : ContentView { public MyContentView() { InitializeComponent(); BindingContext = new MyContentViewModel(); } }
MyContentViewModel.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MauiMRE; public class MyContentViewModel { public MyContentViewModel() { var service = ServiceProvider.GetService<MyService>(); var result = service.RandomInt(); // 执行到这里前就崩溃了 } public string LuckyNumberText { get { return $"Lucky number: {1}"; } } }
MyService.cs
namespace MauiMRE { public static class ServiceProvider { public static T GetService<T>() { var current = Current; // 能执行 return current.GetService<T>(); // 这里崩溃 } public static IServiceProvider Current => #if WINDOWS10_0_17763_0_OR_GREATER MauiWinUIApplication.Current.Services; #elif ANDROID MauiApplication.Current.Services; // 应该是这里崩溃 #else null; #endif } }
内容的提问来源于stack exchange,提问作者infinitezero
相关产品推荐
相关产品推荐

