c++ - How to activate an instance of a ref class -


say have class:

public ref class page1 sealed : windows::ui::xaml::controls::page {}; 

i can activate instance of class this:

auto page = ref new page1(); 

but how in raw c++?

i have tried doesn't work:

microsoft::wrl::wrappers::hstring classname; classname.set(l"app1.page1"); iinspectable *page; windows::foundation::activateinstance(classname.get(), &page); 

the above code work when specify windows runtime class name, (such "windows.ui.xaml.controls.button"), not own ref class "app1.page1".


alternatively, given have declared public ref class named page1 in app1 namespace, how can activate instance of class iinspectable* hstring "app1.page1"?

i think have figured out. well, answer not directly solve problem of activating arbitrary type, want.

the devil in detail. xaml compiler generate bunch of files not visible in solution explorer. these files have extension .g.h , .g.hpp. can click "show files" button in solution explorer see them.

in app.g.h, app class implements windows::ui::xaml::markup::ixamlmetadataprovider class, can use obtain information our xaml types. xamltypeinfo files contains generated type definitions.

here's code shows how activate 1 of our xaml types typename:

object^ activate(typename typename) {     auto app = application::current;     auto provider = static_cast<ixamlmetadataprovider^>(app);     auto xamltype = provider->getxamltype(typename);     return xamltype->activateinstance(); } 

no wrl necessary, 100% c++/cx, xaml type information generated xaml compiler!

i believe similar structure case c# projects, in application-derived class implement ixamlmetadataprovider interface too. under hood, windows runtime not use .net, not have kind of "real" reflection, relies on statically defined type definitions.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -