【问题标题】:Double Animation of HubSection Background (Windows 8.1, XAML, C#)HubSection 背景的双重动画(Windows 8.1、XAML、C#)
【发布时间】:2013-11-02 21:40:20
【问题描述】:

有没有办法在 Windows 8.1 XAML/C# 控件中淡出和淡入 HubSection 的背景图像?

其实我有这个 XAML 代码:

<HubSection x:Name="Section0" Width="700" Margin="0,0,80,0" VerticalContentAlignment="Bottom">
   <HubSection.Background>
      <ImageBrush x:Name="Section0Background" ImageSource="/Assets/images/img1.jpg" Stretch="UniformToFill" />
   </HubSection.Background>
   <!--... some other markup ... -->
</HubSection>

我想每 10 秒淡出背景图像 --> 更改图像 --> 再次淡入。

我已经尝试过使用以下代码行:

Storyboard storyboard = new Storyboard();

DoubleAnimation animation = new DoubleAnimation();
animation.From = 1.0;
animation.To = 0.0;
animation.BeginTime = TimeSpan.FromSeconds(0);
animation.Duration = new Duration(TimeSpan.FromMilliseconds(200));

storyboard.Children.Add(animation);

Storyboard.SetTargetProperty(animation, "Opacity");
Storyboard.SetTarget(animation, Section0Background);

storyboard.Completed += storyboard_Completed; // --> on complete change image and fade in
storyboard.Begin();

但这不起作用。如果情节提要完成,图像将发生变化,但没有褪色效果。 HubSection.Background 不是“动画”吗?

【问题讨论】:

    标签: c# windows-runtime storyboard winrt-xaml windows-8.1


    【解决方案1】:

    我不相信一旦它们位于 HubSection 中,您就不能在后面的代码中引用控件。所以你的 Storyboard.SetTarget 可能是它试图引用 Section0Background 的罪魁祸首。您可能可以在 XAML 中构建此动画的大部分内容,但要更改图像,您必须绑定 ImageBrush 的 ImageSource 属性并设置 HubSection 的 DataContext。

    然后在情节提要完成事件触发时在您的代码中更改您的 ViewModel(设置为 HubSection 的 DataContext),这将引发一个事件,该事件将重新绑定您的 ImageSource 属性。

    总而言之,您不能在 HubSections 中引用控件。您只能通过数据绑定更改属性。

    这是一个类似的question/answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多