【发布时间】:2019-07-16 07:44:58
【问题描述】:
我需要获取一个列表作为 wpf 中 DataGrid 的 ItemSource。我可以从我的异步方法中得到结果,如下所示:
public async Task<List<ManualReadTag>> GetManuallyReadTags(ParameterManualTags model { ..... }
我尝试在我的 MainWindow.xaml 中的 DataGrid 中显示它的值,如下所示:
public partial class MainWindow : Window
{
readonly ApplicationController _ac = new ApplicationController();
private Task<List<ManualReadTag>> _manualReadTagList = null;
public MainWindow()
{
InitializeComponent();
}
private void BtnGEtManualCount_OnClick(object sender, RoutedEventArgs e)
{
GetManuallyReadTags();
}
private void GetManuallyReadTags()
{
var model = new ParameterManualTags
{
Lane = Convert.ToInt32(TxtLane.Text),
Plaza = Convert.ToInt32(TxtLane),
DateTo = DateTo.DisplayDate,
DateFrom = DateFrom.DisplayDate
};
_manualReadTagList = _ac.GetManuallyReadTags(model);
ViewingGrid.ItemsSource = _manualReadTagList;
}
}
但在 ViewingGrid.ItemsSource = _manualReadTagList;它给了我一个错误:
严重性代码描述项目文件行抑制状态 错误 CS0266 无法隐式转换类型 'System.Threading.Tasks.Task>' 到“System.Collections.IEnumerable”。存在显式转换 (你错过了一个 投?)TagReporting D:\Projects\Lane\Antenna_Reading\TagReporting\TagReporting\MainWindow.xaml.cs 38 活动
如何将异步 Task 方法的结果用作数据网格的 ItemSource?谢谢。
【问题讨论】:
-
你应该
await_ac.GetManuallyReadTags(model);打电话