0

Is there a way to fade out and fade in the background image of a HubSection in Windows 8.1 XAML/C# Control?

Actually I have this XAML-Code:

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

I want to fade out the background image each 10 seconds --> change the image --> fade in again.

I have tried this by using the following code lines:

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

But this does not work. If the storyboard has completed the image will change but with no fading effects. Is the HubSection.Background not "animatable"?

4

1 回答 1

1

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

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

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

这是一个类似的问题/答案

于 2013-12-16T03:51:04.540 回答