Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone app calls MapUri() twice when I call NavigateService.Navigate()

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;
    }
}

In InitializePhoneApplication() I do a

RootFrame.UriMapper = new AssociationUriMapper();

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

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.

When I started to investigate this further I noticed that this happens whenever I call NavigateService.Navigate(). But not when I call NavigateService.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).

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

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.

like image 830
mattsson Avatar asked Oct 21 '22 14:10

mattsson


1 Answers

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

like image 178
Muster Station Avatar answered Oct 23 '22 09:10

Muster Station