如何在主窗口外使用 ShowProgressAsync 进度条?

How do I use a ShowProgressAsync progress bar outside of the mainwindow?

我试图在搜索字符串中的单词时显示进度条。我知道有更简单的方法来搜索字符串,但我想展示一个简单的例子来说明我想要什么,而不需要大量代码。

MainWindow.xaml.cs

namespace testProgressBars
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        bool isTextBox1Set = false;

        public MainWindow()
        {
            InitializeComponent();
        }


        public class DocumentSearch
        {
            public string searchTerm { get; set; }
            string myString = "this is a text string that I wanted to search through.";

            //This would be async if what I was trying was possible.
            public void SearchDoc()
            {
                int wordCount = myString.Split().Length;

                string[] words = myString.Split(' ');
                int counter = 0;
                foreach (string word in words)
                {
                    counter++;
                    if (word == searchTerm)
                    {
                        MessageBox.Show("yep....it's in here");
                    }
                    else 
                    { 
                        MessageBox.Show("nope.....it's not in here."); 
                    }
                    //I want my progress bar to update here....but this won't work.
                    //var progressBar = await this.ShowProgressAsync("wait for it", "finding words");
                    //progressBar.SetProgress((double)counter / (double)wordCount * 100);

                }

            }
        }

        private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(!String.IsNullOrEmpty(TextBox1.Text))
            {
                Button1.IsEnabled = true;
            }
            else
            {
                Button1.IsEnabled = false;
            }

        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            //get the word inside of the text field


            DocumentSearch docSearch = new DocumentSearch();
            docSearch.searchTerm = TextBox1.Text;
            docSearch.SearchDoc();


        }
    }
}

MainWindow.xaml

<Controls:MetroWindow x:Class="testProgressBars.MainWindow"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Name ="StackPanel1" Margin="50">
            <TextBox Name="TextBox1" Height="25" Width="100" TextChanged="TextBox1_TextChanged"/>
            <Button Name="Button1" Content="Go" IsEnabled="False" Height="25" Width="100" Margin="50" Click="Button1_Click"/>
        </StackPanel>
    </Grid>
</Controls:MetroWindow>

我不确定如何从 DocumentSearch class 显示进度条,因为我认为当前代码的问题是 ShowProgressAsync 需要以某种方式连接到 MainWindow class .

感谢任何帮助。

我不熟悉 MahApps 库,但我觉得基本问题是您不应该在 DocumentSearch 中做任何 UI 事情。相反,它应该提供挂钩以允许调用者接收进度报告并管理对话本身。例如:

    public class DocumentSearch
    {
        public string searchTerm { get; set; }
        string myString = "this is a text string that I wanted to search through.";

        //This would be async if what I was trying was possible.
        public async Task<bool> SearchDoc(IProgress<double> progress)
        {
            int wordCount = myString.Split().Length;

            string[] words = myString.Split(' ');
            int counter = 0;
            foreach (string word in words)
            {
                counter++;
                if (word == searchTerm)
                {
                    return true;
                }

                progress.Report((double)counter / (double)wordCount * 100);
            }

            return false;
        }
    }

这样调用:

    private async void Button1_Click(object sender, RoutedEventArgs e)
    {
        var progressBar = await this.ShowProgressAsync("wait for it", "finding words");
        IProgress<double> progress =
            new Progress<double>(value => progressBar.SetProgress(value));

        //get the word inside of the text field

        DocumentSearch docSearch = new DocumentSearch();
        docSearch.searchTerm = TextBox1.Text;
        bool result = await docSearch.SearchDoc(progress);

        MessageBox.Show(result ? "yep....it's in here" : "nope.....it's not in here.");
    }