如何在 WPF C# 中三秒后在两个视图之间移动? [英] How to move between two views after three seconds in WPF C#?
问题描述
我目前正在处理一个项目,我有一个图像,我想显示 3 秒钟,然后在剩下的运行中隐藏它并显示主网格.
I am currently working on a project and I have an image which I want to show for 3 seconds, and then hide it for the rest of the run and show the main grid.
我尝试做的是将主网格放在子网格中,不透明度为 0 或 Visibility = Visibility.Hidden,并在公共 MainWindow() {} 方法的代码后面实现一个秒表.当我尝试 if 语句时:if (stopwatch.ElapsedMilliseconds > 3000) {Change Opacity},我还没有达到条件并与第一个窗口堆叠.当我尝试使用 while 方法时,只需添加一个空的 while 循环,三秒钟内什么都没有显示,然后我立即看到了主网格.
What I tried to do is to put the main grid in a sub grid, with opacity 0 or Visibility = Visibility.Hidden, and implement a stopwatch in the code behind of the public MainWindow() {} Method. When I tried an if Statement: if (stopwatch.ElapsedMilliseconds > 3000) {Change Opacity}, I haven't reached the condition and stacked with the first window. When I tried a while approach, by simply adding an empty while loop, nothing was shown up for three seconds, and then I am seeing the main grid straight away.
我怎样才能得到想要的结果?
How can I get the desired result?
提前致谢!
public MainWindow()
{
InitializeComponent();
ViewModel = (Application.Current as App).VM;
DataContext = ViewModel;
Dashboard.DataContext = ViewModel;
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
this.Loaded += new RoutedEventHandler(myMediaElement_MediaEnded);
TimeGrinder();
Page1.Opacity = 0;
MainGrid.Opacity = 100;
}
public void TimeGrinder()
{
var stopWatch = new Stopwatch();
stopWatch.Start();
while (stopWatch.ElapsedMilliseconds < 3000) { }
return;
}
推荐答案
可能有帮助:
using System.Threading;
...
private async void HideGrid()
{
Page1.Opacity = 0;
MainGrid.Opacity = 100;
await Task.Delay(3000);
//await Task.Run(() => Thread.Sleep(3000));
Page1.Opacity = 100;
MainGrid.Opacity = 0;
}
按照@aepot 建议编辑
这篇关于如何在 WPF C# 中三秒后在两个视图之间移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!