using Microsoft.UI.Xaml.Navigation; using XFEWorkflow.Services; namespace XFEWorkflow.Views; public sealed partial class PluginPageHostPage : Page { private readonly Grid root = new(); private PluginPageReference? reference; private CancellationTokenSource? opening; public PluginPageHostPage() { InitializeComponent(); Content = root; } protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (e.Parameter is not PluginPageReference page) return; reference = page; opening = new(); var currentOpening = opening; AppShellPage.Current?.SelectPluginPage(page); AppServices.Current.PluginPages.PageClosed += PageClosed; try { root.Children.Clear(); root.Children.Add(new ProgressRing { IsActive = true, Width = 40, Height = 40 }); var view = await AppServices.Current.PluginPages.OpenAsync(page, currentOpening.Token); if (currentOpening.IsCancellationRequested) return; root.Children.Clear(); root.Children.Add(view); } catch (OperationCanceledException) { } catch (Exception ex) { root.Children.Clear(); root.Children.Add(new TextBlock { Text = "插件页面加载失败:" + AppServices.Current.Redactor.Redact(ex.Message), TextWrapping = TextWrapping.Wrap, Margin = new(24) }); } finally { if (ReferenceEquals(opening, currentOpening)) { opening = null; currentOpening.Dispose(); } } } private void PageClosed(string id) { if (reference?.PageId != id) return; root.Children.Clear(); root.Children.Add(new TextBlock { Text = "插件页面已关闭。请在插件中心重新启用插件。", Margin = new(24) }); } protected override void OnNavigatedFrom(NavigationEventArgs e) { opening?.Cancel(); opening?.Dispose(); opening = null; AppServices.Current.PluginPages.PageClosed -= PageClosed; root.Children.Clear(); base.OnNavigatedFrom(e); } }