{"id":5812,"date":"2022-10-11T20:54:42","date_gmt":"2022-10-11T20:54:42","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5812"},"modified":"2022-10-11T20:54:42","modified_gmt":"2022-10-11T20:54:42","slug":"trying-out-avalonia","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/trying-out-avalonia\/","title":{"rendered":"Trying out Avalonia"},"content":{"rendered":"<p><em>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind.<\/em><\/p>\n<p>Avalonia is aiming to support a XAML way of implementing cross platform UI&#8217;s. Whilst I can use Xamarin Forms for developing on iOS, Android etc. it doesn&#8217;t currently support Linux.<\/p>\n<p>So let&#8217;s have a look at implementing a very basic Hello World application using Avalonia. This post covers using Visual Studio 2017 (obviously on Windows). <\/p>\n<ul>\n<li>Creating a WPF application, mine&#8217;s named HelloAvaloniaWorld<\/li>\n<li>Remove the following references\n<ul>\n<li>PresentationCore<\/li>\n<li>PresentationFrameworj<\/li>\n<li>WindowsBase<\/li>\n<\/ul>\n<p>as we&#8217;re not using WPF\n<\/li>\n<li>Add NuGet packages\n<ul>\n<li>Avalonia<\/li>\n<li>Avalonia.Desktop<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Now replace App.xaml with the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Application xmlns=&quot;https:\/\/github.com\/avaloniaui&quot;&gt;\r\n    &lt;Application.Styles&gt;\r\n        &lt;StyleInclude Source=&quot;resm:Avalonia.Themes.Default.DefaultTheme.xaml?assembly=Avalonia.Themes.Default&quot;\/&gt;\r\n        &lt;StyleInclude Source=&quot;resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default&quot;\/&gt;\r\n    &lt;\/Application.Styles&gt;\r\n&lt;\/Application&gt;\r\n<\/pre>\n<p>Replace the application class in App.xaml.cs with <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Avalonia;\r\nusing Avalonia.Markup.Xaml;\r\n\r\nnamespace HelloAvaloniaWorld\r\n{\r\n    public class App : Application\r\n    {\r\n        public override void Initialize()\r\n        {\r\n            AvaloniaXamlLoader.Load(this);\r\n        }\r\n\r\n        private static void Main()\r\n        {\r\n            AppBuilder.Configure&lt;App&gt;()\r\n                .UsePlatformDetect()\r\n                .Start&lt;MainWindow&gt;();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Let&#8217;s now alter the MainWindow.xaml to look like <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Window xmlns=&quot;https:\/\/github.com\/avaloniaui&quot;&gt;\r\n    &lt;Grid&gt;\r\n        &lt;TextBlock FontSize=&quot;16&quot; Text=&quot;Hello World&quot;\/&gt;\r\n    &lt;\/Grid&gt;\r\n&lt;\/Window&gt;\r\n<\/pre>\n<p>And replace the MainWindow.xaml.cs contents with<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Avalonia;\r\nusing Avalonia.Controls;\r\nusing Avalonia.Markup.Xaml;\r\n\r\nnamespace HelloAvaloniaWorld\r\n{\r\n    public class MainWindow : Window\r\n    {\r\n        public MainWindow()\r\n        {\r\n            this.InitializeComponent();\r\n            this.AttachDevTools();\r\n        }\r\n\r\n        private void InitializeComponent()\r\n        {\r\n            AvaloniaXamlLoader.Load(this);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Before we move on, select each XAML file and display the file properties in Visual Studio, from here, remove the Custom Tool and change the Build Action to Embedded Resource.<\/p>\n<p>From AssemblyInfo.cs remove the section<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;assembly: ThemeInfo(...)]\r\n<\/pre>\n<p>Before the application builds you&#8217;ll need to copy the packages\\SkiaSharp.1.57.1\\build and runtime folders to the solution root folder, into a folder named SkiaSharp\\1.57.1.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind. Avalonia is aiming to support a XAML way of implementing cross platform UI&#8217;s. Whilst I can use Xamarin Forms for developing on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-5812","post","type-post","status-publish","format-standard","hentry","category-avalonia"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5812"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5812\/revisions"}],"predecessor-version":[{"id":9589,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5812\/revisions\/9589"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}