Sample improves

* No remove axaml files for makes the sample simpler.
* Creation of utilities class.
* Avoid using ref Bitmap on MainViewModel.
* Avoid application crashing when dotnet.png or avalonia.png are missing. Using an replace embeded image instead.
* Conditionally invoke of UseAvaloniaNative at runtime.
This commit is contained in:
Joseph Moreno
2021-12-13 16:17:55 -05:00
parent 0368c711d6
commit eab730c1f2
9 changed files with 69 additions and 54 deletions

View File

@ -1,7 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Avalonia.Media.Imaging;
@ -34,35 +31,24 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return !_darkTheme ? "windows.png" : "windows_d.png";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return !_darkTheme ? "macos.png" : "macos_d.png";
if (Utilities.IsWindows)
return !this._darkTheme ? "windows.png" : "windows_d.png";
else if (Utilities.IsOSX)
return !this._darkTheme ? "macos.png" : "macos_d.png";
else
return !_darkTheme ? "linux.png" : "linux_d.png";
return !this._darkTheme ? "linux.png" : "linux_d.png";
}
}
public AboutViewModel(Boolean darkTheme)
{
this._darkTheme = darkTheme;
this._computerImage = GetImageFromResources(this.ComputerImageName);
this._computerImage = Utilities.GetImageFromResources(this.ComputerImageName);
}
~AboutViewModel()
{
this._computerImage.Dispose();
}
private static Bitmap GetImageFromResources(String fileName)
{
Assembly asm = Assembly.GetExecutingAssembly();
String resourceName = asm.GetManifestResourceNames().FirstOrDefault(str => str.EndsWith(fileName));
if (resourceName != null)
using (Stream bitmapStream = asm.GetManifestResourceStream(resourceName))
return new Bitmap(bitmapStream);
else
return default;
}
}
}

View File

@ -55,7 +55,7 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
public void DefaultDarkMethod() => this.ChangeTheme(ApplicationTheme.DefaultDark);
public void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight);
public void FluentDarkMethod() => this.ChangeTheme(ApplicationTheme.FluentDark);
public void HelpAboutMethod() => base.RunHelpAbout(this._window);
public override void HelpAboutMethod() => base.RunHelpAbout(this._window);
private void RunFileExit()
=> Environment.Exit(0);

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls;
@ -26,6 +22,8 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
public MainViewModelBase(IThemeSwitch window)
=> this._themeSwitch = window;
public abstract void HelpAboutMethod();
protected async void RunHelpAbout(Window currentWindow)
{
if (this.AboutEnabled)