c# - 检测是否在 NavigationContext 中调用了 GoBack()

标签 c# .net wpf prism

在实现 IConfirmNavigationRequest 的 View 中,我使用了四种导航过渡动画:ForwardIn、ForwardOut、BackwardIn 和 BackwardOut。

我正在使用 ConfirmNavigationRequest 等待 Out 转换完成后再导航。但是,根据导航的类型——使用 RequestNavigate() 向前或使用 GoBack() 向后——我想播放不同的 Storyboard:

    public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
    {
        Storyboard storyboard;

        if (navigationContext./* was RequestNavigate() or GoBack() called? */)
        {
            // GoBack navigation request.
            storyboard = ((Storyboard)FindResource(RegionTransitions.BackwardOut));
        }
        else
        {
            // Forward navigation request.
            storyboard = ((Storyboard)FindResource(RegionTransitions.ForwardOut));
        }

        storyboard.Completed += (sender, args) => continuationCallback(true);
        storyboard.Begin();
    }

是否有任何准确且可预测的方法来检测从 NavigationContext 使用了哪种类型的导航?

编辑:我已经放弃使用 IConfirmNavigation 进行过渡,现在使用 AnimatedContentControl。

我不太喜欢它,但我正在发布一个事件来“警告”AnimatedContentControl 下一个导航应该使用特定的转换:

_eventAggregator.GetEvent<RegionTransitionEvent>().Publish(new RegionTransitionEventArgs { RegionName = RegionNames.NavRegion, RegionTransition = RegionTransitions.BackwardIn });

_regionManager.RequestNavigate(RegionNames.ContentRegion, ViewNames.ABC);

最佳答案

您可以在 View 或 View 模型中实现 INavigationAware 接口(interface)。

该接口(interface)将实现 3 个方法。

public interface INavigationAware
{
    void OnNavigatedTo(NavigationContext navigationContext);
    bool IsNavigationTarget(NavigationContext navigationContext);
    void OnNavigatedFrom(NavigationContext navigationContext);
}

这将允许您确定它是否是往返某处的导航。您甚至可以确定导航来自哪个 View 。

关于c# - 检测是否在 NavigationContext 中调用了 GoBack(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707749/

相关文章:

c# - 在 Web API 中创建 POST 方法

c# - 异常和返回语句是 C# 中唯一可能的提前退出吗?

c# - 在 WPF 中限制 "Auto"和 "1*"上的行高

c# - 在 ASP.NET 下拉值更改时显示/隐藏 DIV

c# - numpy array[0, :] *= 1. 23 的 MathNet 等价物是什么

C# 手动停止异步 for 语句(打字机效果)

WPF Canvas - 类似 Split View的 Visual Studio

c# - 以编程方式更改 WPF 中的按钮图标

c# - 无法解析/使用 System.ServiceModel.Security.WSTrustServiceContract 作为服务名称

c# - 使用文本框在数据库中插入 bool 值