XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Storage.Pickers;
using XFEWorkflow.Services;

namespace XFEWorkflow.Views;

public class ManagementPage : Page
{
    protected AppServices Services => AppServices.Current;
    protected Grid Workspace { get; } = new();
    protected StackPanel HeaderActions { get; } = new() { Orientation = Orientation.Horizontal, Spacing = 8, VerticalAlignment = VerticalAlignment.Center };
    protected Controls.AdaptiveWrapPanel Toolbar { get; } = new();
    protected TextBlock Status { get; } = new() { Style = (Style)Application.Current.Resources["ManagementCaptionStyle"] };
    protected InfoBar ErrorBar { get; } = new() { Severity = InfoBarSeverity.Error, IsClosable = true, Visibility = Visibility.Collapsed };
    protected Grid PageLayout { get; } = new() { RowSpacing = 14, Margin = new(0, 0, 0, 16) };
    public ManagementPage() : this("", "") { }
    protected ManagementPage(string title, string description)
    {
        var header = new Grid { ColumnSpacing = 16, RowSpacing = 10 };
        header.ColumnDefinitions.Add(new()); header.ColumnDefinitions.Add(new() { Width = GridLength.Auto });
        header.RowDefinitions.Add(new() { Height = GridLength.Auto }); header.RowDefinitions.Add(new() { Height = GridLength.Auto });
        var heading = new StackPanel { Spacing = 4 };
        heading.Children.Add(new TextBlock { Text = title, FontSize = 26, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold });
        heading.Children.Add(Caption(description)); header.Children.Add(heading); Grid.SetColumn(HeaderActions, 1); header.Children.Add(HeaderActions);
        header.SizeChanged += (_, e) => { var narrow = e.NewSize.Width < 720; Grid.SetColumn(HeaderActions, narrow ? 0 : 1); Grid.SetRow(HeaderActions, narrow ? 1 : 0); Grid.SetColumnSpan(HeaderActions, narrow ? 2 : 1); HeaderActions.HorizontalAlignment = narrow ? HorizontalAlignment.Left : HorizontalAlignment.Right; };
        foreach (var size in new[] { GridLength.Auto, GridLength.Auto, new GridLength(1, GridUnitType.Star), GridLength.Auto, GridLength.Auto }) PageLayout.RowDefinitions.Add(new() { Height = size });
        PageLayout.Children.Add(header); Grid.SetRow(Toolbar, 1); PageLayout.Children.Add(Toolbar); Grid.SetRow(Workspace, 2); PageLayout.Children.Add(Workspace);
        Grid.SetRow(Status, 3); PageLayout.Children.Add(Status); Grid.SetRow(ErrorBar, 4); PageLayout.Children.Add(ErrorBar);
        ErrorBar.Closed += (_, _) => { if (!ErrorBar.IsOpen) ErrorBar.Visibility = Visibility.Collapsed; }; Content = PageLayout;
    }
    protected Button ActionButton(string text, Func<Task> action, string? id = null, bool accent = false)
    {
        var button = new Button { Content = text, VerticalAlignment = VerticalAlignment.Center };
        if (id is not null) Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(button, id);
        if (accent) button.Style = (Style)Application.Current.Resources["AccentButtonStyle"];
        button.Click += async (_, _) => { button.IsEnabled = false; try { await Guard(action); } finally { button.IsEnabled = true; } }; return button;
    }
    protected async Task Guard(Func<Task> action)
    { try { await Services.Ready; ErrorBar.IsOpen = false; ErrorBar.Visibility = Visibility.Collapsed; await action(); } catch (Exception ex) { ErrorBar.Message = Services.Redactor.Redact(ex.Message); ErrorBar.Visibility = Visibility.Visible; ErrorBar.IsOpen = true; Status.Text = "操作未完成"; } }
    protected static StackPanel Row(params UIElement[] controls)
    { var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8 }; foreach (var control in controls) row.Children.Add(control); return row; }
    protected static TextBlock Caption(string text) => new() { Text = text, Style = (Style)Application.Current.Resources["ManagementCaptionStyle"] };
    protected static TextBlock Label(string text) => new() { Text = text, TextWrapping = TextWrapping.Wrap, IsTextSelectionEnabled = true };
    protected static Border Card(UIElement child) => new() { Style = (Style)Application.Current.Resources["ManagementCardStyle"], Child = child };
    protected async Task<string?> PickAsync(string extension)
    {
        var picker = new FileOpenPicker(); picker.FileTypeFilter.Add(extension);
        WinRT.Interop.InitializeWithWindow.Initialize(picker, WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow));
        return (await picker.PickSingleFileAsync())?.Path;
    }
    protected Task ShowTextAsync(string title, string text) => new ContentDialog
    {
        Title = title, Content = new TextBox { AcceptsReturn = true, Text = text, IsReadOnly = true, TextWrapping = TextWrapping.Wrap, MinWidth = Math.Min(480, Math.Max(240, XamlRoot.Size.Width - 100)), MaxHeight = 460, FontFamily = new FontFamily("Consolas") },
        CloseButtonText = "关闭", XamlRoot = XamlRoot
    }.ShowAsync().AsTask();
}

// Legacy credential navigation forwards to settings.
public sealed partial class CredentialsPage : Page
{
    public CredentialsPage() { InitializeComponent(); Loaded += (_, _) => AppShellPage.Current!.ViewModel.NavigationViewService.NavigateTo<SettingPage>("credentials"); }
}
public sealed partial class BrowserPage : Page
{
    public BrowserPage() => InitializeComponent();
    protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); AppShellPage.Current?.ShowBrowser(true); }
    protected override void OnNavigatedFrom(NavigationEventArgs e) { AppShellPage.Current?.ShowBrowser(false); base.OnNavigatedFrom(e); }
}