How to move between two views after three seconds in WPF C#?

▼魔方 西西 提交于 2020-05-28 08:25:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!