Skip to content

Commit 0d6579e

Browse files
committed
created avalonia project
1 parent fe0b201 commit 0d6579e

24 files changed

Lines changed: 410 additions & 0 deletions

CutCode.MultiPlatform/App.axaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Application xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:local="using:CutCode.MultiPlatform"
4+
x:Class="CutCode.MultiPlatform.App">
5+
<Application.DataTemplates>
6+
<local:ViewLocator/>
7+
</Application.DataTemplates>
8+
9+
<Application.Styles>
10+
<FluentTheme Mode="Light"/>
11+
</Application.Styles>
12+
</Application>

CutCode.MultiPlatform/App.axaml.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Controls.ApplicationLifetimes;
4+
using Avalonia.Markup.Xaml;
5+
using CutCode.MultiPlatform.ViewModels;
6+
using CutCode.MultiPlatform.Views;
7+
using Live.Avalonia;
8+
using ReactiveUI;
9+
using System;
10+
using System.Diagnostics;
11+
using System.Reactive;
12+
13+
namespace CutCode.MultiPlatform
14+
{
15+
public class App : Application, ILiveView
16+
{
17+
public override void Initialize()
18+
{
19+
AvaloniaXamlLoader.Load(this);
20+
}
21+
22+
public override void OnFrameworkInitializationCompleted()
23+
{
24+
if (Debugger.IsAttached || IsProduction())
25+
{
26+
var window = new Window();
27+
window.Content = CreateView(window);
28+
window.Show();
29+
}
30+
else
31+
{
32+
var window = new LiveViewHost(this, Console.WriteLine);
33+
window.StartWatchingSourceFilesForHotReloading();
34+
window.Show();
35+
}
36+
RxApp.DefaultExceptionHandler = Observer.Create<Exception>(Console.WriteLine);
37+
base.OnFrameworkInitializationCompleted();
38+
}
39+
public object CreateView(Window window)
40+
{
41+
window.DataContext ??= new ViewModelBase();
42+
return new
43+
}
44+
45+
private static bool IsProduction()
46+
{
47+
#if DEBUG
48+
return false;
49+
#else
50+
return true;
51+
#endif
52+
}
53+
}
54+
55+
}
90.9 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>WinExe</OutputType>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Folder Include="Models\" />
9+
<AvaloniaResource Include="Assets\**" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Avalonia" Version="0.10.3" />
13+
<PackageReference Include="Avalonia.Desktop" Version="0.10.3" />
14+
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.3" />
15+
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.3" />
16+
<PackageReference Include="Live.Avalonia" Version="1.3.1" />
17+
</ItemGroup>
18+
<ItemGroup>
19+
<Resource Include="Assets\logo.ico" />
20+
<Resource Include="logo.ico" />
21+
</ItemGroup>
22+
<ItemGroup>
23+
<Watch Include="**\*.xaml" />
24+
</ItemGroup>
25+
</Project>

CutCode.MultiPlatform/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Avalonia;
2+
using Avalonia.Controls.ApplicationLifetimes;
3+
using Avalonia.ReactiveUI;
4+
using System;
5+
6+
namespace CutCode.MultiPlatform
7+
{
8+
class Program
9+
{
10+
// Initialization code. Don't use any Avalonia, third-party APIs or any
11+
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
12+
// yet and stuff might break.
13+
public static void Main(string[] args) => BuildAvaloniaApp()
14+
.StartWithClassicDesktopLifetime(args);
15+
16+
// Avalonia configuration, don't remove; also used by visual designer.
17+
public static AppBuilder BuildAvaloniaApp()
18+
=> AppBuilder.Configure<App>()
19+
.UsePlatformDetect()
20+
.LogToTrace()
21+
.UseReactiveUI();
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Avalonia.Controls;
2+
using Avalonia.Controls.Templates;
3+
using CutCode.MultiPlatform.ViewModels;
4+
using System;
5+
6+
namespace CutCode.MultiPlatform
7+
{
8+
public class ViewLocator : IDataTemplate
9+
{
10+
public bool SupportsRecycling => false;
11+
12+
public IControl Build(object data)
13+
{
14+
var name = data.GetType().FullName!.Replace("ViewModel", "View");
15+
var type = Type.GetType(name);
16+
17+
if (type != null)
18+
{
19+
return (Control)Activator.CreateInstance(type)!;
20+
}
21+
else
22+
{
23+
return new TextBlock { Text = "Not Found: " + name };
24+
}
25+
}
26+
27+
public bool Match(object data)
28+
{
29+
return data is ViewModelBase;
30+
}
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CutCode.MultiPlatform.ViewModels
6+
{
7+
public class MainWindowViewModel : ViewModelBase
8+
{
9+
public string Greeting => "Welcome to Avalonia!";
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using ReactiveUI;
2+
3+
namespace CutCode.MultiPlatform.ViewModels
4+
{
5+
public class ViewModelBase : ReactiveObject
6+
{
7+
8+
}
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:vm="using:CutCode.MultiPlatform.ViewModels"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
mc:Ignorable="d"
7+
MinHeight="500" MinWidth="700"
8+
Height="570" Width="800"
9+
x:Class="CutCode.MultiPlatform.Views.MainWindow"
10+
Icon="/Assets/logo.ico"
11+
Title="CutCode.MultiPlatform">
12+
13+
<Design.DataContext>
14+
<vm:MainWindowViewModel/>
15+
</Design.DataContext>
16+
17+
<Button Content="Click me" Width="200" Height="100" BorderThickness="0" Background="Red"/>
18+
19+
</Window>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Markup.Xaml;
4+
5+
namespace CutCode.MultiPlatform.Views
6+
{
7+
public partial class MainWindow : Window
8+
{
9+
public MainWindow()
10+
{
11+
InitializeComponent();
12+
#if DEBUG
13+
this.AttachDevTools();
14+
#endif
15+
}
16+
17+
private void InitializeComponent()
18+
{
19+
AvaloniaXamlLoader.Load(this);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)