-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f63470
commit 81b3744
Showing
104 changed files
with
2,446 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
8.0/PlatformIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:label="@string/app_name" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"> | ||
</application> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
65 changes: 65 additions & 0 deletions
65
8.0/PlatformIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/MainActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using static Android.Views.ViewGroup.LayoutParams; | ||
|
||
namespace NativeEmbeddingDemo.Droid | ||
{ | ||
[Activity(Label = "@string/app_name", MainLauncher = true, Theme = "@style/AppTheme")] | ||
public class MainActivity : Activity | ||
{ | ||
public static readonly Lazy<MauiApp> MauiApp = new(() => | ||
{ | ||
var mauiApp = MauiProgram.CreateMauiApp(builder => | ||
{ | ||
builder.UseMauiEmbedding(); | ||
}); | ||
return mauiApp; | ||
}); | ||
|
||
public static bool UseWindowContext = true; | ||
|
||
MyMauiContent? mauiView; | ||
|
||
protected override void OnCreate(Bundle? savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
|
||
// Set the view from the "main" layout resource | ||
SetContentView(Resource.Layout.activity_main); | ||
|
||
var rootLayout = FindViewById<LinearLayout>(Resource.Id.rootLayout)!; | ||
|
||
// Create Android button | ||
var androidButton = new Android.Widget.Button(this); | ||
androidButton.Text = "Android button above .NET MAUI controls"; | ||
androidButton.Click += OnAndroidButtonClicked; | ||
rootLayout.AddView(androidButton, new LinearLayout.LayoutParams(MatchParent, WrapContent)); | ||
|
||
// Ensure .NET MAUI app is built before creating .NET MAUI views | ||
var mauiApp = MainActivity.MauiApp.Value; | ||
|
||
// Create .NET MAUI context | ||
var mauiContext = UseWindowContext | ||
? mauiApp.CreateEmbeddedWindowContext(this) // Create window context | ||
: new MauiContext(mauiApp.Services, this); // Create app context | ||
|
||
// Create .NET MAUI content | ||
mauiView = new MyMauiContent(); | ||
|
||
// Create native view | ||
var nativeView = mauiView.ToPlatformEmbedded(mauiContext); | ||
|
||
// Add native view to layout | ||
rootLayout.AddView(nativeView, new LinearLayout.LayoutParams(MatchParent, WrapContent)); | ||
} | ||
|
||
async void OnAndroidButtonClicked(object? sender, EventArgs e) | ||
{ | ||
if (mauiView?.DotNetBot is not Image bot) | ||
return; | ||
|
||
await bot.RotateTo(360, 1000); | ||
bot.Rotation = 0; | ||
|
||
bot.HeightRequest = 90; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/NativeEmbeddingDemo.Droid.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0-android</TargetFramework> | ||
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion> | ||
<OutputType>Exe</OutputType> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
|
||
<UseMaui>true</UseMaui> | ||
<MauiEnablePlatformUsings>true</MauiEnablePlatformUsings> | ||
|
||
<ApplicationId>com.companyname.nativeembeddingdemo</ApplicationId> | ||
<ApplicationVersion>1</ApplicationVersion> | ||
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> | ||
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NativeEmbeddingDemo.Library\NativeEmbeddingDemo.Library.csproj" /> | ||
<ProjectReference Include="..\NativeEmbeddingDemo\NativeEmbeddingDemo.csproj" /> | ||
</ItemGroup> | ||
|
||
<ProjectExtensions><VisualStudio><UserProperties XamarinHotReloadDebuggerTimeoutExceptionNativeEmbeddingDemoDroidHideInfoBar="True" /></VisualStudio></ProjectExtensions> | ||
|
||
</Project> |
44 changes: 44 additions & 0 deletions
44
...ormIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/AboutResources.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Images, layout descriptions, binary blobs and string dictionaries can be included | ||
in your application as resource files. Various Android APIs are designed to | ||
operate on the resource IDs instead of dealing with images, strings or binary blobs | ||
directly. | ||
|
||
For example, a sample Android app that contains a user interface layout (main.xml), | ||
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png) | ||
would keep its resources in the "Resources" directory of the application: | ||
|
||
Resources/ | ||
drawable/ | ||
icon.png | ||
|
||
layout/ | ||
main.xml | ||
|
||
values/ | ||
strings.xml | ||
|
||
In order to get the build system to recognize Android resources, set the build action to | ||
"AndroidResource". The native Android APIs do not operate directly with filenames, but | ||
instead operate on resource IDs. When you compile an Android application that uses resources, | ||
the build system will package the resources for distribution and generate a class called "Resource" | ||
(this is an Android convention) that contains the tokens for each one of the resources | ||
included. For example, for the above Resources layout, this is what the Resource class would expose: | ||
|
||
public class Resource { | ||
public class Drawable { | ||
public const int icon = 0x123; | ||
} | ||
|
||
public class Layout { | ||
public const int main = 0x456; | ||
} | ||
|
||
public class Strings { | ||
public const int first_string = 0xabc; | ||
public const int second_string = 0xbcd; | ||
} | ||
} | ||
|
||
You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or | ||
Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string | ||
to reference the first string in the dictionary file values/strings.xml. |
10 changes: 10 additions & 0 deletions
10
...egration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/layout/activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:id="@+id/rootLayout" | ||
android:padding="8dp"> | ||
</LinearLayout> |
4 changes: 4 additions & 0 deletions
4
...ion/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-anydpi-v26/appicon.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<background android:drawable="@mipmap/appicon_background" /> | ||
<foreground android:drawable="@mipmap/appicon_foreground" /> | ||
</adaptive-icon> |
4 changes: 4 additions & 0 deletions
4
...tiveEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-anydpi-v26/appicon_round.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<background android:drawable="@mipmap/appicon_background" /> | ||
<foreground android:drawable="@mipmap/appicon_foreground" /> | ||
</adaptive-icon> |
Binary file added
BIN
+2.13 KB
...NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-hdpi/appicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+97 Bytes
...dingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-hdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.25 KB
...dingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-hdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.49 KB
...NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-mdpi/appicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+92 Bytes
...dingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-mdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.24 KB
...dingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-mdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.03 KB
...ativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xhdpi/appicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+100 Bytes
...ingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xhdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.76 KB
...ingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xhdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.56 KB
...tiveEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xxhdpi/appicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+108 Bytes
...ngDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xxhdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.88 KB
...ngDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xxhdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.67 KB
...iveEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xxxhdpi/appicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+117 Bytes
...gDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xxxhdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.74 KB
...gDemo/NativeEmbeddingDemo.Droid/Resources/mipmap-xxxhdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
...formIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/values/colors.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<resources> | ||
<color name="primaryColor">#3F51B5</color> | ||
<color name="primaryDarkColor">#303F9F</color> | ||
<color name="accentColor">#FF4081</color> | ||
</resources> |
4 changes: 4 additions & 0 deletions
4
...NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/values/ic_launcher_background.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="ic_launcher_background">#2C3E50</color> | ||
</resources> |
4 changes: 4 additions & 0 deletions
4
...ormIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/values/strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<resources> | ||
<string name="app_name">NativeEmbeddingDemo.Droid</string> | ||
<string name="app_text">Hello, Android!</string> | ||
</resources> |
7 changes: 7 additions & 0 deletions
7
...formIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Droid/Resources/values/styles.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<resources> | ||
<style name="AppTheme" parent="Theme.MaterialComponents.Light"> | ||
<item name="colorPrimary">@color/primaryColor</item> | ||
<item name="colorPrimaryDark">@color/primaryDarkColor</item> | ||
<item name="colorAccent">@color/accentColor</item> | ||
</style> | ||
</resources> |
80 changes: 80 additions & 0 deletions
80
...PlatformIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Library/EmbeddedExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Maui.Platform; | ||
|
||
#if ANDROID | ||
using PlatformView = Android.Views.View; | ||
using PlatformWindow = Android.App.Activity; | ||
using PlatformApplication = Android.App.Application; | ||
#elif IOS || MACCATALYST | ||
using PlatformView = UIKit.UIView; | ||
using PlatformWindow = UIKit.UIWindow; | ||
using PlatformApplication = UIKit.IUIApplicationDelegate; | ||
#elif WINDOWS | ||
using PlatformView = Microsoft.UI.Xaml.FrameworkElement; | ||
using PlatformWindow = Microsoft.UI.Xaml.Window; | ||
using PlatformApplication = Microsoft.UI.Xaml.Application; | ||
#endif | ||
|
||
namespace Microsoft.Maui.Controls; | ||
|
||
public static class EmbeddedExtensions | ||
{ | ||
public static MauiAppBuilder UseMauiEmbedding(this MauiAppBuilder builder, PlatformApplication? platformApplication = null) | ||
{ | ||
#if ANDROID | ||
platformApplication ??= (Android.App.Application)Android.App.Application.Context; | ||
#elif IOS || MACCATALYST | ||
platformApplication ??= UIKit.UIApplication.SharedApplication.Delegate; | ||
#elif WINDOWS | ||
platformApplication ??= Microsoft.UI.Xaml.Application.Current; | ||
#endif | ||
|
||
builder.Services.AddSingleton(platformApplication); | ||
builder.Services.AddSingleton<EmbeddedPlatformApplication>(); | ||
builder.Services.AddScoped<EmbeddedWindowProvider>(); | ||
|
||
// Returning null is acceptable here as the platform window is optional - but we don't know until we resolve it | ||
builder.Services.AddScoped<PlatformWindow>(svc => svc.GetRequiredService<EmbeddedWindowProvider>().PlatformWindow!); | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IMauiInitializeService, EmbeddedInitializeService>()); | ||
builder.ConfigureMauiHandlers(handlers => | ||
{ | ||
handlers.AddHandler(typeof(Window), typeof(EmbeddedWindowHandler)); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
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; | ||
} | ||
|
||
public static PlatformView ToPlatformEmbedded(this IElement element, IMauiContext context) | ||
{ | ||
var wndProvider = context.Services.GetService<EmbeddedWindowProvider>(); | ||
if (wndProvider is not null && wndProvider.Window is Window wnd && element is VisualElement visual) | ||
wnd.AddLogicalChild(visual); | ||
|
||
return element.ToPlatform(context); | ||
} | ||
|
||
private class EmbeddedInitializeService : IMauiInitializeService | ||
{ | ||
public void Initialize(IServiceProvider services) => | ||
services.GetRequiredService<EmbeddedPlatformApplication>(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Library/EmbeddedPlatformApplication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#if ANDROID | ||
using PlatformApplication = Android.App.Application; | ||
#elif IOS || MACCATALYST | ||
using PlatformApplication = UIKit.IUIApplicationDelegate; | ||
#elif WINDOWS | ||
using PlatformApplication = Microsoft.UI.Xaml.Application; | ||
#endif | ||
|
||
namespace Microsoft.Maui.Controls; | ||
|
||
internal class EmbeddedPlatformApplication : IPlatformApplication | ||
{ | ||
private readonly MauiContext rootContext; | ||
private readonly IMauiContext applicationContext; | ||
|
||
public IServiceProvider Services { get; } | ||
public IApplication Application { get; } | ||
|
||
public EmbeddedPlatformApplication(IServiceProvider services) | ||
{ | ||
IPlatformApplication.Current = this; | ||
|
||
#if ANDROID | ||
var platformApp = services.GetRequiredService<PlatformApplication>(); | ||
rootContext = new MauiContext(services, platformApp); | ||
#else | ||
rootContext = new MauiContext(services); | ||
#endif | ||
|
||
applicationContext = MakeApplicationScope(rootContext); | ||
Services = applicationContext.Services; | ||
Application = Services.GetRequiredService<IApplication>(); | ||
} | ||
|
||
private static IMauiContext MakeApplicationScope(IMauiContext rootContext) | ||
{ | ||
var scopedContext = new MauiContext(rootContext.Services); | ||
InitializeScopedServices(scopedContext); | ||
return scopedContext; | ||
} | ||
|
||
private static void InitializeScopedServices(IMauiContext scopedContext) | ||
{ | ||
var scopedServices = scopedContext.Services.GetServices<IMauiInitializeScopedService>(); | ||
|
||
foreach (var service in scopedServices) | ||
service.Initialize(scopedContext.Services); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...tformIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Library/EmbeddedWindowHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.Maui.Handlers; | ||
|
||
#if ANDROID | ||
using PlatformWindow = Android.App.Activity; | ||
#elif IOS || MACCATALYST | ||
using PlatformWindow = UIKit.UIWindow; | ||
#elif WINDOWS | ||
using PlatformWindow = Microsoft.UI.Xaml.Window; | ||
#endif | ||
|
||
namespace Microsoft.Maui.Controls; | ||
|
||
internal class EmbeddedWindowHandler : ElementHandler<IWindow, PlatformWindow>, IWindowHandler | ||
{ | ||
public static IPropertyMapper<IWindow, IWindowHandler> Mapper = | ||
new PropertyMapper<IWindow, IWindowHandler>(ElementHandler.ElementMapper) | ||
{ | ||
}; | ||
|
||
public static CommandMapper<IWindow, IWindowHandler> CommandMapper = | ||
new CommandMapper<IWindow, IWindowHandler>(ElementHandler.ElementCommandMapper) | ||
{ | ||
}; | ||
|
||
public EmbeddedWindowHandler() : base(Mapper) | ||
{ | ||
} | ||
|
||
protected override PlatformWindow CreatePlatformElement() => | ||
MauiContext!.Services.GetRequiredService<PlatformWindow>() ?? | ||
throw new InvalidOperationException("EmbeddedWindowHandler could not locate a platform window."); | ||
} |
27 changes: 27 additions & 0 deletions
27
...formIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.Library/EmbeddedWindowProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if ANDROID | ||
using PlatformWindow = Android.App.Activity; | ||
#elif IOS || MACCATALYST | ||
using PlatformWindow = UIKit.UIWindow; | ||
#elif WINDOWS | ||
using PlatformWindow = Microsoft.UI.Xaml.Window; | ||
#endif | ||
|
||
namespace Microsoft.Maui.Controls; | ||
|
||
public class EmbeddedWindowProvider | ||
{ | ||
WeakReference<PlatformWindow?>? platformWindow; | ||
WeakReference<Window?>? window; | ||
|
||
public PlatformWindow? PlatformWindow => Get(platformWindow); | ||
public Window? Window => Get(window); | ||
|
||
public void SetWindow(PlatformWindow? platformWindow, Window? window) | ||
{ | ||
this.platformWindow = new WeakReference<PlatformWindow?>(platformWindow); | ||
this.window = new WeakReference<Window?>(window); | ||
} | ||
|
||
private static T? Get<T>(WeakReference<T?>? weak) where T : class => | ||
weak is not null && weak.TryGetTarget(out var target) ? target : null; | ||
} |
Oops, something went wrong.