1
votes

I would like to allow a user to be able to pin a ConnectionSettingsTask item to the start page, and then navigate to that specific ConnectSettingsTask by pressing the corresponding tile on the Start page. My issue is, once the user has navigated to the corresponding ConnectionSettingsTask page, the user cannot directly navigate back to the start page using the hardware back button. For some reason, the application keeps loading the same ConnectionSettingsTask page over and over. My implementation as of now is as follows:

MainPage.xaml.cs

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

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

        if (_title != null)
        {
            ConnectionSettingsTask connectionSettingsTask = new ConnectionSettingsTask();                

            switch (_title)
            {
                case "WiFi":                        
                    connectionSettingsTask.ConnectionSettingsType = ConnectionSettingsType.WiFi;
                    connectionSettingsTask.Show();
                    break;
                 ...
            }

            try
            {                     
                //NavigationService.RemoveBackEntry();
                //_title = null;
                NavigationContext.QueryString.Remove(_title);
            }
            catch(InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

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

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

        if (Tile == null)
        {
            //choose which original background tile image to use
            switch (title)
            {
                case "WiFi":                        
                        background = new Uri("/Images/Mode/Mode_WiFi.png", UriKind.Relative); 
                    break;
                ...
            }

            try
            {
                var LiveTile = new StandardTileData
                {
                    Title = item.TileName,
                    BackgroundImage = background,
                    BackTitle = item.TileName,
                    //BackBackgroundImage = new Uri("/background.png", UriKind.Relative),
                    BackContent = item.Message,
                };

                ShellTile.Create(new Uri("/View/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;
    }

So, essentially the creation of the tile and pinning the secondary tile to the start screen works, as well as loading the WiFI ConnectionSettingsTask from the secondary tile. But when I try to navigate back from the ConnectionSettingsTask page, it just keeps reloading that page. How might I fix this issue according to the Windows Phone hardware back button certification requirements (must return to start screen in this scenario)?

3
What is your intention of overriding OnNavigatedTo method?dotNETbeginner
well if the secondary tile has been tapped, and the querystring is properly passed, the respective ConnectionSettingsTask page is loaded instead of the MainPage. The issue though is, once that occurs, the hardware back button attempts to go back, but the same ConnectionSttingsTask page is reloaded. So I would like to somehow fix it so the start page is the page that is shown once the hardware back button is pressed, which would follow Microsoft's standards.Matthew

3 Answers

1
votes

the task load over and over because everytime you back to your taskpage , call the navigatedto function which calls ConnectionSettingsTask whether or not navigated mode is new or back.

Make sure you open the task , when the navigated mode is new just like code:

        base.OnNavigatedTo(e);
        if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New)
        {
             // Open Task
         }

........................................ i can not add my comment after yours:

if you return to mainpage from connectionsetting task , you want to return the start page ?

you can know whether the last page is task page.

        if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New)
        {
             // Open Task
             _fromTask = true;
         }
        else if(e.NavigationMode==System.Windows.Navigation.NavigationMode.Back&&_fromTask)
    {
    // exit your app . 
    }
0
votes

I think, when you press back button, it will come back to your main page to where it originally navigated..and in main page you have redirect logic so it again goes to your ConnectSettingsTask page

Why can't you set ConnectSettingsTask page URL for your secondary tile along with tile argument.. I think it should work..

-1
votes

Try this code. By some modifications you may use this code for your scenario.

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)

    { 

     while (NavigationService.BackStack.Count() >= 1)
                NavigationService.RemoveBackEntry();
     }