Added features:Ursa.ReactiveUIExtension

This commit is contained in:
望尘空忧
2024-09-20 15:36:38 +08:00
parent a3b3a11644
commit 4c50d28283
5 changed files with 156 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
using Avalonia.Metadata;
[assembly: XmlnsDefinition("https://irihi.tech/ursa", "Ursa.ReactiveUIExtension")]

View File

@@ -0,0 +1,63 @@
using Avalonia;
using ReactiveUI;
using Ursa.Controls;
/* These codes are ported from Avalonia.ReactiveUI.
* **/
namespace Ursa.ReactiveUIExtension;
/// <summary>
/// A ReactiveUI <see cref="UrsaView"/> that implements the <see cref="IViewFor{TViewModel}"/> interface and
/// will activate your ViewModel automatically if the view model implements <see cref="IActivatableViewModel"/>.
/// When the DataContext property changes, this class will update the ViewModel property with the new DataContext
/// value, and vice versa.
/// </summary>
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
public class ReactiveUrsaView<TViewModel> : UrsaView, IViewFor<TViewModel> where TViewModel : class
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1002", Justification = "Generic avalonia property is expected here.")]
public static readonly StyledProperty<TViewModel?> ViewModelProperty = AvaloniaProperty
.Register<ReactiveUrsaView<TViewModel>, TViewModel?>(nameof(ViewModel));
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveUrsaView{TViewModel}"/> class.
/// </summary>
public ReactiveUrsaView()
{
// This WhenActivated block calls ViewModel's WhenActivated
// block if the ViewModel implements IActivatableViewModel.
this.WhenActivated(disposables => { });
}
/// <summary>
/// The ViewModel.
/// </summary>
public TViewModel? ViewModel
{
get => GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
object? IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel?)value;
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == DataContextProperty) {
if (ReferenceEquals(change.OldValue, ViewModel)
&& change.NewValue is null or TViewModel) {
SetCurrentValue(ViewModelProperty, change.NewValue);
}
}
else if (change.Property == ViewModelProperty) {
if (ReferenceEquals(change.OldValue, DataContext)) {
SetCurrentValue(DataContextProperty, change.NewValue);
}
}
}
}

View File

@@ -0,0 +1,63 @@
using Avalonia;
using ReactiveUI;
using Ursa.Controls;
/* These codes are ported from Avalonia.ReactiveUI.
* **/
namespace Ursa.ReactiveUIExtension;
/// <summary>
/// A ReactiveUI <see cref="UrsaWindow"/> that implements the <see cref="IViewFor{TViewModel}"/> interface and will
/// activate your ViewModel automatically if the view model implements <see cref="IActivatableViewModel"/>. When
/// the DataContext property changes, this class will update the ViewModel property with the new DataContext value,
/// and vice versa.
/// </summary>
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
public class ReactiveUrsaWindow<TViewModel> : UrsaWindow, IViewFor<TViewModel> where TViewModel : class
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1002", Justification = "Generic avalonia property is expected here.")]
public static readonly StyledProperty<TViewModel?> ViewModelProperty = AvaloniaProperty
.Register<ReactiveUrsaWindow<TViewModel>, TViewModel?>(nameof(ViewModel));
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveUrsaWindow{TViewModel}"/> class.
/// </summary>
public ReactiveUrsaWindow()
{
// This WhenActivated block calls ViewModel's WhenActivated
// block if the ViewModel implements IActivatableViewModel.
this.WhenActivated(disposables => { });
}
/// <summary>
/// The ViewModel.
/// </summary>
public TViewModel? ViewModel
{
get => GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
object? IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel?)value;
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == DataContextProperty) {
if (ReferenceEquals(change.OldValue, ViewModel)
&& change.NewValue is null or TViewModel) {
SetCurrentValue(ViewModelProperty, change.NewValue);
}
}
else if (change.Property == ViewModelProperty) {
if (ReferenceEquals(change.OldValue, DataContext)) {
SetCurrentValue(DataContextProperty, change.NewValue);
}
}
}
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Authors>WCKFWCKY</Authors>
<PackageId>Irihi.Ursa.ReactiveUIExtension</PackageId>
<PackageIcon>irihi.png</PackageIcon>
<PackageProjectUrl>https://github.com/irihitech/Ursa.Avalonia</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Description>This is a Ursa expansion. This package integrates and is compatible with UrsaWindow and UrsaView with Avalonia.ReactiveUI. [Avalonia.ReactiveU See: https://docs.avaloniaui.net/zh-Hans/docs/concepts/reactiveui/]
这个是一个Ursa拓展包。这个包整合并互相兼容了UrsaWindow和UrsaView与Avalonia.ReactiveUI的功能。【Avalonia.ReactiveU参见https://docs.avaloniaui.net/docs/concepts/reactiveui/】</Description>
<Version>1.0.1</Version>
<Copyright>WCKFWCKY</Copyright>
<RepositoryUrl>https://github.com/irihitech/Ursa.Avalonia</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia.ReactiveUI" Version="11.1.1" />
<PackageReference Include="Irihi.Ursa" Version="1.3.0" />
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB