Browse Source

[base] 调整框架适配 linux 平台

master
fajiao 2 years ago
parent
commit
c0577bff5a
  1. 31
      JiLinApp/App.axaml.cs
  2. BIN
      JiLinApp/Assets/Fonts/msyh.ttf
  3. 17
      JiLinApp/Core/Avalonia/NativeHost.cs
  4. 7
      JiLinApp/Core/Avalonia/ViewModelBase.cs
  5. 1
      JiLinApp/JiLinApp.csproj
  6. 4
      JiLinApp/Pages/Main/MainWindow.axaml
  7. 11
      JiLinApp/Pages/Main/MainWindow.axaml.cs
  8. 8
      JiLinApp/Pages/Main/MainWindowViewModel.cs
  9. 45
      JiLinApp/Program.cs
  10. 7
      JiLinApp/ViewModels/MainWindowViewModel.cs
  11. 8
      JiLinApp/ViewModels/ViewModelBase.cs
  12. 12
      JiLinApp/Views/MainWindow.axaml.cs

31
JiLinApp/App.axaml.cs

@ -1,29 +1,26 @@
using Avalonia; using Avalonia;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using JiLinApp.ViewModels; using JiLinApp.Pages.Main;
using JiLinApp.Views;
namespace JiLinApp namespace JiLinApp;
public partial class App : Application
{ {
public partial class App : Application public override void Initialize()
{ {
public override void Initialize() AvaloniaXamlLoader.Load(this);
{ }
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted() public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) desktop.MainWindow = new MainWindow
{ {
desktop.MainWindow = new MainWindow DataContext = new MainWindowViewModel(),
{ };
DataContext = new MainWindowViewModel(),
};
}
base.OnFrameworkInitializationCompleted();
} }
base.OnFrameworkInitializationCompleted();
} }
} }

BIN
JiLinApp/Assets/Fonts/msyh.ttf

Binary file not shown.

17
JiLinApp/Core/Avalonia/NativeHost.cs

@ -0,0 +1,17 @@
using Avalonia.Controls;
using Avalonia.Platform;
using System;
namespace JiLinApp.Core.Avalonia;
public class NativeHost : NativeControlHost
{
public IntPtr Handle { get; set; }
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
{
IPlatformHandle? pfHandle = base.CreateNativeControlCore(parent);
Handle = pfHandle.Handle;
return pfHandle;
}
}

7
JiLinApp/Core/Avalonia/ViewModelBase.cs

@ -0,0 +1,7 @@
using ReactiveUI;
namespace JiLinApp.Core.Avalonia;
public class ViewModelBase : ReactiveObject
{
}

1
JiLinApp/JiLinApp.csproj

@ -9,7 +9,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" /> <AvaloniaResource Include="Assets\**" />
</ItemGroup> </ItemGroup>

4
JiLinApp/Views/MainWindow.axaml → JiLinApp/Pages/Main/MainWindow.axaml

@ -1,10 +1,10 @@
<Window xmlns="https://github.com/avaloniaui" <Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:JiLinApp.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:JiLinApp.Pages.Main"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="JiLinApp.Views.MainWindow" x:Class="JiLinApp.Pages.Main.MainWindow"
x:DataType="vm:MainWindowViewModel" x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico" Icon="/Assets/avalonia-logo.ico"
Title="JiLinApp"> Title="JiLinApp">

11
JiLinApp/Pages/Main/MainWindow.axaml.cs

@ -0,0 +1,11 @@
using Avalonia.Controls;
namespace JiLinApp.Pages.Main;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}

8
JiLinApp/Pages/Main/MainWindowViewModel.cs

@ -0,0 +1,8 @@
using JiLinApp.Core.Avalonia;
namespace JiLinApp.Pages.Main;
public class MainWindowViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
}

45
JiLinApp/Program.cs

@ -1,24 +1,35 @@
using Avalonia; using Avalonia;
using Avalonia.Media;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
using System; using System;
namespace JiLinApp namespace JiLinApp;
internal class Program
{ {
internal 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
// Initialization code. Don't use any Avalonia, third-party APIs or any // yet and stuff might break.
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized [STAThread]
// yet and stuff might break. public static void Main(string[] args) => BuildAvaloniaApp()
[STAThread] .StartWithClassicDesktopLifetime(args);
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
// 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>() => AppBuilder.Configure<App>()
.UsePlatformDetect() .UsePlatformDetect()
.WithInterFont() .WithInterFont()
.LogToTrace() .LogToTrace()
.UseReactiveUI(); .UseReactiveUI()
} .With(new FontManagerOptions
{
DefaultFamilyName = "avares://JiLinApp/Assets/Fonts/msyh.ttf#Microsoft YaHei",
FontFallbacks = new[]
{
new FontFallback
{
FontFamily = new FontFamily("avares://JiLinApp/Assets/Fonts/msyh.ttf#Microsoft YaHei")
}
}
});
} }

7
JiLinApp/ViewModels/MainWindowViewModel.cs

@ -1,7 +0,0 @@
namespace JiLinApp.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
}
}

8
JiLinApp/ViewModels/ViewModelBase.cs

@ -1,8 +0,0 @@
using ReactiveUI;
namespace JiLinApp.ViewModels
{
public class ViewModelBase : ReactiveObject
{
}
}

12
JiLinApp/Views/MainWindow.axaml.cs

@ -1,12 +0,0 @@
using Avalonia.Controls;
namespace JiLinApp.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Loading…
Cancel
Save