0

我试图创建一个“连接到服务器”窗口。此窗口需要在程序的打开启动屏幕之后显示。该窗口仅包含一个 MediaElement,在此 MediaElement 中我需要显示一个 .avi 文件。在 Window 的 .cs 文件中,我正在创建 Socket,它需要连接到我的服务器并检查更新。此外,当响应(来自服务器)被接受时,我不希望启动屏幕停止显示 .avi 文件。

我的问题是窗口不显示 .avi 文件。当我用 mp3 文件(用于测试..)替换 .avi 文件时,它执行了相同的结果。

通常,我的代码如下所示:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.MediaElement.Play();

        ------------------------------
        |  Socket's Code Comes Here  |
        ------------------------------

        if (this.IsUpdateNeeded == true) //IsUpdateNeeded == My Own Variable..
        {
            MessageBox.Show("New Version Is Here !", "New Version Is Here !", MessageBoxButtons.OK, .MessageBoxIcon.Information);
            GoToWebsite();
        }
        else
        {
            MessageBox.Show("You Own The Latest Version", "No Update Is Needed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        this.MediaElement.Stop();
        this.Close();
    }

谁能帮我弄清楚吗?

4

1 回答 1

1

在另一个线程中运行的代码(因此不会阻塞主 UI 线程并停止视频播放)

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.MediaElement.Play();

    Task.Factory.StartNew(
        new Action(delegate()
        {
          /*
           * ------------------------------
             Socket's Code Comes Here  |
             ------------------------------
           */

          Dispatcher.Invoke(
                      new Action(delegate()
                      {

                            if (this.IsUpdateNeeded == true) //IsUpdateNeeded == My Own Variable..
                            {
                                       MessageBox.Show("New Version Is Here !", "New Version Is Here !", MessageBoxButton.OK, MessageBoxImage.Information);
                                       GoToWebsite();
                            }
                            else
                           {
                               MessageBox.Show("You Own The Latest Version", "No Update Is Needed", MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                           this.MediaElement.Stop();
                           this.Close();   
                      }                
                    ));
                }
            )
        );
    }
于 2013-07-31T20:40:46.610 回答