英文:
How to add IUserDialogs to Maui IOC Add Singleton for Acr.UserDialogs
问题
I'm converting app to Maui. Old IOS through TinyIOC/FreshMvvm was
FreshMvvm.FreshIOC.Container.Register<IUserDialogs>(UserDialogs.Instance);
so how should that look in MauiProgram.cs using built-in IOC?
builder.Services.AddSingleton<IRestService, RestService>();
builder.Services.AddSingleton<ICacheManager, CacheManager>();
Says Instance does not exist in the type UserDialogs.
builder.Services.AddSingleton<IUserDialogs, UserDialogs>();
Says Static types cannot be used as type Arguments.
英文:
I'm converting app to Maui. Old IOS through TinyIOC/FreshMvvm was
FreshMvvm.FreshIOC.Container.Register<IUserDialogs>(UserDialogs.Instance);
so how should that look in MauiProgram.cs using built in IOC?
builder.Services.AddSingleton<IRestService, RestService>();
builder.Services.AddSingleton<ICacheManager, CacheManager>();
builder.Services.AddSingleton<IUserDialogs,UserDialogs.Instance>();
Says Instance does not exist in the type UserDialogs.
and
builder.Services.AddSingleton<IUserDialogs,UserDialogs>();
Says Static types cannot be used as type Arguments.
答案1
得分: 2
Here is the translated content:
> 如何将 IUserDialogs 添加到 Maui IOC 中的 Acr.UserDialogs 的 Singleton
我尝试通过以下步骤来实现:
MauiProgram.cs:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(...)
.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(android => android.OnApplicationCreate(app => UserDialogs.Init(app)));
#endif
});
#if ANDROID
builder.Services.AddSingleton(UserDialogs.Instance);
#endif
return builder.Build();
}
}
App.xaml.cs:
public partial class App : Application
{
public static IUserDialogs UserDialogs;
public App(IUserDialogs userDialogs)
{
InitializeComponent();
UserDialogs = userDialogs;
MainPage = new AppShell();
}
}
Page.xaml.cs 用法:
public partial class NewPage1 : ContentPage
{
public NewPage1()
{
InitializeComponent();
App.UserDialogs.Alert("sss");
//UserDialogs.Instance.Alert("ss");
}
}
但我已经添加了代码 UserDialogs.Init(app)
,它仍然出现上述错误。我添加了断点并发现它总是首先执行这行代码:builder.Services.AddSingleton(UserDialogs.Instance);
而不是 UserDialogs.Init(app)
。
当然,如果不添加 Singleton,可以直接使用 UserDialogs:
public partial class NewPage1 : ContentPage
{
public NewPage1()
{
InitializeComponent();
//App.UserDialogs.Alert("sss");
UserDialogs.Instance.Alert("ss");
}
}
它可以正常工作,也会执行 UserDialogs.Init(app)
。
希望这些对您有帮助。
更新:
> 在您的 ContentPage 中是如何解析 UserDialogs 的?
您需要初始化 UserDialogs:way1(MauiProgram.cs) 或者 way2(MainActivity.cs):
protected override void OnCreate(Bundle savedInstanceState)
{
UserDialogs.Init(this);
base.OnCreate(savedInstanceState);
}
更新2:
我找到了解决方法!上面的 App.xaml.cs
和 Page.xaml.cs
代码保持不变。并更改 MauiProgram.cs:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
//在此处初始化 UserDialogs
#if ANDROID
UserDialogs.Init(()=>Platform.CurrentActivity);
#endif
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if ANDROID
builder.Services.AddSingleton(UserDialogs.Instance);
#endif
return builder.Build();
}
}
它可以正常工作,并且可以正常调用 builder.Services.AddSingleton(UserDialogs.Instance)
。
英文:
> How to add IUserDialogs to Maui IOC Add Singleton for Acr.UserDialogs
I tried to achieve it by following step:
MauiProgram.cs:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(...)
.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(android => android.OnApplicationCreate(app => UserDialogs.Init(app)));
#endif
});
#if ANDROID
builder.Services.AddSingleton(UserDialogs.Instance);
#endif
return builder.Build();
}
}
App.xaml.cs:
public partial class App : Application
{
public static IUserDialogs UserDialogs;
public App(IUserDialogs userDialogs)
{
InitializeComponent();
UserDialogs = userDialogs;
MainPage = new AppShell();
}
}
Page.xaml.cs to use it:
public partial class NewPage1 : ContentPage
{
public NewPage1()
{
InitializeComponent();
App.UserDialogs.Alert("sss");
//UserDialogs.Instance.Alert("ss");
}
}
But I have added the code UserDialogs.Init(app)
, it still has the error above. I added breakpoints and found that It always executes this line of code first: builder.Services.AddSingleton(UserDialogs.Instance);
instead of UserDialogs.Init(app)
.
Of course, if not add Singleton, use UserDialogs directly:
public partial class NewPage1 : ContentPage
{
public NewPage1()
{
InitializeComponent();
//App.UserDialogs.Alert("sss");
UserDialogs.Instance.Alert("ss");
}
}
It works well, and also will execute UserDialogs.Init(app)
.
I hope these can help you.
Update:
> How is your UserDialogs resolving in your ContentPage?
You need to initialize UserDialogs: way1(in MauiProgram.cs) or way2(in MainActivity.cs):
protected override void OnCreate(Bundle savedInstanceState)
{
UserDialogs.Init(this);
base.OnCreate(savedInstanceState);
}
Update2:
I found the way to solve it! The codes of App.xaml.cs
and Page.xaml.cs
above remain unchanged. And Change MauiProgram.cs:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
//initialize UserDialogs here
#if ANDROID
UserDialogs.Init(()=>Platform.CurrentActivity);
#endif
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if ANDROID
builder.Services.AddSingleton(UserDialogs.Instance);
#endif
return builder.Build();
}
}
It works well and can call builder.Services.AddSingleton(UserDialogs.Instance)
normally.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论