问题
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.
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?
Thanks in advance!
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;
}
回答1:
May be help:
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;
}
Edited as @aepot suggest
来源:https://stackoverflow.com/questions/61197436/how-to-move-between-two-views-after-three-seconds-in-wpf-c