简体   繁体   English

如何从WPF中的Viewport3d.Triggers中提取Storyboard

[英]How to extract Storyboard from Viewport3d.Triggers in wpf

I have a separate xaml file that looks like this : 我有一个单独的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">
.....

And I have this segment of code in the program : 我在程序中有这段代码:

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

I would like to know how I can extract the storyboard from the EventTrigger.Actions. 我想知道如何从EventTrigger.Actions中提取情节提要。 I guess it would look like this: 我想它看起来像这样:

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

Does anyone know how to do this? 有谁知道如何做到这一点?

If you need a Storyboard somewhere else beside the Triggers define it as resource. 如果您需要Triggers旁边的Storyboard ,请将其定义为资源。 Then you can reference it in BeginStoryboard using StaticResource and in code you can find it with FindResource or get it directly from the Resources property. 然后,您可以使用StaticResourceBeginStoryboard引用它,并在代码中使用FindResource找到它,或者直接从Resources属性中获取它。

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

The not so nice alternative is actually casting your way down, something like 不太好用的替代方法实际上是让您失望,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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