简体   繁体   English

从辅助磁贴导航到页面时如何管理后退堆栈

[英]How to manage back stack when navigating to page from secondary tile

I have implemented secondary tiles in my application, so a user is able to pin a secondary tile to the start screen, and then navigate to a respective page in my application accordingly. 我已经在应用程序中实现了辅助磁贴,因此用户可以将辅助磁贴固定到开始屏幕,然后相应地导航到应用程序中的相应页面。 This implementation works ok, except for when the user hits the back hardware button after navigating to a specific page from the pinned secondary tile, nothing happens? 此实现可以正常运行,除了当用户从固定的辅助磁贴导航到特定页面后单击后退硬件按钮时,什么都没有发生? In fact, for a quick second the previous page in the application is actually shown, although the user is coming from the start screen. 实际上,尽管用户来自开始屏幕,但很快就显示了应用程序中的上一页。 What would be the proper method to actually return to the start screen as the user would expect would happen (I am assuming this would be the proper back stack navigation)? 如用户期望的那样,实际返回到开始屏幕的正确方法是什么(我假设这将是正确的后退堆栈导航)?

What I have is as follows, but only works during normal page navigation scenarios, not when the user is navigating to the SharePage from the start screen pinned tile. 我所拥有的内容如下,但仅在正常的页面导航方案中有效,而当用户从“开始”屏幕固定图块导航到SharePage时,则无效。

MainPage.xaml.cs MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        string _title = null;
        NavigationContext.QueryString.TryGetValue("Param", out _title);

        if (_title != null)
        {
            switch (_title)
            {
                case "status":
                    this.NavigationService.Navigate(new Uri("/Views/ShareStatusPage.xaml", UriKind.Relative));
                    break;
                case "link":
                    this.NavigationService.Navigate(new Uri("/Views/ShareLinkPage.xaml", UriKind.Relative));
                    break;
            }
        }            
    }

private void CreateLiveTile(TileItem item)
    {
        string tileParameter = "Param=" + item.Title.ToString();

        ShellTile Tile = CheckIfTileExist(tileParameter);  // Check if Tile's title has been used 

        if (Tile == null)
        {
            try
            {
                var LiveTile = new StandardTileData
                {
                    Title = item.TileName,
                    //BackgroundImage = ((System.Windows.Media.Imaging.BitmapImage)hubtile.Source).UriSource,
                    BackgroundImage = new Uri(item.ImageUri.ToString(), UriKind.Relative),
                    //Count = 1,
                    BackTitle = item.TileName,
                    //BackBackgroundImage = new Uri("", UriKind.Relative),
                    BackContent = item.Message,
                };

                ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), LiveTile);  //pass the tile parameter as the QueryString

            }
            catch (Exception)
            {
                MessageBox.Show("This tile could not be pinned", "Warning", MessageBoxButton.OK);
            }
        }
        else
        {
            MessageBox.Show("This tile has already been pinned", "Notice", MessageBoxButton.OK);
        }
    }

private ShellTile CheckIfTileExist(string tileUri)
    {
        ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(tile => tile.NavigationUri.ToString().Contains(tileUri));
        return shellTile;
    }

SharePage.xaml.cs SharePage.xaml.cs

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        base.OnBackKeyPress(e);

        //return to the previous page in the phones back stack?
        if (NavigationService.CanGoBack)
        {
            e.Cancel = true;
            NavigationService.GoBack();
        }
        //else
        //{
        //    ??
        //}
    }

So far, CreateLiveTile() method creates the secondary tile and then when that tile is pressed, MainPage is navigated to and then the querystring is checked in the MainPage OnNavigatedTo event and the respective page is then loaded based on what secondary tile was clicked. 到目前为止, CreateLiveTile()方法创建了辅助磁贴,然后在按下该磁贴时,将MainPage导航到,然后在MainPage OnNavigatedTo事件中检查查询字符串,然后根据单击的辅助磁贴加载相应的页面。 Once this is performed and the respective pages has loaded, I can no longer press the back button to get back to the start screen to follow the standard backstack behavior. 一旦执行了此操作并加载了相应的页面,我将无法再按“后退”按钮返回到开始屏幕以遵循标准的后退行为。 How can I fix this? 我怎样才能解决这个问题?

否则您可以使用NavigationService.Navigate(homepageUri)

Why do you cancel a navigaion? 为什么要取消导航? Just remove OnBackKeyPress . 只需删除OnBackKeyPress You don't need it in this scenario. 在这种情况下,您不需要它。

Create a variable to track whether the navigation to the main page came from a navigation through a secondary tile. 创建一个变量以跟踪到主页的导航是否来自通过辅助磁贴的导航。 On MainPage.Load check that variable, and if the variable came through a secondary tile, remove the prior page from the back stack. 在MainPage.Load上检查该变量,如果该变量通过辅助磁贴来,请从后堆栈中删除前一页。 It makes sense to leave from the main page back to the start menu, rather than go back through the secondary tile page. 从主页返回到开始菜单,而不是回到第二磁贴页面是有意义的。 This is how MyStocks Portfolio (no, not one of my apps, but one I l 这就是MyStocks Portfolio(不,不是我的一个应用程序,而是一个我的应用程序)

Here's a good blog on back button use: http://blogs.msdn.com/b/ptorr/archive/2011/10/06/back-means-back-not-forwards-not-sideways-but-back.aspx 这是一个关于使用后退按钮的好博客: http : //blogs.msdn.com/b/ptorr/archive/2011/10/06/back-means-back-not-forwards-not-sideways-but-back.aspx

According to your new update, lets have a look at this method: 根据您的新更新,让我们看看这种方法:

private void CreateLiveTile(TileItem item)
{
    string tileParameter = "Param=" + item.Title.ToString();
     //...

    if (Tile == null)
    {
        try
        {
            var LiveTile = new StandardTileData
            {
              //...
            };

            ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), LiveTile);  //pass the tile parameter as the QueryString

          //blah-blah-blah
}

Here you create a new tile and pass tileParameter to a MainPage . 在这里,您将创建一个新的tile并将tileParameter传递给MainPage So, you navigate to main page, then detect the text in tile parameter, and navigate to the ShareLink or ShareStatus pages. 因此,您导航到主页,然后检测tile参数中的文本,然后导航到ShareLinkShareStatus页面。 That's why you have a dirty navigation stack. 这就是为什么您的导航堆栈很脏。

Let me suggest you a way to avoid this situation: 让我建议您一种避免这种情况的方法:

private void CreateLiveTile(TileItem item)
    {

    var title =  item.Title.ToString();
     //...

    if (Tile == null)
    {
        try
        {
            var LiveTile = new StandardTileData
            {
              //...
            };
             string page;
        switch (title)
        {
            case "status":
                page = "/Views/ShareStatusPage.xaml";
                break;
            case "link":
                page = "/Views/ShareLinkPage.xaml");
                break;
        }                
        if(string.IsNullOrEmpty(page))
         { 
             //handle this situation. for example: page = "/MainPage.xaml";
         }
            ShellTile.Create(new Uri(page, UriKind.Relative), LiveTile); 

          //blah-blah-blah
}

When user taps on your secondary tile he will be navigated directly to ShareLink or ShareStatus page. 当用户点击您的辅助图块时,他将直接导航到ShareLinkShareStatus页面。 And a NavigationStack will be clean. 而且NavigationStack将很干净。 When the user press Back button, the application will be closed and user will see a start screen (this is a right back button behaviour for secondary tiles). 当用户按下“ Back按钮时,该应用程序将关闭,并且用户将看到“开始”屏幕(这是辅助磁贴的右后退按钮行为)。

ps Don't forget to start all your services or load all resources if you have ones. ps如果有服务,别忘了启动所有服务或加载所有资源。 Because MainPage won't be created! 因为不会创建MainPage! Anyway every page of your application has to be able to itnitialize whole application, because you must support restoring from tombstoned state . 无论如何,应用程序的每个页面都必须能够初始化整个应用程序,因为您必须支持从逻辑删除状态还原。

Feel free to ask for details if you need. 如果需要,请随时询问详细信息。

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

相关问题 如何在导航和返回页面时保留文本字段数据 - How to keep textfield data when navigating away and back to page 从桌面应用程序创建磁贴/辅助磁贴 - Create tile / secondary tile from a desktop app 从我的添加页面导航回来时,如何在我的 FlyoutContentTemplate 视图 model 上触发数据刷新? - How do I trigger a data refresh on my FlyoutContentTemplate view model when navigating back from my add page? 如何在导航回页面后保存页面上的内容 - How to save content on the page after navigating on it back 如何从隔离存储加载辅助磁贴的图像 - How to load image from isolated storage for secondary tile 导航时如何重新加载页面 - How to reload page when navigating 如何从代码中为Windows Phone 8.1(通用)应用程序添加/删除默认磁贴/辅助磁贴? - How to add / remove default tile / secondary tile for Windows phone 8.1 (universal) applications from code? 从页面导航时如何删除默认的“您确定”弹出窗口 - How to remove the default 'Are you sure' popup when navigating from a page 无法调用辅助磁贴页面以正确显示 - Could not call the secondary tile page to show correctly 离开一页时如何清除会话 - How to clear Session when navigating away from one page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM