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

XFEExtension.NetCore.WinUIHelper

【DLL】WinUI的各种工具类,帮助开发者快速构建一个模块化的WinUI项目

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using XFEExtension.NetCore.WinUIHelper.Interface.Services;
using XFEExtension.NetCore.WinUIHelper.Utilities;
using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;

namespace XFEExtension.NetCore.WinUIHelper.Implements.Services;

internal class AutoNavigationService : GlobalServiceBase, IAutoNavigationService
{
    private Frame? frame;
    public bool CanGoBack => frame is not null && frame.CanGoBack;
    public bool CanGoForward => frame is not null && frame.CanGoForward;
    public Frame? Frame
    {
        get => frame;
        set
        {
            if (value is not null)
            {
                value.Navigated += (s, e) => Navigated?.Invoke(s, e);
                if (frame is not null)
                    frame.Navigated -= (s, e) => Navigated?.Invoke(s, e);
                frame = value;
            }
        }
    }

    public event NavigatedEventHandler? Navigated;

    public void GoBack() => frame?.GoBack();

    public void GoForward() => frame?.GoForward();

    public void NavigateTo(string pageType, object? parameter = null, NavigationTransitionInfo? navigationTransitionInfo = null)
    {
        if (PageManager.PageDefinitions.TryGetValue(pageType, out var type))
            NavigateTo(type, parameter, navigationTransitionInfo);
    }

    public void NavigateTo(Type type, object? parameter = null, NavigationTransitionInfo? navigationTransitionInfo = null)
    {
        if (frame?.SourcePageType != type || frame.Content is Page page && !Equals(page.GetParameter(), parameter))
            frame?.Navigate(type, parameter, navigationTransitionInfo);
    }

    public void NavigateTo<T>(object? parameter = null, NavigationTransitionInfo? navigationTransitionInfo = null) where T : Page => NavigateTo(typeof(T), parameter, navigationTransitionInfo);
}