Я пытаюсь показать индикатор выполнения, пока я просматриваю слова в строке. Я знаю, что есть более простые способы поиска по строкам, но я пытаюсь показать простой пример того, что я хочу, без тонны кода.Как использовать индикатор выполнения ShowProgressAsync за пределами основного окна?
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, так как я думаю, что проблема с текущим кодом заключается в том, что ShowProgressAsync необходимо каким-то образом подключить к классу MainWindow.
Любая помощь приветствуется.
Фантастический ответ, спасибо. Ответил больше вопросов, чем я спросил. – trueCamelType