特定の基準に一致するファイルをディレクトリで検索するプログラムがあります。この検索プロセスには時間がかかるため、非同期で呼び出す必要があります。検索アルゴリズムがファイルを見つけると、イベントがトリガーされます。私のMainWindow
インスタンスはこのイベントをリッスンし、GUI を更新する必要があります。これらの「追加された」ファイルをにバインドするにはどうすればよいListView
ですか? インスタンスを使用できると考えましたがObservableCollection<FileInfo>
、それをバインドする方法がわかりません。
無関係なコントロールとコードをすべて取り除きました。関連する 2 つのファイルを次に示します。
MainWindow.xaml:
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CR Search" Height="395" Width="525">
<Grid>
<ListView x:Name="Results">
<ListView.View>
<GridView>
<GridViewColumn Header="Filename"/>
<GridViewColumn Header="Directory"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.IO;
using System.Threading.Tasks;
public partial class MainWindow
{
private SearchLogic _backgroundSearch;
private async void Search(object sender, RoutedEventArgs e)
{
// TODO: clear Results
_backgroundSearch = new SearchLogic("", new DirectoryInfo("C:\"));
_backgroundSearch.FileAdded += FileAdded;
await Task.Run(new Action(_backgroundSearch.Search));
}
private void FileAdded(object sender, FileAddedEventArgs eventArgs)
{
// TODO: add eventArgs.File to Results
// eventArgs.File is an instance of FileInfo
}
}