Jesoo
Jesoo

Reputation: 84

.NET MAUI BindableObject throws an InvalidOperationException when try to instantiate it

I'm currently trying to embed some maui views into a legacy android native application and I found out a way to accomplish that using MSDN native embedding guide;

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/native-embedding?view=net-maui-8.0&pivots=devices-android#create-a-net-maui-single-project

Anyway, I crossed a very strange exception when I try to instantiate a new MauiContext in order to use it later to embed some maui view into native android windows; basically when MauiContext object is instantiated, visual studio stops execution and throws this exception:

exception screenshot

exception call stack

The exception itself is pretty clear, but I can't figure it out what is the Bindable object who crashes the execution and most important, I don't know how to fix it because the whole call stack is made by external code.

This is my codebase:

private static Lazy<MauiApp> _mauiApp;
private static IMauiContext _context;

public static bool UseWindowContext = true;

public static void SetupManager(Activity activity)
{
  _mauiApp = new(() =>
  {
    var mauiApp = MauiProgram.CreateMauiApp(builder =>
    {
      builder.UseMauiEmbedding();
    });
    return mauiApp;
  });

// this ternary operator is the same as in the MSDN example, but even if a comment CreateEmbeddedWindowContext method and try to instantiate just the MauiContext itself, it throws the same exception
  _context = UseWindowContext
    ? _mauiApp.Value.CreateEmbeddedWindowContext(activity)
    : new MauiContext(_mauiApp.Value.Services, activity);  
}
// here is CreateEmbeddedWindowContext extension method, this is also taken as it is from MSDN example  
public static IMauiContext CreateEmbeddedWindowContext(this MauiApp mauiApp, PlatformWindow platformWindow, Window? window = null)
  {
    var windowScope = mauiApp.Services.CreateScope();

#if ANDROID
    var windowContext = new MauiContext(windowScope.ServiceProvider, platformWindow);
#else
        var windowContext = new MauiContext(windowScope.ServiceProvider);
#endif

    window ??= new Window();

    var wndProvider = windowContext.Services.GetRequiredService<EmbeddedWindowProvider>();
    wndProvider.SetWindow(platformWindow, window);
    window.ToHandler(windowContext);

    return windowContext;
  }

I'm stuck on this issue since a couple of days ago and I wrapped my head more and more but I can't get out, so thank you very much in advance to anyone could help me to solve it.

Upvotes: 1

Views: 43

Answers (0)

Related Questions