如何调用mainpage.xaml.cpp中声明的函数

时间:2015-03-24 14:03:02

标签: c++ windows-8 c++-cx

基本上:

MainProject中引用WrapperProject的

MainPage.xaml.h

#includes yadda-yadda
#include "Wrapper.h"
namespace MyNamespace
{
    public ref class MainPage sealed
    {
    public:
        MainPage();
        //...
        DoAGreatThing(int greatThingId);
    private:
        //...
    }
WrapperProject中的

Wrapper.h

#include "pch.h"
ref class Wrapper sealed
{
public:
    static void InitInstance();
    static Wrapper^ instance();
    //...
}

Wrapper如何调用DoAGreatThing方法?


文字墙:

我有一个包含多个项目的Win8应用程序。主应用程序项目是默认的基于XAML的C ++ / CX项目。

Wrapper项目有一个单例,其文件包含在mainpage.xaml中,以便在某些情况下调用包装器方法。

我引用了一些必须仅在主应用程序项目中引用的库,因此只能从那里调用它的方法,但包装器没有看到这些文件(mainpage.xaml)。我不能在我的包装器中包含mainpage,但是当在其他项目中发生事件时我需要调用上述库中的一些方法,它应该由包装器传递。

我无法在mainpage.xaml.cpp中创建一个函数指针,并将其传递给包装器单例,因为它是C ++ / CX并且它不像本机类型。

我未能创建委托/事件,但委托声明应该在mainpage.xaml.h中完成,因此包装器不可见。

我该怎么办?如何从包装器调用mainpages函数?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

App.xaml.cpp (了解MainPage并将其作为mMainPage)

void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args)
{
//...
NativeClass::instance()->SetGreatThingDoer([=](int greatThingId){mMainPage->DoAGreatThing(greatThingId);});
//...
}
位于WrapperProject

中的包装器旁边的

NativeClass.h

#include <functional>
//...
class NativeClass
{
public:
    void SetGreatThingDoer(std::function<void(int)> func) {mDoAGreatThing = func;};
    void DoAGreatThing(int greatThingId) {mDoAGreatThing(greatThingId);};
private:
    std::function<void(int)> mDoAGreatThing;
//...
}

从NativeClass调用DoAGreatThing调用MainPages DoAGreatThing

所有赞美lambdas!