How to add IUserDialogs to Maui IOC Add Singleton for Acr.UserDialogs

huangapple go评论63阅读模式
英文:

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&lt;IUserDialogs&gt;(UserDialogs.Instance);

so how should that look in MauiProgram.cs using built in IOC?

    builder.Services.AddSingleton&lt;IRestService, RestService&gt;();
    builder.Services.AddSingleton&lt;ICacheManager, CacheManager&gt;();
    builder.Services.AddSingleton&lt;IUserDialogs,UserDialogs.Instance&gt;();

Says Instance does not exist in the type UserDialogs.
and

   builder.Services.AddSingleton&lt;IUserDialogs,UserDialogs&gt;();

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&lt;App&gt;()
            .ConfigureFonts(...)
            .ConfigureLifecycleEvents(events =&gt;
            {
#if ANDROID
                events.AddAndroid(android =&gt; android.OnApplicationCreate(app =&gt; 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(&quot;sss&quot;);
        
       //UserDialogs.Instance.Alert(&quot;ss&quot;);
    }
}

然而,出现了以下错误:How to add IUserDialogs to Maui IOC Add Singleton for Acr.UserDialogs

但我已经添加了代码 UserDialogs.Init(app),它仍然出现上述错误。我添加了断点并发现它总是首先执行这行代码:builder.Services.AddSingleton(UserDialogs.Instance); 而不是 UserDialogs.Init(app)

当然,如果不添加 Singleton,可以直接使用 UserDialogs:

public partial class NewPage1 : ContentPage
{
    public NewPage1()
    {
        InitializeComponent();
        //App.UserDialogs.Alert(&quot;sss&quot;);

        UserDialogs.Instance.Alert(&quot;ss&quot;);
    }
}

它可以正常工作,也会执行 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.csPage.xaml.cs 代码保持不变。并更改 MauiProgram.cs:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
//在此处初始化 UserDialogs 
#if ANDROID
            UserDialogs.Init(()=&gt;Platform.CurrentActivity);
#endif

        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp&lt;App&gt;()
            .ConfigureFonts(fonts =&gt;
            {
                fonts.AddFont(&quot;OpenSans-Regular.ttf&quot;, &quot;OpenSansRegular&quot;);
                fonts.AddFont(&quot;OpenSans-Semibold.ttf&quot;, &quot;OpenSansSemibold&quot;);
            });

#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&lt;App&gt;()
            .ConfigureFonts(...)
            .ConfigureLifecycleEvents(events =&gt;
            {
#if ANDROID
                events.AddAndroid(android =&gt; android.OnApplicationCreate(app =&gt; 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(&quot;sss&quot;);
        
       //UserDialogs.Instance.Alert(&quot;ss&quot;);
    }
}

However, this error occurs:How to add IUserDialogs to Maui IOC Add Singleton for Acr.UserDialogs

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(&quot;sss&quot;);

        UserDialogs.Instance.Alert(&quot;ss&quot;);
    }
}

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(()=&gt;Platform.CurrentActivity);
#endif

        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp&lt;App&gt;()
            .ConfigureFonts(fonts =&gt;
            {
                fonts.AddFont(&quot;OpenSans-Regular.ttf&quot;, &quot;OpenSansRegular&quot;);
                fonts.AddFont(&quot;OpenSans-Semibold.ttf&quot;, &quot;OpenSansSemibold&quot;);
            });

#if ANDROID
            builder.Services.AddSingleton(UserDialogs.Instance);
#endif

        return builder.Build();
    }
}

It works well and can call builder.Services.AddSingleton(UserDialogs.Instance) normally.

huangapple
  • 本文由 发表于 2023年5月18日 01:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定