简体   繁体   English

LoadState中的Frame.Navigate

[英]Frame.Navigate in LoadState

I'm having trouble trying to navigate automatically between pages in my Windows 8.1 app based on a little check. 我在尝试根据一点检查尝试在Windows 8.1应用程序的页面之间自动导航时遇到麻烦。 It just doesn't want to navigate to another page when doing this in LoadState , as if something isn't loaded yet, but it doesn't give an error either. LoadState执行此操作时,它只是不想导航到另一个页面,就好像尚未加载内容一样,但是它也没有给出错误。 When I insert a delay using (for example) await Task.Delay(2000) before doing Frame.Navigate , then my app will redirect without any problem. 当我在执行Frame.Navigate之前使用(例如)使用await Task.Delay(2000)进行Frame.Navigate ,我的应用程序将进行重定向,而不会出现任何问题。

protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
        MyData oData = await getData();

        if (oData != null)
        {
             this.Frame.Navigate(typeof(newPage), oData);
        }
        else
        {
             // do something else
        }

    }

Do I have to put this code in another load- or navigated-event? 我是否必须将此代码放入另一个加载事件或导航事件中? Or how can I make this work? 或者我该如何做?

In LoadState and SaveState you should only save and restore the page state (called when suspending and reactivating the app). LoadStateSaveState您只应保存和恢复页面状态(在挂起和重新激活应用程序时调用)。 Do nothing else (like navigating). 不执行其他操作(例如导航)。

Put your logic into the OnNavigatedTo method instead... 将您的逻辑放入OnNavigatedTo方法中...

If you want to navigate from method that called when page is loads, you should place your navigation code to OnNavigatedTo(...). 如果要从加载页面时调用的方法进行导航,则应将导航代码放置到OnNavigatedTo(...)。 But do not forget to wrap your code in Dispatcher.RunAsync(...) - Frame navigation in xaml return false 但是不要忘了将代码包装在Dispatcher.RunAsync(...)- xaml中的帧导航返回false

I tried calling Frame.Navigate(...) from the OnNavigatedTo method but still the navigation didn't occur. 我尝试从OnNavigatedTo方法调用Frame.Navigate(...),但仍未发生导航。

There are other answers which say use Dispatcher.RunAsync , but that feels like it's making assumptions about the threading model of Windows Phone. 还有其他答案说使用Dispatcher.RunAsync ,但是感觉就像是对Windows Phone的线程模型进行了假设。

Here's what I do: attach a handler to the Loaded event of the page instead, and put my "redirect" logic in there. 我的工作是:将处理程序附加到页面的Loaded事件,然后在其中放置“重定向”逻辑。 Loaded fires after OnNavigateTo and after NavigationHelper_LoadState, but before the page has become visible. 在OnNavigateTo之后和NavigationHelper_LoadState之后但在页面变为可见之前触发加载。

    public LaunchPadPage() {
        this.InitializeComponent();

        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

        this.Loaded += LaunchPadPage_Loaded;

        this.app = (App)App.Current;
    }

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) {
        // Let's show the root zone items
        // NB: In case we don't have this data yet, do nothing
        if (app.Hierarchy != null)
        {
            DefaultViewModel["Items"] = app.Hierarchy.RootItems;
        }
    }

    private void LaunchPadPage_Loaded(object sender, RoutedEventArgs e) {
        // No data? Go to the downloads page instead.
        if (app.Hierarchy == null)
        {
            Frame.Navigate(typeof(DownloadingPage));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM