如何从Viewport3d提取情节提要.wpf中的触发器

本文关键字:wpf 触发器 Viewport3d 提取 | 更新日期: 2023-09-27 18:26:03

我有一个单独的xaml文件,如下所示:

<Viewport3D x:Name="ZAM3DViewport3D" ClipToBounds="true" Width="400" Height="300" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006" xmlns:c="http://schemas.openxmlformats.org/markup-compatibility/2006" c:Ignorable="d">
<Viewport3D.Triggers>
    <EventTrigger RoutedEvent="Viewport3D.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard Duration="Forever" FillBehavior="HoldEnd" BeginTime="0:0:0" x:Name="storyboard" d:StoryboardName="OnLoaded">
.....

我在程序中有这样一段代码:

....
FileStream fs = new FileStream(mm.path, FileMode.Open, FileAccess.Read); //
Viewport3D v3d = (Viewport3D)XamlReader.Load(fs);
....

我想知道如何从EventTrigger.Actions中提取故事板。我想它看起来像这样:

Storyboard sb = (Storyboard)v3d.Triggers[0]....

有人知道怎么做吗?

如何从Viewport3d提取情节提要.wpf中的触发器

如果您在Triggers旁边的其他地方需要Storyboard,请将其定义为资源。然后,您可以使用StaticResourceBeginStoryboard中引用它,在代码中,您可以用FindResource找到它,也可以直接从Resources属性中获取它。

<Viewport3D.Resources>
    <Storyboard x:Key="ThatSb">...</Storyboard>
<Viewport3D.Resources>
<!-- .... -->
    <BeginStoryboard Storyboard="{StaticResource ThatSb}"/>
Storyboard sb = (Storyboard)v3d.Resources["ThatSb"];

不太好的选择实际上是向下投射,就像一样

var trigger = (EventTrigger)v3d.Triggers[0];
var beginSb = (BeginStoryboard)trigger.Actions[0];
var sb = beginSb.Storyboard;