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:
parent
0368c711d6
commit
eab730c1f2
@ -1,3 +1,4 @@
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Markup.Xaml;
|
||||
@ -30,12 +31,7 @@ namespace AvaloniaCoreRTDemo
|
||||
this.InitializeThemes();
|
||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow =
|
||||
#if !OSX
|
||||
new MainWindow();
|
||||
#else
|
||||
new MainWindowMacOS();
|
||||
#endif
|
||||
desktop.MainWindow = !Utilities.IsOSX ? new MainWindow() : new MainWindowMacOS();
|
||||
this.DataContext = desktop.MainWindow.DataContext;
|
||||
}
|
||||
base.OnFrameworkInitializationCompleted();
|
||||
|
@ -99,15 +99,11 @@
|
||||
<Compile Update="Windows/MainWindow.axaml.cs">
|
||||
<DependentUpon>MainWindow.axaml</DependentUpon>
|
||||
</Compile>
|
||||
<AvaloniaXaml Remove="Windows/MainWindowMacOS.axaml" />
|
||||
<Compile Remove="Windows/MainWindowMacOS.axaml.cs" />
|
||||
</ItemGroup>
|
||||
<!--OSX only xaml-->
|
||||
<ItemGroup Condition="'$(IsOSX)'=='true'">
|
||||
<Compile Update="Windows/MainWindowMacOS.axaml.cs">
|
||||
<DependentUpon>MainWindowMacOS.axaml</DependentUpon>
|
||||
</Compile>
|
||||
<AvaloniaXaml Remove="Windows/MainWindow.axaml" />
|
||||
<Compile Remove="Windows/MainWindow.axaml.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using Avalonia.Media.Imaging;
|
||||
|
||||
@ -13,22 +12,23 @@ namespace AvaloniaCoreRTDemo.Controls.ViewModels
|
||||
private readonly IBitmap _dotNetImage;
|
||||
private readonly IBitmap _avaloniaImage;
|
||||
|
||||
public IBitmap DotNetImage
|
||||
{
|
||||
get { return _dotNetImage; }
|
||||
set { this.RaiseAndSetIfChanged(ref Unsafe.AsRef(this._dotNetImage), value); }
|
||||
}
|
||||
public IBitmap DotNetImage => this._dotNetImage;
|
||||
|
||||
public IBitmap AvaloniaImage
|
||||
{
|
||||
get { return _avaloniaImage; }
|
||||
set { this.RaiseAndSetIfChanged(ref Unsafe.AsRef(this._avaloniaImage), value); }
|
||||
}
|
||||
public IBitmap AvaloniaImage => this._avaloniaImage;
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
this._dotNetImage = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dotnet.png"));
|
||||
this._avaloniaImage = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "avalonia.png"));
|
||||
this._dotNetImage = Utilities.GetImageFromFile(GetImageFullPath("dotnet.png"));
|
||||
this._avaloniaImage = Utilities.GetImageFromFile(GetImageFullPath("avalonia.png"));
|
||||
}
|
||||
|
||||
private static String GetImageFullPath(String fileName)
|
||||
=> Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
|
||||
|
||||
~MainViewModel()
|
||||
{
|
||||
this._dotNetImage.Dispose();
|
||||
this._avaloniaImage.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
src/Images/broken-link.png
Normal file
BIN
src/Images/broken-link.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
@ -1,9 +1,8 @@
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia;
|
||||
|
||||
namespace AvaloniaCoreRTDemo
|
||||
{
|
||||
class Program
|
||||
public static class Program
|
||||
{
|
||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||
@ -13,9 +12,10 @@ namespace AvaloniaCoreRTDemo
|
||||
// Avalonia configuration, don't remove; also used by visual designer.
|
||||
public static AppBuilder BuildAvaloniaApp()
|
||||
=> AppBuilder.Configure<App>().UsePlatformDetect()
|
||||
#if OSX
|
||||
.UseAvaloniaNative()
|
||||
#endif
|
||||
.UseAvaloniaNativeOSX()
|
||||
.LogToTrace();
|
||||
|
||||
private static AppBuilder UseAvaloniaNativeOSX(this AppBuilder appBuilder)
|
||||
=> Utilities.IsOSX ? appBuilder.UseAvaloniaNative() : appBuilder;
|
||||
}
|
||||
}
|
||||
|
39
src/Utilities.cs
Normal file
39
src/Utilities.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Avalonia.Media.Imaging;
|
||||
|
||||
namespace AvaloniaCoreRTDemo
|
||||
{
|
||||
internal static class Utilities
|
||||
{
|
||||
public static readonly Boolean IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
public static readonly Boolean IsOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
||||
|
||||
public 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;
|
||||
}
|
||||
|
||||
public static Bitmap GetImageFromFile(String path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Bitmap(path);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return GetImageFromResources("broken-link.png");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user