Navigate to a XAML page in a different assembly in

2020-06-03 03:53发布

I have a XAML page in a separate Windows Phone class library. The library is included in my VS solution and referenced from my app project. Let's say the page is called TestPage.xaml and it's in the root folder of my library called SharedPages.

I'd like to navigate to this page in my app using the NavigationService. I found this post which suggests using this URI format:

/{assemblyName};component/{pathToResource}

So I'm trying something like this:

NavigationService.Navigate(new Uri("/SharedPages;component/TestPage.xaml"));

When I run this I get the following exception:

Navigation is only supported to relative URIs that are fragments, or begin with '/', or which contain ';component/'.

What am I doing wrong?

Thanks.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-03 04:38

Looks like the URI needs to be created as a relative URI. This works:

NavigationService.Navigate(new Uri("/SharedPages;component/TestPage.xaml", UriKind.Relative));
查看更多
狗以群分
3楼-- · 2020-06-03 04:40

Hey I don't think it's your case, but it's good to review the generated assembly name.

Option 1) Incorrect Assembly name

The following format:

/{assemblyName};component/{pathToResource}

The assembly name can be checked it in the tab properties of the project. I remember a guy who has renamed the project but not the assembly and that was a reason because of the navigation was failing.

Option 2)

Dot separator in your assembly name.

I don't know the real reason of this, but I took me some time to found it. The name of the assembly must not have any dot as separator. I guess the Uri parse will incorrectly the Uri. For example I had an assembly called: "Com.Hmb.Prax" and I got the following exception:

Navigation is only supported to relative URIs that are fragments, or begin with '/', or which contain ';component/'.
Parameter name: uri

Once I rename my dll to "ComHmbPrax" it worked well. Herber

查看更多
登录 后发表回答