-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added embedding extensions #5218
Conversation
Maui EmbeddingSo you have a Net 6.0 App (iOS MacOS, Android, or Windows) , and you want to add a few Maui Screens. No Problem! SetupFirst you need to add UseMaui to the CSProj <PropertyGroup>
<UseMaui>true</UseMaui>
</PropertyGroup>
iOS/MacFirst create a MauiContext. This is best done in your AppDelegate public static MauiContext MauiContext;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
///Your normal iOS registration
//Setup MauiBits
var builder = MauiApp.CreateBuilder();
//Add Maui Controls
builder.UseMauiEmbedded();
//Using comet?
//builder.UseComet();
//iOS/Mac need to register the Window
builder.Services.Add( new Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(UIKit.UIWindow),window));
var mauiApp = builder.Build();
//Create and save a Maui Context. This is needed for creating Platform UI
mauiContext = new MauiContext(mauiApp.Services);
return true;
} Now you can create and use a MauiView! //Create a Maui Page
var myMauiPage = new MyMauiPage();
//Turn the Maui page into an Android View
UIView view = myMauiPage.ToPlatform(mauiContext); Androidusing Android.App;
using Android.OS;
using Microsoft.Maui.Platform;
using Microsoft.Maui.Embedding;
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
MauiContext mauiContext;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Setup MauiBits
var builder = MauiApp.CreateBuilder();
//Add Maui Controls
builder.UseMauiEmbedded();
//Using comet?
//builder.UseComet();
var mauiApp = builder.Build();
//Create and save a Maui Context. This is needed for creating Platform UI
mauiContext = new MauiContext(mauiApp.Services,this);
//Create a Maui Page
var myMauiPage = new MyMauiPage();
//Turn the Maui page into an Android View
var view = myMauiPage.ToPlatform(mauiContext);
//Use the Android View
SetContentView(view);
}
} |
Maybe you can make use of the extensions in
For example, it could be possible to make a new extension method for // this can be stored in a static location
var mauiEmbeddedApp = MauiApp
.CreateBuilder()
.UseMauiEmbedded()
.Build()
.ToEmbedded(); // new method
// this runs on each window or activity
var platformWindow = ... // Window on iOS, this on Android, this on WinUI
var mauiContext = mauiEmbeddedApp.CreateWindowContext(platformWindow);
// back to normal things
var page = new ContentPage {
new Label { Text = "w00t!" }
};
var platformView = page.ToPlatform(mauiContext); The new extension method just returns a wrapped static class EmbeddedExtensions {
public static MauiEmbeddedApp ToEmbedded(this MauiApp mauiApp) {
var platformApp = UIApplication.SharedApplication;
var platformApp = Android.App.Application.Context as Android.App.Application;
var platformApp = UI.Xaml.Application.Current;
// create scopes to make sure the platform app is added (ie: UIAppDelegate is in the services)
var mauiContext = new MauiContext(mauiApp.Services, platformApp);
var appContext = mauiContext.MakeAppScope(platformApp);
var embedded = new MauiEmbeddedApp(mauiApp, appContext);
return embedded;
}
public static MauiContext CreateWindowContext(this MauiEmbeddedApp mauiApp, PlatformWindow platformWindow) {
// create scopes to make sure the platform window is added (ie: UIWindow is in the services)
var windowContext = mauiApp.MauiApplicationContext.MakeWindowScope(platformWindow);
return windowContext;
}
}
class MauiEmbeddedApp : IDisposable {
internal MauiEmbeddedApp(MauiApp mauiApp, MauiContext appContext);
public MauiApp { get; }
public MauiContext MauiApplicationContext { get; }
public IServiceProvider Services => MauiApp.Services;
public IConfiguration Configuration => MauiApp.Configuration;
} |
Moved @mattleibow 's comments to the original issue for tracking in future work. |
I tried adding UseMaui to a WinUI project and got the following errors:
It looks like both Maui and WinUI are source generating from the xaml files. Is there a way to disable the Maui xaml source generator? |
@sparkie108 could you open a new issue with you problem? I think there may be some extra steps, but I think the discussion is going to be lost here. |
I have created a WinUI example that now works here: https://github.com/sparkie108/MauiWinUIEmbedding |
Is there (or will there be) a possibility to embed MAUI to UWP application? |
Description of Change
This adds a new Api that will allow us to control registration for Maui Native embedding.
Issues Fixed
Fixes (partially) #1718
Usage:
Maui Embedding.md