diff --git a/packages/Avalonia/AvaloniaBuildTasks.targets b/packages/Avalonia/AvaloniaBuildTasks.targets index 88a6403e00..f0a7f98c03 100644 --- a/packages/Avalonia/AvaloniaBuildTasks.targets +++ b/packages/Avalonia/AvaloniaBuildTasks.targets @@ -3,9 +3,8 @@ low <_AvaloniaSkipXamlCompilation Condition="'$(_AvaloniaSkipXamlCompilation)' == ''">false false + true - - @@ -31,7 +30,14 @@ - + + + + + + diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index a45f544203..3c3841031f 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Avalonia.Automation.Peers; using Avalonia.Controls.Platform; @@ -87,6 +88,7 @@ namespace Avalonia.Controls /// public class Window : WindowBase, IFocusScope, ILayoutRoot { + private static readonly Lazy s_defaultIcon = new(LoadDefaultIcon); private readonly List<(Window child, bool isDialog)> _children = new List<(Window, bool)>(); private bool _isExtendedIntoWindowDecorations; private Thickness _windowDecorationMargin; @@ -228,7 +230,7 @@ namespace Avalonia.Controls this.GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl?.Resize(x, WindowResizeReason.Application)); CreatePlatformImplBinding(TitleProperty, title => PlatformImpl!.SetTitle(title)); - CreatePlatformImplBinding(IconProperty, icon => PlatformImpl!.SetIcon(icon?.PlatformImpl)); + CreatePlatformImplBinding(IconProperty, icon => PlatformImpl!.SetIcon((icon ?? s_defaultIcon.Value)?.PlatformImpl)); CreatePlatformImplBinding(CanResizeProperty, canResize => PlatformImpl!.CanResize(canResize)); CreatePlatformImplBinding(ShowInTaskbarProperty, show => PlatformImpl!.ShowTaskbarIcon(show)); @@ -1100,5 +1102,19 @@ namespace Avalonia.Controls { return new WindowAutomationPeer(this); } + + private static WindowIcon? LoadDefaultIcon() + { + // Use AvaloniaLocator instead of static AssetLoader, so it won't fail on Unit Tests without any asset loader. + if (AvaloniaLocator.Current.GetService() is { } assetLoader + && Assembly.GetEntryAssembly()?.GetName()?.Name is { } assemblyName + && Uri.TryCreate($"avares://{assemblyName}/!__AvaloniaDefaultWindowIcon", UriKind.Absolute, out var path) + && assetLoader.Exists(path)) + { + using var stream = assetLoader.Open(path); + return new WindowIcon(stream); + } + return null; + } } }