从 UIElement 中提取 ContainerVisual

问题描述

ElementCompositionPreview 类允许通过 Visual API 从 UIElement提取支持 GetElementVisual。有没有办法从 UIElement提取 ContainerVisual?由于 Visual 只是所有类型的 Visual 的基类,有没有办法确定这 提取Visual 类型 {{1} } 是?我尝试求助于 Visual,但在尝试将其强制转换为 dynamic_cast 时会产生 nullptr。这是否表示这只是一个普通的 ContainerVisual 类实例,不能用作其他任何东西,并且只能使用 Visual 接口?

编辑:

为我将如何处理添加更多上下文。这是我的代码中的内容

Visual

解决方法

您可以从从 ContainerVisual 中提取的 Compositor 实例创建一个 UIElement

请检查以下代码:

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;