在C ++ / WinRT中实现ICustomProperty

时间:2019-01-16 23:20:33

标签: uwp c++-winrt

我正在尝试在C ++ / WinRT中进行data {Binding} via ICustomProperty(Microsoft文档中没有可用的示例),

#include "winrt\Windows.UI.Xaml.Data.h"

using namespace winrt;
using namespace winrt::Windows::UI::Xaml::Data;

struct MyCustomProperty : winrt::implements<MyCustomProperty, ICustomProperty>
{
// To be implemented
};

static MyCustomProperty TitleProperty;

struct MyCustomObject : winrt::implements<MyCustomObject, ICustomPropertyProvider>
{
    ICustomProperty GetCustomProperty(winrt::hstring name)
    {
        return TitleProperty.try_as<ICustomProperty>();
    }
    // Other methods omitted
};

不幸的是,出现编译错误:

错误C2039:“ try_as”:不是“ MyCustomProperty”的成员

请注意,return TitleProperty;根本不起作用。我该如何解决?

1 个答案:

答案 0 :(得分:0)

在消化this documentation中的神秘术语后,解决方案是使用winrt::make函数模板创建我的静态属性实例:

static ICustomProperty TitleProperty = winrt::make<MyCustomProperty>();

然后我可以稍后再返回:

ICustomProperty GetCustomProperty(winrt::hstring name)
{
    return TitleProperty;
}

winrt::make的文档将这两种情况(运行时类与非运行时类)合并为一个,而没有针对我的非运行时用例的具体示例。