Support Dependency Injection?

1 Answer 284 Views
BusyIndicator
Dmitry
Top achievements
Rank 1
Iron
Dmitry asked on 04 Feb 2022, 06:14 AM | edited on 04 Feb 2022, 09:38 AM

I create new MAUI project in VS 2012 Preview 5 (MAUI Preview 12).
Add Telerik MAUI 0.4 package.
Add to MainPage BusyIndicator control.
App runing and work properly.
I try add DI support. Modify app and run. Then got error.
When i comment BusyIndicator control in MainPage.xaml then app work properly.
Sample project and error attached.

Some Telerik MAUI controls work properly (e.g. RadBorder, RadButton, RadListView)

1 Answer, 1 is accepted

Sort by
0
Petar Marchev
Telerik team
answered on 04 Feb 2022, 12:17 PM

Hi Dmitry,

Thank you for the attached image and project. I was able to run it right away and reproduce the exception.

In short, I suggest you remove the line in the CreateMauiApp() method where the MainPage class is added as a Singleton. You will also need to change the signature of the App constructor and set the MainPage property to a new MainPage().

I did inspect the setup closely and I also debugged the project. The reason that there is an exception is that the RadBusyIndicator tries to access the current application and get some information from it. This is done via Application.Current (the Application is a class in Microsoft.Maui.Controls).

Interestingly, when I debugged it, the current application was null, and this is what is causing the exception. You can check this in the constructor of the MainPage for example.

I think it is strange that the framework tries to create a Page before the Application is ready. The Page is being created too early. The reason that the framework creates this Page too early is this line in MauiProgram.cs:

builder.Services.AddSingleton<MainPage>();

 

This essentially instructs the framework to create a single MainPage instance and reuse it (inject it) where needed.

Ok, so it turns out that for the Application to start, it needs to have the MainPage created, but the MainPage may need an Application for proper setup.

I can see that if I do not have a RadBusyIndicator in the page, the page shows correctly. However, this is only a lucky coincidence, because the setup is pretty simple here. There can be other visual elements that may need to use the Application.Current property. For example, if you try to use the AppThemeBinding - the same exception is thrown:

 

<Label Text="Hello, World!"
             TextColor="{AppThemeBinding Light=Red, Dark=Green}"
             Grid.Row="0"
             SemanticProperties.HeadingLevel="Level1"
             FontSize="32"
             HorizontalOptions="Center" />

I don't think that the AddSingleton() method was intended for visual elements. This is why I suggest you don't use it for the MainPage. Perhaps there is a reason you chose this approach, and if so, please share more details about the requirements and the exact scenario.

Let us know how it goes.

Regards,
Petar Marchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
BusyIndicator
Asked by
Dmitry
Top achievements
Rank 1
Iron
Answers by
Petar Marchev
Telerik team
Share this question
or