c# - LoadState 和 navigationHelper_LoadState 之间的区别

标签 c# windows-store-apps winrt-xaml

我是 Windows 应用商店应用程序开发新手。目前我正在研究使用 c# 在 xaml 之间传递和接收参数。

有人可以通过一些示例帮助解释 LoadState()navigationHelper_LoadState() 之间的区别吗?我应该去哪里接收参数?

最佳答案

因此,NavigationHelper.LoadState 需要两件事:

  1. OnNavigedTo 当该页面即将在框架中显示时调用。
  2. NavigationMode.New 导航到页面的新实例(不是向前或向后)

MSDN说:

In addition to providing the implementations described earlier, NavigationHelper also needs to be called from the OnNavigatedTo() and OnNavigatedFrom() event handlers that are implemented on each page. When these events occur, NavigationHelper calls a page-specific implementation of LoadState() and SaveState(). You can customize the implementation of these functions on each page. They should be used in place of OnNavigatedTo() and OnNavigatedFrom() respectively.

原始代码是:

public void OnNavigatedTo(NavigationEventArgs e)
{
    var frameState = SuspensionManager.SessionStateForFrame(this.Frame);
    this._pageKey = "Page-" + this.Frame.BackStackDepth;
    if (e.NavigationMode == NavigationMode.New)
    {
        var nextPageKey = this._pageKey;
        int nextPageIndex = this.Frame.BackStackDepth;
        while (frameState.Remove(nextPageKey))
        {
            nextPageIndex++;
            nextPageKey = "Page-" + nextPageIndex;
        }
        if (this.LoadState != null)
        {
            this.LoadState(this, new LoadStateEventArgs(e.Parameter, null));
        }
    }
    else
    {
        if (this.LoadState != null)
        {
            this.LoadState(this, new LoadStateEventArgs(e.Parameter, (Dictionary<String, Object>)frameState[this._pageKey]));
        }
    }
}

就你的问题而言,没有 LoadState() 覆盖,除非你定义自己的类似 this blog 。他只是这样做:

private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    LoadState(e);
}

private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
    SaveState(e);
}

protected virtual void LoadState(LoadStateEventArgs e) { }
protected virtual void SaveState(SaveStateEventArgs e) { }

看,它们是相同的。它们之间没有什么不同,除了执行管道可能会稍微影响时序,但可能性不大。最后,没有真正的区别。那些需要使用其中一种而不是另一种的人......他们一定是错误的,将原因归因于其他因素的影响。

祝你好运。

关于c# - LoadState 和 navigationHelper_LoadState 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23993857/

相关文章:

javascript - 如何向 Twitter API 发出 POST 更新请求?

windows-runtime - 导航到其他页面后继续播放的全局 MediaElement

c# - 在 WinRT C# 中,如何使用 RenderTargetBitmap 保存屏幕外 XAML 树?

c# - 从主页绑定(bind)到 Template10 设置

c# - 拆分字符串并忽略引号内的定界符

c# - 有没有免费的代码编辑器WinForms控件?

c# - 分离本地数据库.mdf,复制,附加新文件

c# - 使用 ASP.NET 5 中的 cookie 身份验证重定向到使用属性授权的登录

windows - 使用 windbg 调试 Windows 8 应用程序

c# - NavigateCommand 只调用一次