简体   繁体   English

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

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

Given a dependency property in a Custom attached property defined in it's class as给定在其 class 中定义的自定义附加属性中的依赖属性

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

I have tried,我努力了,


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

    }

however here the compiler is telling me C++ no suitable conversion function from to exists converting from winrt::impl::com_ref<bool> to "bool" exists.但是这里编译器告诉我C++ no suitable conversion function from to exists converting from winrt::impl::com_ref<bool> to "bool" exists. How can I get a boolean value out of it.如何从中获得 boolean 值。

target.GetValue() will return an IInspectable type, you need to use winrt::unbox_value function to unbox an IInspectable back into a scalar value instead of using as method. target.GetValue()将返回 IInspectable 类型,您需要使用 winrt::unbox_value function 将 IInspectable 拆箱回标量值,而不是使用as方法。 And about boxing and unboxing, you can refer to this 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM