简体   繁体   中英

How to extract Storyboard from Viewport3d.Triggers in wpf

I have a separate xaml file that looks like this :

<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. 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. 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.

<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;

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