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:
		| @@ -1,3 +1,4 @@ | |||||||
|  |  | ||||||
| using Avalonia; | using Avalonia; | ||||||
| using Avalonia.Controls.ApplicationLifetimes; | using Avalonia.Controls.ApplicationLifetimes; | ||||||
| using Avalonia.Markup.Xaml; | using Avalonia.Markup.Xaml; | ||||||
| @@ -30,12 +31,7 @@ namespace AvaloniaCoreRTDemo | |||||||
|             this.InitializeThemes(); |             this.InitializeThemes(); | ||||||
|             if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) |             if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||||||
|             { |             { | ||||||
|                 desktop.MainWindow =  |                 desktop.MainWindow = !Utilities.IsOSX ? new MainWindow() : new MainWindowMacOS(); | ||||||
| #if !OSX |  | ||||||
|                 new MainWindow(); |  | ||||||
| #else |  | ||||||
|                 new MainWindowMacOS(); |  | ||||||
| #endif |  | ||||||
|                 this.DataContext = desktop.MainWindow.DataContext; |                 this.DataContext = desktop.MainWindow.DataContext; | ||||||
|             } |             } | ||||||
|             base.OnFrameworkInitializationCompleted(); |             base.OnFrameworkInitializationCompleted(); | ||||||
|   | |||||||
| @@ -99,15 +99,11 @@ | |||||||
|     <Compile Update="Windows/MainWindow.axaml.cs"> |     <Compile Update="Windows/MainWindow.axaml.cs"> | ||||||
|       <DependentUpon>MainWindow.axaml</DependentUpon> |       <DependentUpon>MainWindow.axaml</DependentUpon> | ||||||
| 	</Compile> | 	</Compile> | ||||||
|     <AvaloniaXaml Remove="Windows/MainWindowMacOS.axaml" /> |  | ||||||
|     <Compile Remove="Windows/MainWindowMacOS.axaml.cs" /> |  | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <!--OSX only xaml--> |   <!--OSX only xaml--> | ||||||
|   <ItemGroup Condition="'$(IsOSX)'=='true'"> |   <ItemGroup Condition="'$(IsOSX)'=='true'"> | ||||||
|     <Compile Update="Windows/MainWindowMacOS.axaml.cs"> |     <Compile Update="Windows/MainWindowMacOS.axaml.cs"> | ||||||
|       <DependentUpon>MainWindowMacOS.axaml</DependentUpon> |       <DependentUpon>MainWindowMacOS.axaml</DependentUpon> | ||||||
|     </Compile> |     </Compile> | ||||||
|     <AvaloniaXaml Remove="Windows/MainWindow.axaml" /> |  | ||||||
|     <Compile Remove="Windows/MainWindow.axaml.cs" /> |  | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
| </Project> | </Project> | ||||||
| @@ -1,6 +1,5 @@ | |||||||
| using System; | using System; | ||||||
| using System.IO; | using System.IO; | ||||||
| using System.Runtime.CompilerServices; |  | ||||||
|  |  | ||||||
| using Avalonia.Media.Imaging; | using Avalonia.Media.Imaging; | ||||||
|  |  | ||||||
| @@ -13,22 +12,23 @@ namespace AvaloniaCoreRTDemo.Controls.ViewModels | |||||||
|         private readonly IBitmap _dotNetImage; |         private readonly IBitmap _dotNetImage; | ||||||
|         private readonly IBitmap _avaloniaImage; |         private readonly IBitmap _avaloniaImage; | ||||||
|  |  | ||||||
|         public IBitmap DotNetImage |         public IBitmap DotNetImage => this._dotNetImage; | ||||||
|         { |  | ||||||
|             get { return _dotNetImage; } |  | ||||||
|             set { this.RaiseAndSetIfChanged(ref Unsafe.AsRef(this._dotNetImage), value); } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public IBitmap AvaloniaImage |         public IBitmap AvaloniaImage => this._avaloniaImage; | ||||||
|         { |  | ||||||
|             get { return _avaloniaImage; } |  | ||||||
|             set { this.RaiseAndSetIfChanged(ref Unsafe.AsRef(this._avaloniaImage), value); } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public MainViewModel() |         public MainViewModel() | ||||||
|         { |         { | ||||||
|             this._dotNetImage = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dotnet.png")); |             this._dotNetImage = Utilities.GetImageFromFile(GetImageFullPath("dotnet.png")); | ||||||
|             this._avaloniaImage = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "avalonia.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 | namespace AvaloniaCoreRTDemo | ||||||
| { | { | ||||||
|     class Program |     public static class Program | ||||||
|     { |     { | ||||||
|         // Initialization code. Don't use any Avalonia, third-party APIs or any |         // Initialization code. Don't use any Avalonia, third-party APIs or any | ||||||
|         // SynchronizationContext-reliant code before AppMain is called: things aren't initialized |         // 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. |         // Avalonia configuration, don't remove; also used by visual designer. | ||||||
|         public static AppBuilder BuildAvaloniaApp() |         public static AppBuilder BuildAvaloniaApp() | ||||||
|             => AppBuilder.Configure<App>().UsePlatformDetect() |             => AppBuilder.Configure<App>().UsePlatformDetect() | ||||||
| #if OSX |             .UseAvaloniaNativeOSX() | ||||||
|             .UseAvaloniaNative() |  | ||||||
| #endif |  | ||||||
|             .LogToTrace(); |             .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; | ||||||
| using System.IO; |  | ||||||
| using System.Linq; |  | ||||||
| using System.Reflection; |  | ||||||
| using System.Runtime.InteropServices; | using System.Runtime.InteropServices; | ||||||
|  |  | ||||||
| using Avalonia.Media.Imaging; | using Avalonia.Media.Imaging; | ||||||
| @@ -34,35 +31,24 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels | |||||||
|         { |         { | ||||||
|             get |             get | ||||||
|             { |             { | ||||||
|                 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |                 if (Utilities.IsWindows) | ||||||
|                     return !_darkTheme ? "windows.png" : "windows_d.png"; |                     return !this._darkTheme ? "windows.png" : "windows_d.png"; | ||||||
|                 else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |                 else if (Utilities.IsOSX) | ||||||
|                     return !_darkTheme ? "macos.png" : "macos_d.png"; |                     return !this._darkTheme ? "macos.png" : "macos_d.png"; | ||||||
|                 else |                 else | ||||||
|                     return !_darkTheme ? "linux.png" : "linux_d.png"; |                     return !this._darkTheme ? "linux.png" : "linux_d.png"; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public AboutViewModel(Boolean darkTheme) |         public AboutViewModel(Boolean darkTheme) | ||||||
|         { |         { | ||||||
|             this._darkTheme = darkTheme; |             this._darkTheme = darkTheme; | ||||||
|             this._computerImage = GetImageFromResources(this.ComputerImageName); |             this._computerImage = Utilities.GetImageFromResources(this.ComputerImageName); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         ~AboutViewModel() |         ~AboutViewModel() | ||||||
|         { |         { | ||||||
|             this._computerImage.Dispose(); |             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 DefaultDarkMethod() => this.ChangeTheme(ApplicationTheme.DefaultDark); | ||||||
|         public void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight); |         public void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight); | ||||||
|         public void FluentDarkMethod() => this.ChangeTheme(ApplicationTheme.FluentDark); |         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() |         private void RunFileExit() | ||||||
|             => Environment.Exit(0); |             => Environment.Exit(0); | ||||||
|   | |||||||
| @@ -1,8 +1,4 @@ | |||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Linq; |  | ||||||
| using System.Text; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
|  |  | ||||||
| using Avalonia.Controls; | using Avalonia.Controls; | ||||||
|  |  | ||||||
| @@ -26,6 +22,8 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels | |||||||
|         public MainViewModelBase(IThemeSwitch window) |         public MainViewModelBase(IThemeSwitch window) | ||||||
|             => this._themeSwitch = window; |             => this._themeSwitch = window; | ||||||
|  |  | ||||||
|  |         public abstract void HelpAboutMethod(); | ||||||
|  |  | ||||||
|         protected async void RunHelpAbout(Window currentWindow) |         protected async void RunHelpAbout(Window currentWindow) | ||||||
|         { |         { | ||||||
|             if (this.AboutEnabled) |             if (this.AboutEnabled) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Joseph Moreno
					Joseph Moreno