当我调用NavigateService.Navigate()时,Windows Phone应用程序会调用MapUri()两次 [英] Windows Phone app calls MapUri() twice when I call NavigateService.Navigate()

查看:93
本文介绍了当我调用NavigateService.Navigate()时,Windows Phone应用程序会调用MapUri()两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近添加了一个自定义UriMapper,用于从IE打开文件时将文件保存到我的应用程序隔离存储中.此代码如下所示:

I recently added a custom UriMapper for saving files into my applications isolated storage when opening them from IE. The code for this looks like this:

class AssociationUriMapper : UriMapperBase
{
    public override Uri MapUri(Uri uri)
    {
        var tempUri = uri.ToString();

        if (tempUri.Contains("/FileTypeAssociation"))
        {                
            int fileIdIndex = tempUri.IndexOf("fileToken=") + 10;
            string fileId = tempUri.Substring(fileIdIndex);

            SaveFileToIsolatedStorage(fileId);

            return new Uri("/MainPage.xaml", UriKind.Relative);                
        }

        return uri;
    }
}

InitializePhoneApplication()中,我执行

RootFrame.UriMapper = new AssociationUriMapper();

当然,我已经将 Extensions 标记添加到 WMAppManifest.xml

And of course I've added the Extensions tag to WMAppManifest.xml

所有这些都很好...但是我注意到了一个奇怪的行为.每次从IE打开文件时,我都会得到文件的两个副本.当我在覆盖的MapUri中放置一个断点时,每次应用程序从IE自动启动时,它都会被击中两次.

All this works fine... but I noticed a strange behaviour. I get two copies of the files each time I open them from IE. When I put a breakpoint inside my overridden MapUri it gets hit twice every time the application autolaunches from IE.

当我开始进一步调查时,我注意到无论何时打电话给我,都会发生这种情况 NavigateService.Navigate().但是当我致电 NavigateService.GoBack 时却没有.

When I started to investigate this further I noticed that this happens whenever I call NavigateService.Navigate(). But not when I call NavigateService.GoBack.

有人知道为什么会这样吗?为什么在调用 Navigate()时两次调用 MapUri()?创建页面的新实例时会发生什么事情吗? (我注意到,当我们调用 Navigate()时,会创建一个被调用页面的新实例,但是当我们调用 GoBack()时,我们会检索到已创建的我们从中导航的页面.)

Do anyone know why this is happening? Why are MapUri() called twice when Navigate() is called? Is it something that happens when a new instance of a page is created? (I've noticed that when we call Navigate() a new instance of the called page is created, but when we call GoBack() we retrieve the already created instance of the page we navigated from).

修改:
我现在已经从头开始做一些测试应用程序.结果是一样的.如果我有一个类继承自 UriMapperBase ,并且覆盖了 MapUri 方法,则每次导航到应用程序或调用时,都会两次调用 MapUri .在应用程序中> NavigateService.Navigate().


I've now done a little test application from scratch. The result is the same. If I have a class that inherits from UriMapperBase and overrides the MapUri method, MapUri is called twice whenever I navigate to the application or call NavigateService.Navigate() in the application.

对于我的问题,最明显的解决方法当然是有一个单独的页面供应用程序导航至该页面,然后该页面调用 SaveFileToIsolatedStorage().但这仍然不能回答为什么行为保持原状的问题.

The obvious workaround for my problem is of course to have a separate page that the application navigates to and that page call SaveFileToIsolatedStorage(). But that still doesn't answer the question why the behaviour is as it is.

推荐答案

尝试使用ViewModel而非代码通过MainPage的XAML挂钩UriMapper.

Try hooking up the UriMapper through the XAML of the MainPage using a ViewModel instead of through code.

这篇关于当我调用NavigateService.Navigate()时,Windows Phone应用程序会调用MapUri()两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆