我正在寻找:
例如,我有2个用户控件:uc1.xaml和uc2.xaml以及我的应用程序中的1个主窗口。现在,当应用程序打开时,主窗口将显示第一个用户控件,即uc1。因此,当主窗口显示uc1时,如何设置幻灯片效果或任何其他效果的动画。另外,当我点击uc1上的按钮时,主窗口从uc1切换到uc2。我还需要在这一步有动画。
请帮我一些示例代码。
注意:我使用的是wpf桌面应用程序,它是" NOT" winforms申请。
答案 0 :(得分:0)
要为uc1外观设置动画,您可以处理您的MainWindow的已加载事件并从中启动任何动画:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ShowUserControl1().Begin();
}
private Storyboard ShowUserControl1()
{
// var control1 = new uc1(); uncomment it if the control does not exist
control1.RenderTransform = new TranslateTransform();
Root.Children.Add(im);
// Root is your root container. In this case - Grid
// and you have to add it only in the case if it does not exists
Storyboard sb = new Storyboard();
DoubleAnimation slide = new DoubleAnimation();
slide.To = 0;
slide.From = Root.ActualWidth;
slide.Duration = new Duration(TimeSpan.FromMilliseconds(400));
// Set the target of the animation
Storyboard.SetTarget(slide, control1);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
// Kick the animation off
sb.Children.Add(slide);
return sb;
}
处理按钮的Click事件以对uc2执行相同操作。
如果你想隐藏uc1然后在点击按钮时显示uc2,你必须这样做:
Storyboard sbHideUC1 = HideUserControl1(); //you will have to implement HideUserControl1() that returns Storyboard
sbHideUC1.Completed += (s, e) =>
{
var sbShowUC2 = ShowUserControl2(); // you will have to implement ShowUserControl2() that returns Storyboard
sbShowUC2.Begin();
}
sbHideUC1.Begin();