我可以把StretchDirection添加到VisualBrush中吗?

8

是否可以给VisualBrush添加StretchDirection属性?我的VisualBrush包含一个媒体元素,而媒体元素具有此属性,但VisualBrush没有。当我将StretchDirection属性应用于此MediaElement时,它被忽略了,我猜测是因为VisualBrush覆盖了它的属性。

  <VisualBrush x:Key="vb_zoneOneAdvertisement" TileMode="None" Stretch="Uniform" AlignmentX="Center" AlignmentY="Center" >
     <VisualBrush.Visual>
        <MediaElement />
     </VisualBrush.Visual>
  </VisualBrush> 

对我来说,仅使用画笔来渲染MediaElement似乎有点奇怪?这样做的原因和目的是什么? - GCamel
@GCamel - 出于性能原因。我需要在多个不同的窗口上运行相同的媒体文件,这些窗口又会显示在一个到多个监视器上。如果我为每个要播放媒体文件的区域使用不同的媒体元素,那么就会浪费大量的CPU和内存。通过使用mediabrush,我可以运行一个媒体播放器,并将内容放置在任何我想要的窗口/空间中,而不会损失资源。 - nikotromus
嗨,我对我的代码进行了一些尝试-没有找到你想要的方向控制。VisualBrush中的Stretch似乎已经足够了。顺便说一句,由于内容在VisualBrush中的ViewBox中显示,所以不能使用方向控制。你需要调整它来满足你的需要-ViewBox="0,0,0.5,0.5"或者ViewPort。祝好运。 - GCamel
1个回答

2

VisualBrushVisualBrush.Visual的所有内容作为Brush使用。因此,如果你在大小为10000x10000的Image上使用Stretch="None",并将此VisualBrush设置为WindowBackground,则会导致性能问题。

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListViewBasicSample" Height="500" Width="500">

    <Grid>
        <Grid.Background>
            <VisualBrush TileMode="None" AlignmentX="Center" Stretch="Fill" AlignmentY="Center" >
                <VisualBrush.Visual>
                    <Grid Background="red">
                        <Image Source="wp2444179.jpg" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None" Margin="100" Height="10000" />
                    </Grid>
                </VisualBrush.Visual>
            </VisualBrush>
        </Grid.Background>
    </Grid>

</Window>

你会期望只有图片的一小部分被渲染,但实际上所有VisualBrush.Visual的内容都被视为VisualBrush。这是结果:enter image description here 在这种情况下,没有Stretch属性可用。你可以使用marginpaddingTileMode进行微小调整。可能需要添加一个容器(比如Grid)来定义MediaElementWidthHeight
简而言之, Stretch 可以用于 VisualBrush,也可以像你的代码示例那样正确地应用。问题出在VisualBrush.Visual上。在此处使用该属性几乎没有意义,因为所有渲染的VisualBrush.Visual都被用作 Brush。我会说,你拉伸得越多,用作 Brush 的就越多 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接