【问题标题】:Use Storyboard code to work from Resource Dictionary in Windows Universal App使用 Storyboard 代码从 Windows 通用应用程序中的资源字典工作
【发布时间】:2014-07-21 15:34:11
【问题描述】:

在此处需要有关 Storyboard 的一些帮助。在我的 MainPage.xaml 中,我有一个故事板代码,如下所示:

<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 的资源字典。

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

【问题讨论】:

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


    【解决方案1】:

    试试,

    在所有需要的 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();
    

    希望这能解决您的问题。

    【讨论】:

    • 无法在对象“System.Windows.Media.Animation.Storyboard”上设置属性,因为它处于只读状态。'
    猜你喜欢
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多