首页 > 解决方案 > 从 UIElement 中提取 ContainerVisual

问题描述

ElementCompositionPreview类允许通过GetElementVisual API从UIElement中提取支持的Visual 。有没有办法从ContainerVisual中提取?由于 a只是所有类型s 的基类,有没有办法确定提取的这个是什么类型的?我尝试诉诸于,但这会在尝试将其转换为. 这是否表明这只是一个普通的类实例,不能用作其他任何东西,并且仅限于接口使用?UIElementVisualVisualVisual Visualdynamic_castnullptrContainerVisualVisualVisual

编辑:

为我的处理方式添加更多背景信息。这是我的代码中的内容

FrameworkElement^ elem = new UserControl();
auto visual = ElementCompositionPreview::GetElementVisual(elem);
if(auto container = dynamic_cast<ContainerVisual^>(visual)) {
  //Use ContainerVisual APIs and use it's children property to insert visuals in the visual tree
}

标签: uwpc++-cx

解决方案


您可以ContainerVisualCompositorUIElement.

请检查以下代码:

FrameworkElement^ elem =ref new UserControl();
auto _compositor = ElementCompositionPreview::GetElementVisual(elem)->Compositor;
auto containerVisual= _compositor->CreateContainerVisual();
if (containerVisual)
{
    //Test if the containerVisual is a null value
    containerVisual->Comment = "1122";
}
auto str = containerVisual->Comment;

推荐阅读