如何将float3转换为C++/WinRT类型?

huangapple go评论61阅读模式
英文:

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 &quot;:&quot;错误。使用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 &quot;MyObject.g.h&quot;

namespace winrt::TestComponent::implementation 
{
	struct MyObject : MyObjectT&lt;MyObject&gt;
	{
		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&lt;MyObject, implementation::MyObject&gt;
    {
    };
}

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 &quot;:&quot; 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 &quot;Class.g.h&quot;

namespace winrt::RuntimeComponent1::implementation
{
    struct Class : ClassT&lt;Class&gt;
    {
        Class() = default;

        int32_t MyProperty();
        void MyProperty(int32_t value);
        Windows::Foundation::Numerics::float3 Position();
    };
}

namespace winrt::RuntimeComponent1::factory_implementation
{
    struct Class : ClassT&lt;Class, implementation::Class&gt;
    {
    };
}

class.cpp:

#include &quot;pch.h&quot;
#include &quot;Class.h&quot;
#include &quot;Class.g.cpp&quot;

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 &quot;Class.g.h&quot;

namespace winrt::RuntimeComponent1::implementation
{
    struct Class : ClassT&lt;Class&gt;
    {
        Class() = default;

        int32_t MyProperty();
        void MyProperty(int32_t value);
        Windows::Foundation::Numerics::float3 Position();
    };
}

namespace winrt::RuntimeComponent1::factory_implementation
{
    struct Class : ClassT&lt;Class, implementation::Class&gt;
    {
    };
}

class.cpp:

#include &quot;pch.h&quot;
#include &quot;Class.h&quot;
#include &quot;Class.g.cpp&quot;

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();
    }
}

huangapple
  • 本文由 发表于 2023年7月13日 19:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定