【问题标题】:Implement ICustomProperty in C++/WinRT在 C++/WinRT 中实现 ICustomProperty
【发布时间】:2019-01-18 00:30:21
【问题描述】:

我正在尝试在 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; 不起作用。我该如何解决这个问题?

【问题讨论】:

    标签: uwp c++-winrt


    【解决方案1】:

    在消化了this documentation中的隐晦行话后,解决方案是使用winrt::make函数模板创建我的静态属性实例:

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

    然后我可以稍后简单地返回它:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多