From c339ca5d3f82656d0f8798fda788f8d61f8e1e0a Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Sat, 14 Sep 2024 18:47:59 +0200 Subject: [PATCH] more reactive UI work --- Stars Assistant/Stars Assistant.csproj | 1 + .../ViewModels/MainWindowViewModel.cs | 21 +++++++++++++++++++ Stars Assistant/Views/MainWindow.axaml | 4 ++-- Stars Assistant/Views/MainWindow.axaml.cs | 19 ++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Stars Assistant/Stars Assistant.csproj b/Stars Assistant/Stars Assistant.csproj index 9e540ba..ef96db4 100644 --- a/Stars Assistant/Stars Assistant.csproj +++ b/Stars Assistant/Stars Assistant.csproj @@ -27,6 +27,7 @@ + diff --git a/Stars Assistant/ViewModels/MainWindowViewModel.cs b/Stars Assistant/ViewModels/MainWindowViewModel.cs index 81bb73b..81c8068 100644 --- a/Stars Assistant/ViewModels/MainWindowViewModel.cs +++ b/Stars Assistant/ViewModels/MainWindowViewModel.cs @@ -1,5 +1,8 @@ using System.Collections.ObjectModel; +using System.Reactive.Disposables; +using System.Reactive.Linq; using ReactiveUI; +using ReactiveUI.SourceGenerators; namespace StarsAssistant.ViewModels; @@ -7,6 +10,24 @@ public partial class MainWindowViewModel : ViewModelBase, IActivatableViewModel { 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 Planets { get; } = PlanetViewModel.LoadAll(); } diff --git a/Stars Assistant/Views/MainWindow.axaml b/Stars Assistant/Views/MainWindow.axaml index 13afb4c..1f2e8a9 100644 --- a/Stars Assistant/Views/MainWindow.axaml +++ b/Stars Assistant/Views/MainWindow.axaml @@ -19,10 +19,10 @@ - Welcome to Stars Assistant! + Hi - { 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(); } + } \ No newline at end of file