storyboard - 使用 Storyboard 代码在 Windows 通用应用程序中的资源字典中工作

标签 storyboard winrt-xaml windows-8.1 windows-phone-8.1 win-universal-app

这里需要一些有关 Storyboard的帮助。在我的 MainPage.xaml 中,我有一个如下所示的 Storyboard代码:

<Page.Resources>
    <Storyboard x:Name="BackStoryBoard">
        <DoubleAnimation Duration="0" To="4.55" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="image" d:IsOptimized="True"/>
        <DoubleAnimationUsingKeyFrames AutoReverse="True" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="image">
            <EasingDoubleKeyFrame KeyTime="0" Value="710"/>
            <EasingDoubleKeyFrame KeyTime="0:0:30" Value="-710"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Page.Resources>

现在,在我的网格中,我有一个如下所示的图像代码(我只有一个网格):

<Grid>
    <Image x:Name="image" Source="Images/Background4.png" Stretch="Fill" RenderTransformOrigin="0.5,0.5">
        <Image.RenderTransform>
            <CompositeTransform/>
        </Image.RenderTransform>
    </Image>

</Grid>

从后面的代码来看,我在 MainPage 构造函数中添加了以下代码:

    BackStoryBoard.Begin();

正如我们所看到的,这个 Storyboard使应用程序的背景图像在每 30 秒的时间间隔后从右到左和从左到右移动。

现在,我有一些页面,我想在所有页面中使用相同的 Storyboard效果。我可以在所有页面中复制粘贴相同的代码,但这不是一个好的做法。因此,我创建了一个名为 Styles.xaml 的资源字典。

所以,我的问题是,如何在资源字典中编写 Storyboard 的代码并在不同的 XAML 页面中使用它?即使我添加了 Storyboard,我也无法调用 BackStoryBoard.Begin(); 因为它不可访问。那么,我该如何解决这个问题呢?任何帮助将不胜感激。 :-)

最佳答案

尝试,

将资源字典合并到所有需要的 xaml 页面中,例如,

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

从代码后面,

var storyboard = ((this.Resources as ResourceDictionary).MergedDictionaries[0] as ResourceDictionary)["BackStoryBoard"] as Storyboard;
Storyboard.SetTarget(storyboard, image);
storyboard.Begin();

希望这能解决您的问题。

关于storyboard - 使用 Storyboard 代码在 Windows 通用应用程序中的资源字典中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24842144/

相关文章:

php - 在 XAMPP (WINDOWS 8.1) 中将 PHP 中的 Python 作为脚本运行

Android adb devices 没有检测到我的手机

ios - 选项卡栏 Controller 问题内的 UI 导航 Controller

ios - 如何制作像 Facebook 应用程序一样的登录屏幕?

ios - 当多人编辑分镜时,如何管理分镜的开发?

c# - Windows 8 将属性属性名称绑定(bind)到 UI

ios - 指定委托(delegate)从 Storyboard快速查看

xaml - 语义缩放的自定义点击处理程序

xaml - 使用 RichtextBlock 格式化文本

xaml - Behaviors SDK 的行为不一致