c++ winrt uwp 如何从依赖中获取价值 属性

c++ winrt uwp how to get value out of dependency property

给定自定义附加 属性 中的依赖项 属性 定义为 class as

   private:
            static Windows::UI::Xaml::DependencyProperty m_IsOpenProperty;

我试过了,


bool FlyoutCloser::GetIsOpen(Windows::UI::Xaml::DependencyObject const& target)
    {
        return target.GetValue(m_IsOpenProperty).as<bool>();

    }

但是这里编译器告诉我 C++ no suitable conversion function from to exists converting from winrt::impl::com_ref<bool> to "bool" exists. 如何从中获取布尔值。

target.GetValue() 将 return 一个 IInspectable 类型,您需要使用 winrt::unbox_value 函数将 IInspectable 拆箱回标量值,而不是使用 as 方法。而关于装箱和拆箱,可以参考这个document.

bool FlyoutCloser::GetIsOpen(Windows::UI::Xaml::DependencyObject const& target)
{
    return winrt::unbox_value<bool>(target.GetValue(m_IsOpenProperty));
}

void FlyoutCloser::SetIsOpen(Windows::UI::Xaml::DependencyObject const& target, bool value)
{
    target.SetValue(m_IsOpenProperty, winrt::box_value(value));
}