more reactive UI work

This commit is contained in:
Torben Nehmer 2024-09-14 18:47:59 +02:00
parent 17f890cb9f
commit c339ca5d3f
4 changed files with 42 additions and 3 deletions

View File

@ -27,6 +27,7 @@
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.10" /> <PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.10" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.3" /> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageReference Include="ReactiveUI.SourceGenerators" Version="1.0.3" />
<PackageReference Include="Splat" Version="15.1.1" /> <PackageReference Include="Splat" Version="15.1.1" />
<PackageReference Include="System.Reactive" Version="6.0.1" /> <PackageReference Include="System.Reactive" Version="6.0.1" />
</ItemGroup> </ItemGroup>

View File

@ -1,5 +1,8 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.SourceGenerators;
namespace StarsAssistant.ViewModels; namespace StarsAssistant.ViewModels;
@ -7,6 +10,24 @@ public partial class MainWindowViewModel : ViewModelBase, IActivatableViewModel
{ {
public ViewModelActivator Activator { get; } = new ViewModelActivator(); public ViewModelActivator Activator { get; } = new ViewModelActivator();
[ObservableAsProperty]
private string _welcomeMessage;
public MainWindowViewModel()
{
_welcomeMessage = "Lorem Ipsum";
_welcomeMessageHelper = Observable.Return("Dolor sit amet")
.ToProperty(this, x => x.WelcomeMessage);
this.WhenActivated((CompositeDisposable disposables) =>
{
// /* handle activation */
// Disposable
// .Create(() => { /* handle deactivation */ })
// .DisposeWith(disposables);
});
}
public ObservableCollection<PlanetViewModel> Planets { get; } = PlanetViewModel.LoadAll(); public ObservableCollection<PlanetViewModel> Planets { get; } = PlanetViewModel.LoadAll();
} }

View File

@ -19,10 +19,10 @@
<TabControl> <TabControl>
<TabItem Header="Home"> <TabItem Header="Home">
Welcome to Stars Assistant! <TextBlock x:Name="WelcomeText">Hi</TextBlock>
</TabItem> </TabItem>
<TabItem Header="DataGrid"> <TabItem Header="DataGrid">
<DataGrid ItemsSource="{Binding Planets}" <DataGrid x:Name="PlanetsGrid"
AutoGenerateColumns="True" IsReadOnly="True" AutoGenerateColumns="True" IsReadOnly="True"
GridLinesVisibility="All" GridLinesVisibility="All"
FrozenColumnCount="1" FrozenColumnCount="1"

View File

@ -4,6 +4,8 @@ using System.Diagnostics;
using System; using System;
using StarsAssistant.ViewModels; using StarsAssistant.ViewModels;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using ReactiveUI;
using System.Reactive.Disposables;
namespace StarsAssistant.Views; namespace StarsAssistant.Views;
@ -11,6 +13,21 @@ public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
{ {
public MainWindow() public MainWindow()
{ {
AvaloniaXamlLoader.Load(this); this.WhenActivated(
disposables => {
this.OneWayBind(ViewModel,
viewModel => viewModel.Planets,
view => view.PlanetsGrid.ItemsSource)
.DisposeWith(disposables);
this.OneWayBind(ViewModel,
vm => vm.WelcomeMessage,
v => v.WelcomeText.Text)
.DisposeWith(disposables);
} }
);
InitializeComponent();
}
} }