简体   繁体   中英

How to control a MediaElement inside of a VisualBrush

I have a MediaElement inside of a VisualBrush object so that I can run multiple video displays while only using one MediaElement. The problem is that I cannot figure out how to control the MediaElement from the code behind. The code behind doesn't recognize the name 'myMedia'.

How do I access this element?

 <Window.Resources>
        <VisualBrush x:Key="Media" Stretch="Uniform">
            <VisualBrush.Visual>
                <MediaElement Name="myMedia" Source="c:\a.mp4" Width="100" Height="100"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>

You can get resources by key from an element in code behind via the FrameworkElement.Resources property . Then it is just a matter of casting the returned resource to a VisualBrush and accessing and casting its VisualBrush.Visual property .

VB.NET:

Dim VBrush As VisualBrush = TryCast(Me.Resources("Media"), VisualBrush)

If VBrush IsNot Nothing Then
    Dim Media As MediaElement = TryCast(VBrush.Visual, MediaElement)

    If Media IsNot Nothing Then
        'Do your stuff here...
    End If
End If

C#:

VisualBrush VBrush = this.Resources["Media"] as VisualBrush;

if(VBrush != null) {
    MediaElement media = VBrush.Visual as MediaElement;

    if(media != null) {
        //Do your stuff here...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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