简体   繁体   English

HubSection背景的双重动画(Windows 8.1,XAML,C#)

[英]Double Animation of HubSection Background (Windows 8.1, XAML, C#)

Is there a way to fade out and fade in the background image of a HubSection in Windows 8.1 XAML/C# Control? Windows 8.1 XAML / C#控件中是否可以淡出或淡入HubSection的背景图像?

Actually I have this XAML-Code: 其实我有这个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>

I want to fade out the background image each 10 seconds --> change the image --> fade in again. 我想每10秒淡出背景图像->更改图像->再次淡入。

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"? HubSection.Background是否不是“可动画的”?

I do not believe you can reference controls in the code behind once they are in a HubSection. 我认为一旦控件位于HubSection中,您就无法在其后面的代码中引用控件。 So your Storyboard.SetTarget is probably the culprit here where it tries to reference Section0Background. 因此,您的Storyboard.SetTarget可能是此处试图引用Section0Background的元凶。 You can probably build most of this animation in XAML but for the changing of the image what you will have to do is bind the ImageBrush's ImageSource property and set the DataContext of the HubSection. 您可能可以在XAML中构建大多数动画,但是要更改图像,您需要绑定ImageBrush的ImageSource属性并设置HubSection的DataContext。

Then in your code behind when the storyboard completed event fires, you change your ViewModel (which is set as the HubSection's DataContext) which raises an event that will rebind your ImageSource property. 然后,在情节提要完成事件触发后的代码中,更改ViewModel(设置为HubSection的DataContext),这将引发一个事件,该事件将重新绑定ImageSource属性。

In summary you can't reference controls in HubSections. 总之,您不能在HubSections中引用控件。 You can only change properties through Data Binding. 您只能通过数据绑定更改属性。

Here's a similar question/answer . 这是一个类似的问题/答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM