Skip to content

Commit

Permalink
Merge pull request #2255 from unoplatform/mergify/bp/release/stable/4…
Browse files Browse the repository at this point in the history
….1/pr-2246

chore: Fixing reference to UIWindow (backport #2246)
  • Loading branch information
dansiegel authored Apr 17, 2024
2 parents 71b3bcc + 250c0b4 commit 4162050
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 61 deletions.
2 changes: 2 additions & 0 deletions samples/MauiEmbedding/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Project>
<Import Project="../../src/maui-embedding.props" />

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
27 changes: 13 additions & 14 deletions samples/MauiEmbedding/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
<Project ToolsVersion="15.0">
<ItemGroup>
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.5.240311000" />
<PackageVersion Include="SkiaSharp" Version="2.88.7" />
<PackageVersion Include="SkiaSharp.Views.Uno.WinUI" Version="2.88.7" />
<PackageVersion Include="SkiaSharp.NativeAssets.iOS" Version="2.88.7" />
<PackageVersion Include="SkiaSharp.Skottie" Version="2.88.7" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="$(WinAppSdkVersion)" />
<PackageVersion Include="SkiaSharp" Version="2.88.8" />
<PackageVersion Include="SkiaSharp.Views.Uno.WinUI" Version="2.88.8" />
<PackageVersion Include="SkiaSharp.NativeAssets.iOS" Version="2.88.8" />
<PackageVersion Include="SkiaSharp.Skottie" Version="2.88.8" />
<PackageVersion Include="Uno.Core.Extensions.Logging.Singleton" Version="4.0.1" />
<PackageVersion Include="Uno.Extensions.Logging.OSLog" Version="1.7.0" />
<PackageVersion Include="Uno.Material.WinUI" Version="4.0.4" />
<PackageVersion Include="Uno.Dsp.Tasks" Version="1.2.7" />
<PackageVersion Include="Uno.Toolkit.WinUI" Version="5.0.15" />
<PackageVersion Include="Uno.Toolkit.WinUI.Material" Version="5.0.15" />
<PackageVersion Include="Uno.Resizetizer" Version="1.2.0" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.0.19" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.UniversalImageLoader" Version="1.9.36" />
<PackageVersion Include="Uno.WinUI" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.Skia.Gtk" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.RemoteControl" Version="5.0.19" />
<PackageVersion Include="CommunityToolkit.Maui" Version="2.0.0" />
<PackageVersion Include="Uno.WinUI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Skia.Gtk" Version="" />
<PackageVersion Include="Uno.WinUI.RemoteControl" Version="$(UnoVersion)" />
<PackageVersion Include="Telerik.UI.for.Maui" Version="5.1.0" Condition="$(_UseTelerik)" />
<PackageVersion Include="Esri.ArcGISRuntime.Maui" Version="200.1.0" />
<PackageVersion Include="Microsoft.Windows.Compatibility" Version="7.0.4" />
<PackageVersion Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.7.0" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="5.0.19" />
<PackageVersion Include="Uno.Wasm.Bootstrap" Version="8.0.7" />
<PackageVersion Include="Uno.Wasm.Bootstrap.DevServer" Version="8.0.7" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.Wasm.Bootstrap" Version="8.0.13" />
<PackageVersion Include="Uno.Wasm.Bootstrap.DevServer" Version="8.0.13" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static void Init()
return;
}

#if IOS
#if IOS || MACCATALYST
if (h.PlatformView is UIKit.UITextField textField)
{
var color = entry.BorderColor?.ToPlatform().CGColor ?? UIKit.UIColor.Black.CGColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<!-- This is only required because this project references Uno.Extensions.Maui.WinUI as a package reference (ie by source) -->
<Import Project="..\..\..\src\Uno.Extensions.Maui.UI\build\Package.props" />

Expand Down Expand Up @@ -37,7 +37,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Uno.Extensions.Logging.OSLog" />
<PackageReference Include="Telerik.UI.for.Maui" Condition="$(_UseTelerik)"/>
<PackageReference Include="CommunityToolkit.Maui" />
<PackageReference Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" />
<!--<PackageReference Include="Uno.WinUI.RemoteControl" Condition="'$(Configuration)'=='Debug'" />-->
</ItemGroup>
Expand Down
29 changes: 20 additions & 9 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<Project ToolsVersion="15.0">
<!--
Due to an issue with duplicate references on output for Windows we need to ensure that when building for MAUI Embedding that we use the same version
of both WinUI and Uno.WinUI across Extensions and the MauiEmbedding Sample Project.
-->
<PropertyGroup Condition="$(SolutionName) != 'MauiEmbedding'">
<UnoVersion Condition="$(UnoVersion) == '' AND !$(MSBuildProjectName.Contains('Maui'))">5.0.19</UnoVersion>
<UnoVersion Condition="$(UnoVersion) == '' AND $(MSBuildProjectName.Contains('Maui'))">5.2.22</UnoVersion>
<WinAppSdkVersion Condition="$(WinAppSdkVersion) == ''">1.4.231219000</WinAppSdkVersion>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)maui-embedding.props" Condition="$(SolutionName) == 'MauiEmbedding'" />
<ItemGroup>
<PackageVersion Include="CommunityToolkit.Mvvm" Version="7.0.1" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.1.1" />
Expand All @@ -14,7 +25,7 @@
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.60.3" />
<PackageVersion Include="Microsoft.Identity.Client.Extensions.Msal" Version="2.22.0" />
<PackageVersion Include="Microsoft.Maui.Controls" Version="8.0.3" />
<PackageVersion Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.3" />
Expand All @@ -23,7 +34,7 @@
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.UI.Xaml" Version="2.7.1" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.4.231219000" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="$(WinAppSdkVersion)" />
<PackageVersion Include="Microsoft.Graphics.Win2D" Version="1.2.0" />
<PackageVersion Include="Moq" Version="4.17.2" />
<PackageVersion Include="Refit" Version="6.3.2" />
Expand All @@ -48,15 +59,15 @@
<PackageVersion Include="Uno.Toolkit" Version="5.0.15" />
<PackageVersion Include="Uno.Toolkit.UI" Version="5.0.15" />
<PackageVersion Include="Uno.Toolkit.WinUI" Version="5.0.15" />
<PackageVersion Include="Uno.UI" Version="5.0.19" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.0.19"/>
<PackageVersion Include="Uno.UI.MSAL" Version="5.0.19" />
<PackageVersion Include="Uno.UI.Runtime.WebAssembly" Version="5.0.19" />
<PackageVersion Include="Uno.UI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="$(UnoVersion)"/>
<PackageVersion Include="Uno.UI.MSAL" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.UI.Runtime.WebAssembly" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.UI.RuntimeTests.Engine" Version="0.24.0-dev.95" />
<PackageVersion Include="Uno.WinUI" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Markup" Version="5.2.4" />
<PackageVersion Include="Uno.WinUI.MSAL" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.Runtime.WebAssembly" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.MSAL" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Runtime.WebAssembly" Version="$(UnoVersion)" />
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
<PackageVersion Include="xunit" Version="2.4.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.1" />
Expand Down
6 changes: 5 additions & 1 deletion src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Uno.Extensions.Hosting;
namespace Uno.Extensions.Hosting;

internal record ApplicationBuilder(Application App, LaunchActivatedEventArgs Arguments, Assembly ApplicationAssembly) : IApplicationBuilder
{
Expand All @@ -9,6 +9,10 @@ internal record ApplicationBuilder(Application App, LaunchActivatedEventArgs Arg
public Window Window { get; } =
#if NET6_0_OR_GREATER && WINDOWS && !HAS_UNO
new Window();
#elif HAS_UNO_WINUI
// Window.Current can be null with Uno.WinUI 5.2+
// When updating Hosting to Uno.WinUI 5.2+ we need to remove the compiler directive
Window.Current ?? new Window();
#else
Window.Current;
#endif
Expand Down
54 changes: 24 additions & 30 deletions src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,37 @@
using Uno.Extensions.Maui.Platform;
using Microsoft.Maui;
using Windows.UI.Core;
using Uno.UI.Xaml;

namespace Uno.Extensions.Maui;

partial class MauiEmbedding
{
// NOTE: This is meant to help initialize MauiEmbedding similar to MauiUIApplicationDelegate
// https://github.com/dotnet/maui/blob/ace9fe5e7d8d9bd16a2ae0b2fe2b888ad681433e/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs#L36-L70
private static MauiAppBuilder RegisterPlatformServices(this MauiAppBuilder builder, Application app)
{
builder.Services.AddSingleton<UIWindow>(sp =>
{
var window = sp.GetRequiredService<Microsoft.UI.Xaml.Window>();
// The _window field is the only way to grab the underlying UIWindow from inside the CoreWindow
// https://github.com/unoplatform/uno/blob/34a32058b812a0a08e658eba5e298ea9d258c231/src/Uno.UWP/UI/Core/CoreWindow.iOS.cs#L17
var internalWindow = typeof(CoreWindow).GetField("_window", BindingFlags.Instance | BindingFlags.NonPublic);
if(internalWindow is null)
{
throw new MauiEmbeddingException(Properties.Resources.MissingWindowPrivateField);
}
var uiwindow = internalWindow?.GetValue(window.CoreWindow) as UIWindow;
return uiwindow!;
})
.AddSingleton<IUIApplicationDelegate>(sp => sp.GetRequiredService<Application>());
// NOTE: This is meant to help initialize MauiEmbedding similar to MauiUIApplicationDelegate
// https://github.com/dotnet/maui/blob/ace9fe5e7d8d9bd16a2ae0b2fe2b888ad681433e/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs#L36-L70
private static MauiAppBuilder RegisterPlatformServices(this MauiAppBuilder builder, Application app)
{
builder.Services.AddTransient<UIWindow>(sp =>
{
var window = sp.GetRequiredService<Microsoft.UI.Xaml.Window>();
var nativeWindow = WindowHelper.GetNativeWindow(window);
return nativeWindow is UIWindow uiWindow ? uiWindow : throw new InvalidOperationException("Unable to locate the Native UIWindow");
})
.AddSingleton<IUIApplicationDelegate>(sp => sp.GetRequiredService<Application>());

return builder;
}
return builder;
}

private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp, Application app)
{
var rootContext = new MauiContext(mauiApp.Services);
rootContext.InitializeScopedServices();
private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp, Application app)
{
var rootContext = new MauiContext(mauiApp.Services);
rootContext.InitializeScopedServices();

var iApp = mauiApp.Services.GetRequiredService<IApplication>();
var iApp = mauiApp.Services.GetRequiredService<IApplication>();

Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService<UIWindow>().RootViewController!);
_ = new EmbeddedApplication(mauiApp.Services, iApp);
app.SetApplicationHandler(iApp, rootContext);
InitializeApplicationMainPage(iApp);
}
Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService<UIWindow>().RootViewController!);
_ = new EmbeddedApplication(mauiApp.Services, iApp);
app.SetApplicationHandler(iApp, rootContext);
InitializeApplicationMainPage(iApp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ services is null ||
}

#if WINUI
dialog.XamlRoot = Window!.Content.XamlRoot;
// NOTE: We needed to add the ! operator on Content due to a nullability issue starting in Uno.WinUI 5.2
dialog.XamlRoot = Window!.Content!.XamlRoot;
#endif

dialog.SetInstance(Region);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static class ServiceProviderExtensions
/// <param name="services">The IServiceProvider instance to attach</param>
public async static Task<IServiceProvider> AttachServicesAsync(this Window window, IServiceProvider services)
{
return await window.Content
// NOTE: This creates a nullable error in Uno.WinUI 5.2+
return await window.Content!
.AttachServiceProvider(services)
.RegisterWindowAsync(window);
}
Expand Down
6 changes: 6 additions & 0 deletions src/maui-embedding.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup>
<UnoVersion>5.2.22</UnoVersion>
<WinAppSdkVersion>1.5.240404000</WinAppSdkVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion testing/TestHarness/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="6.0.7" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.60.3" />
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.2.0" />
<PackageVersion Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.11" />
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" />
Expand Down

0 comments on commit 4162050

Please sign in to comment.