Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从辅助磁贴导航到页面时如何管理后堆栈_C#_Windows Phone 7 - Fatal编程技术网

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

C# 从辅助磁贴导航到页面时如何管理后堆栈,c#,windows-phone-7,C#,Windows Phone 7,我已经在我的应用程序中实现了辅助磁贴,因此用户可以将辅助磁贴固定到开始屏幕,然后相应地导航到我的应用程序中的相应页面。这个实现工作正常,除了当用户在从固定的辅助磁贴导航到特定页面后点击back hardware按钮,什么都没有发生?事实上,应用程序中的前一页实际上显示了一秒钟,尽管用户来自开始屏幕。按照用户的预期,返回开始屏幕的正确方法是什么(我假设这是正确的后堆栈导航) 我所做的如下,但仅在正常页面导航场景下有效,而不是在用户从开始屏幕导航到共享页面时 MainPage.xaml.cs pro

我已经在我的应用程序中实现了辅助磁贴,因此用户可以将辅助磁贴固定到开始屏幕,然后相应地导航到我的应用程序中的相应页面。这个实现工作正常,除了当用户在从固定的辅助磁贴导航到特定页面后点击back hardware按钮,什么都没有发生?事实上,应用程序中的前一页实际上显示了一秒钟,尽管用户来自开始屏幕。按照用户的预期,返回开始屏幕的正确方法是什么(我假设这是正确的后堆栈导航)

我所做的如下,但仅在正常页面导航场景下有效,而不是在用户从开始屏幕导航到共享页面时

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;
    }
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
        //{
        //    ??
        //}
    }
SharePage.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;
    }
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
        //{
        //    ??
        //}
    }

到目前为止,
CreateLiveTile()
方法创建辅助互动程序,然后当按下该互动程序时,将导航到MainPage,然后在MainPage OnNavigatedTo事件中检查查询字符串,然后根据单击的辅助互动程序加载相应的页面。一旦执行了此操作并加载了相应的页面,我就不能再按back按钮返回开始屏幕以遵循标准的Backback行为。如何解决此问题?

否则您可以使用
导航服务。导航(homepageUri)
为什么要取消导航?只需按BackKeyPress键将
移除即可。在这种情况下不需要它。

创建一个变量来跟踪主页的导航是否来自通过辅助磁贴的导航。加载检查该变量,如果该变量来自辅助磁贴,则从后堆栈中删除前一页。从主页返回开始菜单,而不是通过辅助平铺页面返回,这是有意义的。这就是MyStocks公文包(不,不是我的一个应用,而是我的一个

下面是一个关于后退按钮使用的好博客:

根据您的最新更新,让我们看看这种方法:

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
}
在这里,您创建一个新的平铺并将
tileParameter
传递到
MainPage
。因此,您导航到主页,然后检测平铺参数中的文本,并导航到
ShareLink
ShareStatus
页面。这就是为什么导航堆栈脏了

让我向您推荐一种避免这种情况的方法:

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
}
当用户点击您的辅助磁贴时,他将被直接导航到
ShareLink
ShareStatus
页面。并且
NavigationStack
将是干净的。当用户按下
Back
按钮时,应用程序将关闭,用户将看到开始屏幕(这是辅助磁贴的右后退按钮行为)

p、 不要忘记启动所有服务或加载所有资源(如果有的话)。因为不会创建主页!无论如何,应用程序的每个页面都必须能够初始化整个应用程序,因为您必须支持从还原


如果需要,请随时询问详细信息。

我编辑了上面的解决方案,以便更准确地演示我的实现和上面的问题。也许这会让我更深入地了解如何纠正问题?我不想使用它,因为这样可能会在我的应用程序导航堆栈中的某个位置导致不必要的循环。我编辑了我的或为了更准确地反映我的问题,我使用了BackKeyPress,以防页面从我的应用程序中导航到,但如果是从live tile导航到,我不确定如何按照用户的预期返回到开始屏幕?我在我的原始帖子中添加了更多内容,也许这会有所帮助?