且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式将MediaTimeline与Storyboard中的动画结合

更新时间:2023-11-30 16:40:16

您的MediaElement不属于VisualTree。您正在构造它,但是由于它没有分配给可视化树,因此布局系统将永远不会使用它。

Your MediaElement is not part of the VisualTree. You are constructing it but as it is not assigned to the visual tree it will never get used by the Layout System.

您可以将其添加到代码隐藏中,方法是添加它到一个有效的容器元素。
例如

You can either add it in code-Behind by adding it to a valid container element. e.g.

XAML:

<Grid x:Name="parentGrid">

隐藏代码:

var mediaElement = new MediaElement();
parentGrid.Children.Add(mediaElement);

或者您可以将MediaElement放在页面/窗口上,删除构造MediaElement的行( .. = new MediaElement)并通过以下代码进行访问:

Or you can place the MediaElement on your page/window, remove the line where you construct the MediaElement ( .. = new MediaElement) and just access it via code-behind:

XAML:

<MediaElement x:Name="mediaElement" Width="200" Height="100"/>