0
votes

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.

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

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. 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?

4

4 Answers

0
votes

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. So, you navigate to main page, then detect the text in tile parameter, and navigate to the ShareLink or ShareStatus pages. 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. And a NavigationStack will be clean. 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).

p.s. Don't forget to start all your services or load all resources if you have ones. Because MainPage won't be created! 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.

0
votes

else you can use NavigationService.Navigate(homepageUri)

0
votes

Why do you cancel a navigaion? Just remove OnBackKeyPress. You don't need it in this scenario.

0
votes

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. 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

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