diff --git a/app/Desktop/Main/Pages/ViewerPage.axaml b/app/Desktop/Main/Pages/ViewerPage.axaml index 3477b1f..b0746b2 100644 --- a/app/Desktop/Main/Pages/ViewerPage.axaml +++ b/app/Desktop/Main/Pages/ViewerPage.axaml @@ -5,7 +5,9 @@ xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages" xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - x:Class="DHT.Desktop.Main.Pages.ViewerPage"> + x:Class="DHT.Desktop.Main.Pages.ViewerPage" + AttachedToVisualTree="OnAttachedToVisualTree" + DetachedFromVisualTree="OnDetachedFromVisualTree"> diff --git a/app/Desktop/Main/Pages/ViewerPage.axaml.cs b/app/Desktop/Main/Pages/ViewerPage.axaml.cs index 8d704e7..49ad191 100644 --- a/app/Desktop/Main/Pages/ViewerPage.axaml.cs +++ b/app/Desktop/Main/Pages/ViewerPage.axaml.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; @@ -12,5 +13,17 @@ namespace DHT.Desktop.Main.Pages { private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } + + public void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) { + if (DataContext is ViewerPageModel model) { + model.SetPageVisible(true); + } + } + + public void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) { + if (DataContext is ViewerPageModel model) { + model.SetPageVisible(false); + } + } } } diff --git a/app/Desktop/Main/Pages/ViewerPageModel.cs b/app/Desktop/Main/Pages/ViewerPageModel.cs index 1aecfcc..72d5686 100644 --- a/app/Desktop/Main/Pages/ViewerPageModel.cs +++ b/app/Desktop/Main/Pages/ViewerPageModel.cs @@ -34,6 +34,8 @@ namespace DHT.Desktop.Main.Pages { private readonly Window window; private readonly IDatabaseFile db; + private bool isPageVisible = false; + [Obsolete("Designer")] public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {} @@ -52,13 +54,20 @@ namespace DHT.Desktop.Main.Pages { FilterModel.Dispose(); } + public void SetPageVisible(bool isPageVisible) { + this.isPageVisible = isPageVisible; + if (isPageVisible) { + UpdateStatistics(); + } + } + private void OnFilterPropertyChanged(object? sender, PropertyChangedEventArgs e) { UpdateStatistics(); HasFilters = FilterModel.HasAnyFilters; } private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) { + if (isPageVisible && e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) { UpdateStatistics(); } }