gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:13:41 33 4
gpt4 key购买 nike

给定在其类中定义的自定义附加属性中的依赖属性

   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.我怎样才能从中得到一个 bool 值。

最佳答案

target.GetValue() 将返回 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));
}

关于c++ winrt uwp 如何从依赖属性中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58946578/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com