Struct 如何在WinRT组件C+;中通过引用传递结构+/CX

Struct 如何在WinRT组件C+;中通过引用传递结构+/CX,struct,components,windows-runtime,c++-cx,Struct,Components,Windows Runtime,C++ Cx,我的WinRT组件中包含以下内容: public value struct WinRTStruct { int x; int y; }; public ref class WinRTComponent sealed { public: WinRTComponent(); int TestPointerParam(WinRTStruct * wintRTStruct); }; int WinRTComponent::TestPointerParam(Wi

我的WinRT组件中包含以下内容:

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;
}
但是,当从C#调用winRTStruct->y和x时,该方法中的值似乎始终为0:


通过引用传递结构的正确方法是什么,以便在用C++/CX编写的WinRTComponent方法中更新它?

不能通过引用传递结构。winrt中的所有值类型(包括结构)都是按值传递的。Winrt结构应该相对较小——它们用于保存点和矩形之类的东西


在您的例子中,您已经指出struct是一个“out”参数,“out”参数是只写的,它的内容在输入时被忽略,在返回时被复制出来。如果你想让一个结构输入输出,把它分成两个参数——一个是“输入”参数,另一个是“输出”参数(输入/输出参数在WinRT中是不允许的,因为它们不会以你期望的方式投影到JS中)。

我的同事帮我解决了这个问题。 在WinRT组件中,最好的方法似乎是定义ref结构而不是value结构:

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编译器会给出内部编译器错误。

我不确定在WinRT中通过引用传递是如何工作的,但在C#中,如果使用
out
传递参数,则不会使用原始值,您需要
ref
。@svick我在这里晚了一点,但我想补充一点,您可以选择使用
ref
&
符号(例如:
comp.TestPointerParam(&winRTStruct)
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; }
 }
};