diff --git a/src/Avalonia.Controls/WindowIcon.cs b/src/Avalonia.Controls/WindowIcon.cs index 20ea872cad..4f81d1ebe0 100644 --- a/src/Avalonia.Controls/WindowIcon.cs +++ b/src/Avalonia.Controls/WindowIcon.cs @@ -1,4 +1,5 @@ using System.IO; +using Avalonia.Logging; using Avalonia.Media.Imaging; using Avalonia.Platform; @@ -11,21 +12,48 @@ namespace Avalonia.Controls { public WindowIcon(IBitmap bitmap) { - PlatformImpl = AvaloniaLocator.Current.GetRequiredService().LoadIcon(bitmap.PlatformImpl.Item); + if (AvaloniaLocator.Current.GetService() is { } iconLoader) + { + PlatformImpl = iconLoader.LoadIcon(bitmap.PlatformImpl.Item); + } + else + { + DoLogIfNull(); + } } public WindowIcon(string fileName) { - PlatformImpl = AvaloniaLocator.Current.GetRequiredService().LoadIcon(fileName); + if (AvaloniaLocator.Current.GetService() is { } iconLoader) + { + PlatformImpl = iconLoader.LoadIcon(fileName); + } + else + { + DoLogIfNull(); + } } public WindowIcon(Stream stream) { - PlatformImpl = AvaloniaLocator.Current.GetRequiredService().LoadIcon(stream); + if (AvaloniaLocator.Current.GetService() is { } iconLoader) + { + PlatformImpl = iconLoader.LoadIcon(stream); + } + else + { + DoLogIfNull(); + } } - public IWindowIconImpl PlatformImpl { get; } + private void DoLogIfNull() + { + Logger.TryGet(LogEventLevel.Error, LogArea.Platforms) + ?.Log(this, "Error: Missing IPlatformIconLoader implementation in current platform."); + } + + public IWindowIconImpl? PlatformImpl { get; } - public void Save(Stream stream) => PlatformImpl.Save(stream); + public void Save(Stream stream) => PlatformImpl?.Save(stream); } }