feat: add mvvm demo.
This commit is contained in:
@@ -18,7 +18,10 @@ public partial class App : Application
|
||||
{
|
||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow = new MainSplashWindow();
|
||||
desktop.MainWindow = new MvvmSplashWindow()
|
||||
{
|
||||
DataContext = new SplashViewModel()
|
||||
};
|
||||
}
|
||||
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)
|
||||
{
|
||||
|
||||
38
demo/Ursa.Demo/ViewModels/SplashViewModel.cs
Normal file
38
demo/Ursa.Demo/ViewModels/SplashViewModel.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Irihi.Avalonia.Shared.Contracts;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public partial class SplashViewModel: ObservableObject, IDialogContext
|
||||
{
|
||||
[ObservableProperty] private double _progress;
|
||||
private Random _r = new();
|
||||
|
||||
public SplashViewModel()
|
||||
{
|
||||
DispatcherTimer.Run(OnUpdate, TimeSpan.FromMilliseconds(200), DispatcherPriority.Default);
|
||||
}
|
||||
|
||||
private bool OnUpdate()
|
||||
{
|
||||
Progress += 10 * _r.NextDouble();
|
||||
if (Progress <= 100)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
RequestClose?.Invoke(this, null);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
RequestClose?.Invoke(this, null);
|
||||
}
|
||||
|
||||
public event EventHandler<object?>? RequestClose;
|
||||
}
|
||||
48
demo/Ursa.Demo/Views/MvvmSplashWindow.axaml
Normal file
48
demo/Ursa.Demo/Views/MvvmSplashWindow.axaml
Normal file
@@ -0,0 +1,48 @@
|
||||
<u:SplashWindow xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
xmlns:viewModels="clr-namespace:Ursa.Demo.ViewModels"
|
||||
xmlns:iri="https://irihi.tech/shared"
|
||||
mc:Ignorable="d"
|
||||
Width="400" Height="400"
|
||||
CountDown="{x:Null}"
|
||||
x:DataType="viewModels:SplashViewModel"
|
||||
x:Class="Ursa.Demo.Views.MvvmSplashWindow"
|
||||
Title="MvvmSplashWindow">
|
||||
<Grid
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
ColumnDefinitions="Auto, Auto"
|
||||
RowDefinitions="Auto, Auto, Auto">
|
||||
<iri:IrihiLogo
|
||||
Grid.Row="0"
|
||||
Grid.Column="0"
|
||||
Width="64"
|
||||
Margin="0,0,16,0"
|
||||
Fill="{DynamicResource SemiGrey5}" />
|
||||
<StackPanel Grid.Row="0" Grid.Column="1">
|
||||
<TextBlock
|
||||
Classes="H2"
|
||||
Text="铱泓科技"
|
||||
Theme="{DynamicResource TitleTextBlock}" />
|
||||
<TextBlock FontWeight="Bold" Text="IRIHI Technology" />
|
||||
</StackPanel>
|
||||
<ProgressBar Grid.Row="1" Grid.ColumnSpan="2" Value="{Binding Progress}" Margin="0 16 0 0"/>
|
||||
<StackPanel
|
||||
Grid.Row="2"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="2">
|
||||
<TextBlock
|
||||
Margin="0,12,0,0"
|
||||
HorizontalAlignment="Center"
|
||||
FontSize="14"
|
||||
Text="聚焦生产力的美学进化" />
|
||||
<TextBlock
|
||||
HorizontalAlignment="Center"
|
||||
FontSize="14"
|
||||
Text="Aesthetic Evolution of Productivity" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</u:SplashWindow>
|
||||
24
demo/Ursa.Demo/Views/MvvmSplashWindow.axaml.cs
Normal file
24
demo/Ursa.Demo/Views/MvvmSplashWindow.axaml.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
namespace Ursa.Demo.Views;
|
||||
|
||||
public partial class MvvmSplashWindow : SplashWindow
|
||||
{
|
||||
public MvvmSplashWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override async Task<Window> CreateNextWindow()
|
||||
{
|
||||
return new MainWindow()
|
||||
{
|
||||
DataContext = new MainViewViewModel()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Avalonia.Controls;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Irihi.Avalonia.Shared.Contracts;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -18,6 +19,24 @@ public abstract class SplashWindow: Window
|
||||
get => GetValue(CountDownProperty);
|
||||
set => SetValue(CountDownProperty, value);
|
||||
}
|
||||
|
||||
static SplashWindow()
|
||||
{
|
||||
DataContextProperty.Changed.AddClassHandler<SplashWindow, object?>((window, e) =>
|
||||
window.OnDataContextChange(e));
|
||||
}
|
||||
|
||||
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
|
||||
{
|
||||
if (args.OldValue.Value is IDialogContext oldContext) oldContext.RequestClose -= OnContextRequestClose;
|
||||
|
||||
if (args.NewValue.Value is IDialogContext newContext) newContext.RequestClose += OnContextRequestClose;
|
||||
}
|
||||
|
||||
private void OnContextRequestClose(object? sender, object? args)
|
||||
{
|
||||
Close(args);
|
||||
}
|
||||
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
@@ -49,6 +68,11 @@ public abstract class SplashWindow: Window
|
||||
}
|
||||
nextWindow.Show();
|
||||
Close();
|
||||
if (DataContext is IDialogContext idc)
|
||||
{
|
||||
idc.Close();
|
||||
idc.RequestClose -= OnContextRequestClose;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user