c#wpf更改按钮内容执行一些代码,然后将其更改回来

时间:2017-05-17 13:49:15

标签: c# wpf multithreading async-await

我有一个简单的wpf c#app,它从inputField获取文本,尝试在那里找到一些信息并将结果返回到outputField。 这是代码:

private void FindButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text));
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
        else
        {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

我想要做的是从&#34; Find&#34;中更改FindButton文本。到&#34;搜索...&#34;并在搜索完成时返回。 我尝试在尝试{}之前添加以下代码:

FindButton.Content = "Searching...";
FindButton.IsEnabled = false;

在尝试{}之后将其更改为&#34;查找&#34;但它没有用。 当我在某处阅读时,我需要在这里使用异步方法或线程。 我在这里找到了一些解决方案并尝试添加&#34; async&#34;我的功能,也改变了代码:

await Task.Run(() => {
//My code which is above
});

但它开始返回以下错误:

  

MS.Internal.PtsHost.UnsafeNativeMethods.PTS.SecondaryException   的NullReferenceException

我对这些主题完全陌生,并且不知道如何使其发挥作用。请有人帮忙。

1 个答案:

答案 0 :(得分:1)

假设您的findAddresses()方法访问UI元素并且必须在UI线程上执行,您可以尝试使用Task.Delay为UI线程提供更新FindButton之前的机会你踢了你的手术。试试这个:

private async void FindButton_Click(object sender, RoutedEventArgs e)
{
    FindButton.Content = "Searching...";
    FindButton.IsEnabled = false;
    await Task.Delay(1);

    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text)) ;
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
                else
                {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }

    FindButton.Content = "Default";
    FindButton.IsEnabled = true;
}