From 05a0eab829ffe58cd49e87a5ce063f24006a1753 Mon Sep 17 00:00:00 2001 From: "kwan.nguyen" Date: Wed, 30 Oct 2024 18:57:04 +0700 Subject: [PATCH] fix edit page loading --- PCUT/PCUT/Pages/DesignCenter/EditPage.xaml | 3 +-- PCUT/PCUT/Pages/DesignCenter/EditPage.xaml.cs | 8 +++++--- PCUT/PCUT/ViewModels/ComponentImageModel.cs | 7 ++++++- PCUT/PCUT/ViewModels/ImageViewModel.cs | 9 +++++++-- PCUT/PCUT/ViewModels/LoadingModel.cs | 2 +- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml b/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml index 74a2468..6003318 100644 --- a/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml +++ b/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml @@ -35,7 +35,7 @@ ManipulationMode="TranslateX,TranslateY" ManipulationDelta="MainScroll_ManipulationDelta" Loading="MainScroll_Loading"> - + @@ -76,7 +76,6 @@ Source="{x:Bind Source, Mode=OneWay}" Opacity="{x:Bind Deleted, Mode=OneWay, Converter={StaticResource opacityConverter}}" Tapped="Image_Tapped" - ImageOpened="Image_Loaded" Tag="{x:Bind Id}"> diff --git a/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml.cs b/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml.cs index eead20a..285ab3b 100644 --- a/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml.cs +++ b/PCUT/PCUT/Pages/DesignCenter/EditPage.xaml.cs @@ -12,6 +12,7 @@ using PCUT.ViewModels; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 @@ -86,9 +87,9 @@ namespace PCUT.Pages if (_svgData.Data != null) { LoadingModel.SetCount(_svgData.ComponentCount + dataUnloaded); - PreviewModel.SetSource(_svgData.Data); + PreviewModel.SetSource(_svgData.Data, Image_Loaded); foreach (var (id, deleted, component) in _svgData.ComponentData) - ComponentModel.AddSource(id, deleted, component); + ComponentModel.AddSource(id, deleted, component, Image_Loaded); } if (!UserContext.Instance.IsPermittedEdit) _designCenterModel.SelectedHeightIndex = 0; @@ -157,9 +158,10 @@ namespace PCUT.Pages ((ComponentStyle)Resources["componentStyle"]).SetSize(ComponentColumnDefinition, rate: 3); } - private void Image_Loaded(object sender, RoutedEventArgs e) + private void Image_Loaded(SvgImageSource sender, SvgImageSourceOpenedEventArgs e) { LoadingModel.DecreaseCount(); + sender.Opened -= Image_Loaded; } private void AllImageLoaded(object sender, PropertyChangedEventArgs e) diff --git a/PCUT/PCUT/ViewModels/ComponentImageModel.cs b/PCUT/PCUT/ViewModels/ComponentImageModel.cs index 71e493f..da2bce6 100644 --- a/PCUT/PCUT/ViewModels/ComponentImageModel.cs +++ b/PCUT/PCUT/ViewModels/ComponentImageModel.cs @@ -3,6 +3,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Xml.Linq; using PCUT.Extensions; +using Windows.Foundation; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; @@ -13,9 +14,13 @@ namespace PCUT.ViewModels { public ObservableCollection ComponentList { get; } = new ObservableCollection(); - public void AddSource(string id, bool deleted, XDocument doc) + public void AddSource(string id, bool deleted, XDocument doc, TypedEventHandler eventHandler = null) { var source = new SvgImageSource(); + if (eventHandler != null) + { + source.Opened += eventHandler; + } ComponentList.Add(new ComponentImageSource { Id = id, Deleted = deleted, Source = source }); _ = source.LoadSvgAsync(doc); } diff --git a/PCUT/PCUT/ViewModels/ImageViewModel.cs b/PCUT/PCUT/ViewModels/ImageViewModel.cs index 7709d35..497d957 100644 --- a/PCUT/PCUT/ViewModels/ImageViewModel.cs +++ b/PCUT/PCUT/ViewModels/ImageViewModel.cs @@ -1,6 +1,7 @@ using System.ComponentModel; using System.Xml.Linq; using PCUT.Extensions; +using Windows.Foundation; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; @@ -11,7 +12,7 @@ namespace PCUT.ViewModels public event PropertyChangedEventHandler PropertyChanged; private ImageSource _source; - private ImageSource _defaultSource = new SvgImageSource(); + private SvgImageSource _defaultSource = new SvgImageSource(); public ImageSource Source { @@ -31,9 +32,13 @@ namespace PCUT.ViewModels Source = source; } - public void SetSource(XDocument doc) + public void SetSource(XDocument doc, TypedEventHandler eventHandler = null) { Source = _defaultSource; + if (eventHandler != null) + { + _defaultSource.Opened += eventHandler; + } if (doc != null) _ = _source.LoadSvgAsync(doc); } diff --git a/PCUT/PCUT/ViewModels/LoadingModel.cs b/PCUT/PCUT/ViewModels/LoadingModel.cs index ff62af0..5c1feca 100644 --- a/PCUT/PCUT/ViewModels/LoadingModel.cs +++ b/PCUT/PCUT/ViewModels/LoadingModel.cs @@ -8,7 +8,7 @@ namespace PCUT.ViewModels public event PropertyChangedEventHandler PropertyChanged; private int _count; - public int Count { get; } + public int Count { get => _count; } public void SetCount(int count) {