Browse Source

add null checks to WindowIcon.cs and just log if the current platform has a missing IPlatformIcon impl.

pull/8927/head
Jumar Macato 4 years ago
parent
commit
0e4b24b1f9
  1. 38
      src/Avalonia.Controls/WindowIcon.cs

38
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<IPlatformIconLoader>().LoadIcon(bitmap.PlatformImpl.Item);
if (AvaloniaLocator.Current.GetService<IPlatformIconLoader>() is { } iconLoader)
{
PlatformImpl = iconLoader.LoadIcon(bitmap.PlatformImpl.Item);
}
else
{
DoLogIfNull();
}
}
public WindowIcon(string fileName)
{
PlatformImpl = AvaloniaLocator.Current.GetRequiredService<IPlatformIconLoader>().LoadIcon(fileName);
if (AvaloniaLocator.Current.GetService<IPlatformIconLoader>() is { } iconLoader)
{
PlatformImpl = iconLoader.LoadIcon(fileName);
}
else
{
DoLogIfNull();
}
}
public WindowIcon(Stream stream)
{
PlatformImpl = AvaloniaLocator.Current.GetRequiredService<IPlatformIconLoader>().LoadIcon(stream);
if (AvaloniaLocator.Current.GetService<IPlatformIconLoader>() 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);
}
}

Loading…
Cancel
Save