询问者
在C++/CX中如何根据Type^构造一个实例?

问题
全部回复
-
这个功能类似于.net里面的反射,可以参考一下: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/211ef583-db11-4e55-926b-6d9ab53dbdb4/ccx-reflection
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
这个功能类似于.net里面的反射,可以参考一下: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/211ef583-db11-4e55-926b-6d9ab53dbdb4/ccx-reflection
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.James 你好:
你确定这个链接是正确的吗,是不是搞错了?因为我没在那个帖子中看到任何与我这个问题有关的内容啊。
-
-
RoGetActivationFactory /IActivationFactory::ActivateInstance
Visual C++ MVPObject^ CreateInstance(Type^ _Type) { ComPtr<IActivationFactory> _Factory; if (SUCCEEDED(RoGetActivationFactory(reinterpret_cast<HSTRING>(_Type->FullName), __uuidof(IActivationFactory), &_Factory)) == true) { ComPtr<IInspectable> _Object = nullptr; if (SUCCEEDED(_Factory->ActivateInstance(&_Object)) == true) return reinterpret_cast<Object^>(_Object.Get()); } }
你能不能提供一个例子呢?我使用以上代码只能建立系统类型,比如Border、Grid等等,只要是系统现有的都没问题,但如果是我自己建立的就不行了。比如:public ref class MyObject sealed : public Grid{};
应该怎么做呢?
-
自己激活的类型应该在应用程序清单中指明,例如
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest"> <Extensions> <Extension Category="windows.activatableClass.inProcessServer"> <InProcessServer> <Path>bin\GrayscaleTransform.dll</Path> <ActivatableClass ActivatableClassId="Microsoft.Samples.GrayscaleEffect" ThreadingModel="both" /> </InProcessServer> </Extension> </Extensions> </Package>
Visual C++ MVP -
-
加上以后同样没有任何效果。另外还是那个问题,为什么在使用Windows::UI::Xaml::Controls::Frame控件中的Navigate(TypeName)函数时什么也不需要做?
我在这篇文章(http://blogs.msdn.com/b/vcblog/archive/2012/10/05/cxxcxpart03underconstruction.aspx)中看到:
Every Windows Store app contains a file named AppXManifest.xml. This manifest contains all sorts of important information about the app, including its identity, name, and logo. The manifest also contains a section containing extensions: this section contains a list of all of the modules that define activatable types and a list of all of the activatable types defined by each of those modules. For example, the following entry is similar to what we would find for the Widget type: <Extension Category="windows.activatableClass.inProcessServer"> <InProcessServer> <Path>WidgetComponent.dll</Path> <ActivatableClass ActivatableClassId="WidgetComponent.Widget" ThreadingModel="both" /> </InProcessServer> </Extension> The list includes only types defined by modules contained in the app package; types provided by Windows (i.e., types in the Windows namespaces) are registered globally, in the registry, and are not included in the AppXManifest.xml manifest.
这和你之前所提到的是一样的。但接下来文章又提到:
For most projects, this manifest is created as part of the app packaging task that runs after an app is built. The contents of the extensions section is automatically populated via examination of the WinMD files for any referenced components and the manifests from any referenced Extension SDKs. When our app calls RoGetActivationFactory, the Windows Runtime uses this list to find the module it needs to load for the Widget type.
也就是说这部分声明应该是自动加入的。但我在AppXManifest.xml文件中没有找到Extension部分。如果我手动加入的话,那在之后也会被自动替换掉。看来这个文件是由系统来自动生成的。事实上在AppXManifest.xml这个文件中本身也有如下一段话。
<!-- 此包清单文件由生成过程生成。 如果重新生成此文件,将丢失对其所做的更改。若要更正此文件中的错 误,请编辑源 .appxmanifest 文件。 有关程序包清单文件的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=241727 -->
打开上面的链接(https://msdn.microsoft.com/zh-cn/library/windows/apps/hh924767(v=vs.110).aspx)。里面提到:
Windows 运行时注册扩展 可为应用程序实现 Windows 运行时组件,但必须向操作系统注册这些组 件,才能使其正常运行。若要注册 Windows 运行时组件,必须将注册信 息放在 WinMD 文件和应用程序清单中。如果项目实现 Windows 运行时 组件,则该项目的生成输出将包含一个 WinMD 文件。Visual Studio 从 WinMD 文件提取 Windows 运行时注册信息并在应用程序清单中生成相 应的“扩展”元素。
也就是说此注册信息应该是由VS来加入的。现在的问题是,也就是我最想知道的问题是,在AppXManifest.xml文件中没有找到Extension部分的情况下,Windows::UI::Xaml::Controls::Frame控件中的Navigate(TypeName)函数仍然能够正确创建实例,这个是怎么一回事呢?。