【问题标题】:c# wpf change button content execute some code and then change it backc# wpf change button content 执行一些代码然后改回来
【发布时间】:2017-05-17 21:59:31
【问题描述】:

我有一个简单的 wpf c# 应用程序,它从 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);
    }
}

我想要做的是将 FindButton 文本从“查找”更改为“正在搜索...”,然后在搜索完成后返回。 我尝试在尝试之前添加以下代码{}:

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

在 try{} 之后将其改回“查找”,但它不起作用。 当我在某处阅读时,我需要在这里使用异步方法或线程。 我在这里找到了一些解决方案并尝试将“异步”添加到我的函数中并更改了代码:

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

但它开始返回以下错误:

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

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

【问题讨论】:

标签: c# wpf multithreading async-await


【解决方案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;
}

【讨论】:

  • 谢谢,它有效。但是是否可以在 MessageBox 弹出之前更改 FindButton.Content ?因为我必须按 OK 并且只有在 MessageBox 消失并且 FindButton.Content 更改之后。
猜你喜欢
  • 2012-01-22
  • 1970-01-01
  • 2015-09-04
  • 2017-04-11
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多