2012-05-14 38 views
3

我已经在我的WinRT组件如下:如何通过WinRT中组分C++/CX参考传递结构

public value struct WinRTStruct 
{ 
    int x; 
    int y; 
}; 

public ref class WinRTComponent sealed 
{ 
    public: 
    WinRTComponent(); 
    int TestPointerParam(WinRTStruct * wintRTStruct); 
}; 

int WinRTComponent::TestPointerParam(WinRTStruct * wintRTStruct) 
{ 
    wintRTStruct->y = wintRTStruct->y + 100; 
    return wintRTStruct->x; 
} 

但是,似乎的winRTStruct-值> Y和X始终为0

WinRTComponent comp = new WinRTComponent(); 
WinRTStruct winRTStruct; 
winRTStruct.x = 100; 
winRTStruct.y = 200; 
comp.TestPointerParam(out winRTStruct); 
textBlock8.Text = winRTStruct.y.ToString(); 

什么是通过由参考一个结构,从而它的一个用C++编写/ CX一个WinRTComponent的方法内被更新的正确的方法:该方法中,从C#调用时内?

+1

我不知道如何在WinRT中通过引用工作,但在C#中,如果您使用'out'传递参数,则不使用原始值,则需要'ref'。 – svick

+0

@svick我在这里有点晚,但是我想补充一点,你可以选择使用'ref'或'&'符号(例如:'comp.TestPointerParam(&winRTStruct)' https:// msdn .microsoft.com/EN-US /库/ hh699870.aspx – user3164339

回答

3

您不能通过引用传递结构。所有值类型(包括结构)在winrt中都是按值传递的。 Winrt结构预计会相对较小 - 它们旨在用于持有像Point和Rect之类的东西。

就你而言,你已经指出struct是一个“out”参数 - “out”参数是只写的,其内容在输入时被忽略,并在返回时被复制出来。如果你想要一个结构体进入和退出,将它分成两个参数 - 一个“in”参数和另一个“out”参数(WinRT中不允许输入/输出参数,因为它们不按照你期望的方式投射到JS上他们去投影)。

1

我的同事帮我解决了这个问题。 在WinRT的成分,似乎要做到这一点的最好办法是定义一个参考结构,而不是一个值的结构:

public ref struct WinRTStruct2 sealed 
{ 
private: int _x; 
public: 
property int X 
{ 
    int get(){ return _x; } 
    void set(int value){ _x = value; } 
} 
private: int _y; 
public: 
property int Y 
{ 
    int get(){ return _y; } 
    void set(int value){ _y = value; } 
} 
}; 

但是,这造成其他问题。现在,当我尝试向返回结构实例的ref结构添加方法时,VS11编译器给出了INTERNAL COMPILER ERROR。