在 UWP 中显示可选包中的页面 [英] Show page from optional package in UWP

查看:20
本文介绍了在 UWP 中显示可选包中的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的 UWP 应用添加一些功能.可选包似乎是一种方法.所以我把 ViewModel 类和 Page 类移到可选包中,并将它们放入可选包的命名空间中.安装可选包后,我想显示移动的页面,但总是出现以下错误:

I want to add some features to my UWP app. Optional packages seem to be a way for that. So I moved the ViewModel class and the Page class to the optional package and put them into the optional package's namespace. After installing the optional package I want to show the moved Page, but I always get the following error:

Windows.UI.Xaml.Markup.XamlParseException
  HResult=0x802B000A
  Message=XAML parsing failed.
  Source=System.Private.Interop
  StackTrace:
   at System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32 hr, RuntimeTypeHandle typeHnd) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Interop\src\Shared\McgMarshal.cs:line 1189
   at __Interop.ComCallHelpers.Call(__ComObject __this, RuntimeTypeHandle __typeHnd, Int32 __targetIndex, Void* arg0, Void* arg1, Int32 arg2)
   at Windows.UI.Xaml.IApplicationStatics__Impl.Stubs.LoadComponent(__ComObject __this, Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
   at Windows.UI.Xaml.Application.__Factory_Windows_UI_Xaml__IApplicationStatics_LoadComponent(String typeName, Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
   at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
   at <my classes>

如何加载和显示可选包的页面?

How can I load and show my optional package's Page?

推荐答案

我终于发现,生成的InitializeComponent()方法是错误的.

I finally found out, that the generated InitializeComponent() method is wrong.

在我的 示例代码库fixed 分支中解决方案.

In my sample code repository's fixed branch is a working solution.

在可选包的 Pages 后面的代码中,构造函数对 InitializeComponent() 的调用必须替换为以下代码:

In the code behind the optional package's Pages, the constructor's call of InitializeComponent() has to be replaced with the following code:

Uri resourceLocator = new Uri("ms-appx://ad4d8e16-9f9c-458f-ac0f-e74cb99fa10c/HalloPage.xaml");
Application.LoadComponent(this, resourceLocator, ComponentResourceLocation.Nested);

ad4d8e16-9f9c-458f-ac0f-e74cb99fa10c 是可选包的标识名称.如果您想加载图像、资源字典等,您必须以相同的格式编写 URI,包括可选包的身份名称.否则,您将从主应用加载资源.

ad4d8e16-9f9c-458f-ac0f-e74cb99fa10c is the optional package's identity name. If you want to load images, resource dictionaries etc. you have to write the URIs in the same format including the optional package's identity name. Otherwise you will load resources from the main app.

如果工具链能够自行生成正确的 URI,那就太好了.

It would be great, if the tool chain would generate the right URIs by itself.

这篇关于在 UWP 中显示可选包中的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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