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?