WPF UI窗口未更新

时间:2017-10-05 13:06:47

标签: c# wpf backgroundworker splash-screen

我有一个包含标签和动画微调器的窗口。这个窗口是一种闪屏,上面写着“正在加载...”。

这是一个简单的基本窗口:

<Window x:Class="MyWin.SplashScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    <!-- Grid and within the label at row 0, and the spinner at row 1 -->
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="50*"></RowDefinition>
        <RowDefinition Height="20*"></RowDefinition>
    </Grid.RowDefinitions>
    <Label Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">Loading...</Label>  
    <Grid Name="mySpinner" Grid.Row="1"></Grid>
</Grid>

</Window>

代码

public partial class SplashScreen: Window
{
    public SplashScreen()
    {
        InitializeComponent();

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
        // Here I instantiate the spinner control and show in UI 
        UCSpinner myUCSpinner = new UCSpinner();

        host.Child = myUCSpinner;
        this.mySpinner.Children.Add(host);
    }
}

动画微调器是一个在窗口加载事件中实例化并加载的控件:Window_Loaded

然后从我的主wpf窗口,我有一个按钮。单击按钮时,我创建了一个后台工作程序,我启动后台工作程序,然后调用上面指示的启动窗口。见下文。

主窗口按钮

<Button Grid.Column="1" Width="75" Command="{Binding LaunchOperationCommand}" Content="Calculate" HorizontalAlignment="Right">
</Button>

代码

        RelayCommand launchOperationCommand;
        public ICommand LaunchOperationCommand
        {
            get
            {
                if (launchOperationCommand== null)
                    launchOperationCommand= new RelayCommand(param => this.DoLongOperation(), param => true);

                return launchOperationCommand;
            }
        }


        private void DoLongOperation()
        {
            BWorker = null;

            if (BWorker == null)
            {
                BWorker = new BackgroundWorker();
                BWorker.WorkerReportsProgress = false;
                BWorker.WorkerSupportsCancellation = false;

                BWorker.DoWork += new DoWorkEventHandler(BWorker_LongOp_DoWork);
                BWorker.ProgressChanged += new ProgressChangedEventHandler(BWorker_LongOp_ProgressChanged);
                BWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BWorker_LongOp_RunWorkerCompleted);
            }

            if (BWorker.IsBusy != true)
            {
                BWorker.RunWorkerAsync();
                ShowSplashScreen();
            }
        }

        private System.Windows.Window splashWindow = null;
        private void ShowSplashScreen()
        {
            if (splashWindow == null)
            {
                splashWindow = new SplashScreen();
            }

            if (splashWindow .IsVisible == false)
                splashWindow .ShowDialog();
        }

问题如下:显示初始屏幕中的标签,但不显示微调器。

0 个答案:

没有答案