Skip to content

Commit

Permalink
fix: Icon not set on WinUI window
Browse files Browse the repository at this point in the history
Fixes #107
  • Loading branch information
nickrandolph committed May 8, 2023
1 parent 65c76f3 commit efd37e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
#if (useLoggingFallback)
using System;
using Microsoft.Extensions.Logging;
#endif
using Microsoft.UI.Xaml;

#elif (useCsharpMarkup)
using Microsoft.UI.Xaml;

#endif
namespace MyExtensionsApp._1;

public sealed partial class AppHead : App
Expand All @@ -25,7 +22,6 @@ public AppHead()
{
this.InitializeComponent();
}
#if useCsharpMarkup

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
Expand All @@ -34,12 +30,15 @@ public AppHead()
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
#if useCsharpMarkup
Resources.Build(r => r.Merged(
new AppResources()));

#endif
base.OnLaunched(args);

SetWindowsIcon();
}
#endif
#if (useLoggingFallback)

/// <summary>
Expand Down Expand Up @@ -113,4 +112,23 @@ private static void InitializeLogging()
//+:cnd:noEmit
}
#endif

private void SetWindowsIcon()
{
//-:cnd:noEmit
#if WINDOWS
var hWnd =
WinRT.Interop.WindowNative.GetWindowHandle(MainWindow);

// Retrieve the WindowId that corresponds to hWnd.
Microsoft.UI.WindowId windowId =
Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);

// Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.
Microsoft.UI.Windowing.AppWindow appWindow =
Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("iconapp.ico");
#endif
//+:cnd:noEmit
}
}
12 changes: 6 additions & 6 deletions src/Uno.Templates/content/unoapp/MyExtensionsApp._1/App.blank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ namespace MyExtensionsApp._1;

public class App : Application
{
public static Window? _window;
protected Window? MainWindow {get; private set; }

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
#if NET6_0_OR_GREATER && WINDOWS && !HAS_UNO
_window = new Window();
MainWindow = new Window();
#else
_window = Microsoft.UI.Xaml.Window.Current;
MainWindow = Microsoft.UI.Xaml.Window.Current;
#endif

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (_window.Content is not Frame rootFrame)
if (MainWindow.Content is not Frame rootFrame)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();

// Place the frame in the current Window
_window.Content = rootFrame;
MainWindow.Content = rootFrame;

rootFrame.NavigationFailed += OnNavigationFailed;
}
Expand All @@ -35,7 +35,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
}

// Ensure the current window is active
_window.Activate();
MainWindow.Activate();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace MyExtensionsApp._1;

public class App : Application
{
private static Window? _window;
public static IHost? Host { get; private set; }
protected Window? MainWindow {get; private set; }
private IHost? Host { get; set; }

//+:cnd:noEmit
#if useFrameNav
Expand Down Expand Up @@ -125,21 +125,21 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
.UseNavigation(RegisterRoutes)
#endif
);
_window = builder.Window;
MainWindow = builder.Window;

#if useFrameNav
//-:cnd:noEmit
Host = builder.Build();

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (_window.Content is not Frame rootFrame)
if (MainWindow.Content is not Frame rootFrame)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();

// Place the frame in the current Window
_window.Content = rootFrame;
MainWindow.Content = rootFrame;
}

if (rootFrame.Content == null)
Expand All @@ -150,7 +150,7 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
rootFrame.Navigate(typeof(MainPage), args.Arguments);
}
// Ensure the current window is active
_window.Activate();
MainWindow.Activate();
//+:cnd:noEmit
#elif (!useAuthentication)
Host = await builder.NavigateAsync<Shell>();
Expand Down

0 comments on commit efd37e6

Please sign in to comment.