在UI内显示忙碌指示器控件

时间:2012-03-12 09:20:10

标签: c# wpf buttonclick busyindicator

我修改了代码,但现在我有另一个问题。在验证用户信息的if语句中发生InvalidOperation异常。它说调用线程无法访问此对象,因为不同的线程拥有它。任何sugestions?

 private void finishConfigButton_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;
        bool validated = false;

        errorLabel.Visibility = System.Windows.Visibility.Collapsed;
        validationProfile.IsBusy = true;
        finishConfigButton.IsEnabled = false;
        backToLoginHyperlink.IsEnabled = false;

        worker.DoWork += (o, ea) =>
        {
            if (newUser.ValidateNewUserInformation(newNameTextBox.Text, newEmailTextBox.Text, newUsernameTextBox.Text, newPasswordPasswordBox.Password, ref errorLabel))
            {
                validated = true;

                string activeDir = Environment.SystemDirectory.Substring(0, 1) + @":\Users\" + Environment.UserName + @"\My Documents\SSK\Users";
                string newPath = System.IO.Path.Combine(activeDir, newUser.Username);
                Directory.CreateDirectory(newPath);

                newUser.SaveUserData(newUser);

                newPath = System.IO.Path.Combine(activeDir, newUser.Username + @"\Settings");
                Directory.CreateDirectory(newPath);

                newUserSettings.SetDefaultValues();
                newUserSettings.SaveSettings(newUser, newUserSettings);
            }
            else
                validated = false;

            if (worker.CancellationPending)
            {
                ea.Cancel = true;
                return;
            }
        };

        worker.RunWorkerCompleted += (o, ea) =>
        {
            validationProfile.IsBusy = false;
            finishConfigButton.IsEnabled = true;
            backToLoginHyperlink.IsEnabled = true;
        };

        worker.RunWorkerAsync(this);

        if (validated)
        {
            IntelliMonitorWindow intelliMonitor = new IntelliMonitorWindow(newUser, newUserSettings);
            intelliMonitor.Show();
            this.Close();
        }
        else
            errorLabel.Visibility = System.Windows.Visibility.Visible;
    }

4 个答案:

答案 0 :(得分:1)

您在这里做的是在UI线程上运行所有内容。这意味着在繁重的代码运行时,您正在阻止重新绘制UI,因此在方法结束时,不会更新validationProfile,其中IsBusy设置为false。

你需要做的是将繁重的代码处理成一个新的线程,它可以同时更新UI。

看一下由Extended Toolkit的创建者Brian Lagunas撰写的这篇博客文章: http://elegantcode.com/2011/10/07/extended-wpf-toolkitusing-the-busyindicator/

他解释了如何将BusyIndi​​cator与BackgroundWorker一起使用。

答案 1 :(得分:1)

XAML代码中的忙指示符没有任何内容。把一些控件放进去:

<wpfet:BusyIndicator Name="validationProfile" IsBusy="False" BusyContent="Working...Please wait"  DisplayAfter="0" Background="DimGray">
    <Grid>
        ...
    </Grid>
</wpfet:BusyIndicator>

如果您更改为忙,那些控件将被禁用,BusyIndi​​cator将显示在它们上方。

我想你想用BusyIndi​​cator包装整个<Grid Background="LightGray">

答案 2 :(得分:0)

使用后台工作程序或新线程来运行繁重的进程并将UI线程设置为空闲。这有助于您即使在后台进程正在运行时也更新UI

例如:

public void finishConfigButton_Click()
{
    worker = new BackgroundWorker();

    worker.DoWork += delegate(object s, DoWorkEventArgs args)
    {
        //Do the heavy work here
    };

    worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
    {
        //Things to do after the execution of heavy work
        validationProfile.IsBusy = false;
    };

    validationProfile.IsBusy= true;
    worker.RunWorkerAsync();
    }
}

答案 3 :(得分:0)

我终于明白了。你不能在worker.DoWork块中使用UI对象。我稍微修改了代码,它现在可以工作了。

 private void finishConfigButton_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;

        errorLabel.Visibility = System.Windows.Visibility.Collapsed;
        validationProfile.IsBusy = true;
        finishConfigButton.IsEnabled = false;
        backToLoginHyperlink.IsEnabled = false;

        bool validated = false;
        string newName = newNameTextBox.Text;
        string newEmail = newEmailTextBox.Text;
        string newUsername = newUsernameTextBox.Text;
        string newPassword = newPasswordPasswordBox.Password;
        string errorMessage = "Unknown error.";

        worker.DoWork += (o, ea) =>
        {
            if (newUser.ValidateNewUserInformation(newName, newEmail, newUsername, newPassword, ref errorMessage))
            {
                string activeDir = Environment.SystemDirectory.Substring(0, 1) + @":\Users\" + Environment.UserName + @"\My Documents\SSK\Users";
                string newPath = System.IO.Path.Combine(activeDir, newUser.Username);
                Directory.CreateDirectory(newPath);

                newUser.SaveUserData(newUser);

                newPath = System.IO.Path.Combine(activeDir, newUser.Username + @"\Settings");
                Directory.CreateDirectory(newPath);

                newUserSettings.SetDefaultValues();
                newUserSettings.SaveSettings(newUser, newUserSettings);

                validated = true;
            }
            else
                ea.Cancel = true;
        };

        worker.RunWorkerCompleted += (o, ea) =>
        {
            if (validated)
            {
                IntelliMonitorWindow intelliMonitor = new IntelliMonitorWindow(newUser, newUserSettings);
                intelliMonitor.Show();
                this.Close();
            }

            validationProfile.IsBusy = false;
            finishConfigButton.IsEnabled = true;
            backToLoginHyperlink.IsEnabled = true;
            errorLabel.Visibility = System.Windows.Visibility.Visible;
            errorLabel.Content = errorMessage;
        };

        worker.RunWorkerAsync();
    }