英文:
How to convert from float3 to C++/WinRT type?
问题
我正在编写一个自定义的C++/WinRT组件,该组件将在C#应用程序中使用。
在这个组件内部,我有一些需要向调用应用程序公开的对象,这些对象具有属性,这种情况下是Id和Position。Id是整数类型,Position是float3类型。
以下是.idl和头文件的内容:
.idl:
runtimeclass MyObject
{
MyObject();
Int32 Id();
Windows::Foundation::Numerics::float3 Position();
}
.h:
#pragma once
#include "MyObject.g.h"
namespace winrt::TestComponent::implementation
{
struct MyObject : MyObjectT<MyObject>
{
MyObject();
int32_t Id();
Windows::Foundation::Numerics::float3 Position();
private:
int _id;
Windows::Foundation::Numerics::float3 _position;
};
}
namespace winrt::TestComponent::factory_implementation
{
struct MyObject : MyObjectrT<MyObject, implementation::MyObject>
{
};
}
看起来在.idl文件中不支持Windows::Foundation::Numerics::float3
,当我尝试构建项目时,该过程失败,并在.idl内声明Position()的行上出现了[msg]syntax error [context]: expecting an identifier near ":"
错误。使用C++/WinRT公开运行时类的float3属性的最佳方法是什么?
英文:
I'm writing a custom C++/WinRT component which will be consumed from a C# application.
Inside this component I have objects that I need to expose to the caller application and these
objects have properties, in this case an Id and a Position. The Id is an integer type, and the Position is float3.
Here are the contents of the .idl and the header file:
.idl:
runtimeclass MyObject
{
MyObject();
Int32 Id();
Windows::Foundation::Numerics::float3 Position();
}
.h:
#pragma once
#include "MyObject.g.h"
namespace winrt::TestComponent::implementation
{
struct MyObject : MyObjectT<MyObject>
{
MyObject();
int32_t Id();
Windows::Foundation::Numerics::float3 Position();
private:
int _id;
Windows::Foundation::Numerics::float3 _position;
};
}
namespace winrt::TestComponent::factory_implementation
{
struct MyObject : MyObjectrT<MyObject, implementation::MyObject>
{
};
}
It looks like that Windows::Foundation::Numerics::float3
is not supported inside .idl files, when I try to build the project the process fails with [msg]syntax error [context]: expecting an identifier near ":"
error at the line where the Position() is declared inside the .idl. What would be the best way to use expose float3 property of a runtime class using C++/WinRT?
答案1
得分: 1
float3
是标准的 WinRT 投影类型用于 C++。在 IDL 中(它应该是与编程语言无关的),您要使用的是 Windows.Foundation.Numerics.Vector3
WinRT 类型,并且在所有的 .h 或 .cpp 文件中使用 float3
,类似于以下方式:
class.idl:
namespace RuntimeComponent1
{
[default_interface]
runtimeclass Class
{
Class();
Int32 MyProperty;
Windows.Foundation.Numerics.Vector3 Position();
}
}
class.h:
#pragma once
#include "Class.g.h"
namespace winrt::RuntimeComponent1::implementation
{
struct Class : ClassT<Class>
{
Class() = default;
int32_t MyProperty();
void MyProperty(int32_t value);
Windows::Foundation::Numerics::float3 Position();
};
}
namespace winrt::RuntimeComponent1::factory_implementation
{
struct Class : ClassT<Class, implementation::Class>
{
};
}
class.cpp:
#include "pch.h"
#include "Class.h"
#include "Class.g.cpp"
namespace winrt::RuntimeComponent1::implementation
{
int32_t Class::MyProperty()
{
throw hresult_not_implemented();
}
void Class::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}
Windows::Foundation::Numerics::float3 Class::Position()
{
throw hresult_not_implemented();
}
}
英文:
float3
is the standard WinRT projected type for C++. What you want to use in the idl (which is supposed to be language agnostic) is the Windows.Foundation.Numerics.Vector3
WinRT type and float3
in all .h or .cpp files, something like this:
class.idl:
namespace RuntimeComponent1
{
[default_interface]
runtimeclass Class
{
Class();
Int32 MyProperty;
Windows.Foundation.Numerics.Vector3 Position();
}
}
class.h:
#pragma once
#include "Class.g.h"
namespace winrt::RuntimeComponent1::implementation
{
struct Class : ClassT<Class>
{
Class() = default;
int32_t MyProperty();
void MyProperty(int32_t value);
Windows::Foundation::Numerics::float3 Position();
};
}
namespace winrt::RuntimeComponent1::factory_implementation
{
struct Class : ClassT<Class, implementation::Class>
{
};
}
class.cpp:
#include "pch.h"
#include "Class.h"
#include "Class.g.cpp"
namespace winrt::RuntimeComponent1::implementation
{
int32_t Class::MyProperty()
{
throw hresult_not_implemented();
}
void Class::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}
Windows::Foundation::Numerics::float3 Class::Position()
{
throw hresult_not_implemented();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论