从任务中显示 WPF window
Show WPF window from within a task
我在我的应用程序的主 window 上有一个 "import xlsx" 函数,其中 return 一个具有导入结果信息的对象,我从这样的任务中启动此函数
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
System.Threading.Tasks.Task.Run(() =>
{
result = XlsxImport.Import(openFileDialog.FileName);
return result;
})
.ContinueWith(r =>
{
if (r.Result.Errors.Count > 0)
{
if (MessageBox.Show("Import finished with " + r.Result.Errors.Count.ToString() + " errors. An error list will be shown.", "Errors occurred during import", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
ImportErrorsView importErrorWindow = new ImportErrorsView(r.Result);
importErrorWindow.Show();
}
}
else
{
MessageBox.Show("Import succesfully completed");
}
}, scheduler);
importErrorWindow 是一个简单的 window,带有一个我绑定错误列表的网格。
但是当显示 importErrorWindow 时,它的网格包含正确的行数,但它们都是空的,在控制台中我除了绑定错误什么也看不到,而我 100% 确定绑定是正确的(我检查了一步一步的调试)
我也尝试删除列绑定并将 AutoGenerateColums 网格 属性 设置为 True,但结果是一样的:正确的行数但全部为空白
为什么会这样?与任务有关吗?
更新
这是我的 window
的代码
<Window x:Class="MyApp.Module.WBS.Views.ImportErrorsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Title="ImportErrorsReport"
xmlns:vm="clr-namespace:MyApp.Module.WBS.ViewModels">
<Window.Resources>
<vm:WBSImportErrorsViewModel x:Key="WBSImportErrorsViewModel" />
</Window.Resources>
<Grid x:Name="MainGrid" DataContext="{StaticResource WBSImportErrorsViewModel}">
<telerik:RadGridView Name="ErrorList" CanUserFreezeColumns="False" CanUserInsertRows="False" GroupRenderMode="Flat"
AutoGenerateColumns="False" ItemsSource="{Binding ImportErrors}"
ShowGroupPanel="False" EnableRowVirtualization="True" EnableColumnVirtualization="False"
FilteringMode="FilterRow" RowIndicatorVisibility="Collapsed" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Line" DataMemberBinding="{Binding XlsxLine}" Width="60" />
<telerik:GridViewDataColumn Header="ErrorMessage" DataMemberBinding="{Binding ErrorMessage}" Width="400" />
<telerik:GridViewDataColumn Header="Exception Message" DataMemberBinding="{Binding Exception.Message}" Width="300" />
<telerik:GridViewDataColumn Header="Exception InnerException" DataMemberBinding="{Binding Exception.InnerException}" Width="400" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</Window>
namespace MyApp.Module.WBS.Views
{
public partial class ImportErrorsView : Window
{
ImportResult ir;
public ImportErrorsView()
{
InitializeComponent();
}
public ImportErrorsView(ImportResult ir)
{
InitializeComponent();
((WBSImportErrorsViewModel)this.MainGrid.DataContext).ImportErrors = new ObservableCollection<ImportError>(ir.Errors);
}
}
}
视图模型:
namespace MyApp.Module.WBS.ViewModels
{
public class WBSImportErrorsViewModel : ViewModelBase
{
private ObservableCollection<ImportError> _importErrors;
public ObservableCollection<ImportError> ImportErrors
{
get
{
return this._importErrors;
}
set
{
this._importErrors = value;
this.OnPropertyChanged(() => this.ImportErrors);
}
}
}
}
和对象
public class ImportResult
{
public List<ImportError> Errors;
public ImportResult()
{
//other non-relevant fields
Errors = new List<ImportError>();
}
}
public class ImportError
{
public int? XlsxLine;
public string ErrorMessage;
public Exception Exception;
public ImportError(int? xlsxLine, string errorMessage, Exception exception)
{
XlsxLine = xlsxLine;
ErrorMessage = errorMessage;
Exception = exception;
}
}
class ImportError
应该将字段 XlsxLine, ErrorMessage, Exception
作为属性。如果您希望各个属性在发生更改时通知网格,您还应该实施 INotifyPropertyChange
。
我在我的应用程序的主 window 上有一个 "import xlsx" 函数,其中 return 一个具有导入结果信息的对象,我从这样的任务中启动此函数
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
System.Threading.Tasks.Task.Run(() =>
{
result = XlsxImport.Import(openFileDialog.FileName);
return result;
})
.ContinueWith(r =>
{
if (r.Result.Errors.Count > 0)
{
if (MessageBox.Show("Import finished with " + r.Result.Errors.Count.ToString() + " errors. An error list will be shown.", "Errors occurred during import", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
ImportErrorsView importErrorWindow = new ImportErrorsView(r.Result);
importErrorWindow.Show();
}
}
else
{
MessageBox.Show("Import succesfully completed");
}
}, scheduler);
importErrorWindow 是一个简单的 window,带有一个我绑定错误列表的网格。
但是当显示 importErrorWindow 时,它的网格包含正确的行数,但它们都是空的,在控制台中我除了绑定错误什么也看不到,而我 100% 确定绑定是正确的(我检查了一步一步的调试)
我也尝试删除列绑定并将 AutoGenerateColums 网格 属性 设置为 True,但结果是一样的:正确的行数但全部为空白
为什么会这样?与任务有关吗?
更新 这是我的 window
的代码<Window x:Class="MyApp.Module.WBS.Views.ImportErrorsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Title="ImportErrorsReport"
xmlns:vm="clr-namespace:MyApp.Module.WBS.ViewModels">
<Window.Resources>
<vm:WBSImportErrorsViewModel x:Key="WBSImportErrorsViewModel" />
</Window.Resources>
<Grid x:Name="MainGrid" DataContext="{StaticResource WBSImportErrorsViewModel}">
<telerik:RadGridView Name="ErrorList" CanUserFreezeColumns="False" CanUserInsertRows="False" GroupRenderMode="Flat"
AutoGenerateColumns="False" ItemsSource="{Binding ImportErrors}"
ShowGroupPanel="False" EnableRowVirtualization="True" EnableColumnVirtualization="False"
FilteringMode="FilterRow" RowIndicatorVisibility="Collapsed" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Line" DataMemberBinding="{Binding XlsxLine}" Width="60" />
<telerik:GridViewDataColumn Header="ErrorMessage" DataMemberBinding="{Binding ErrorMessage}" Width="400" />
<telerik:GridViewDataColumn Header="Exception Message" DataMemberBinding="{Binding Exception.Message}" Width="300" />
<telerik:GridViewDataColumn Header="Exception InnerException" DataMemberBinding="{Binding Exception.InnerException}" Width="400" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</Window>
namespace MyApp.Module.WBS.Views
{
public partial class ImportErrorsView : Window
{
ImportResult ir;
public ImportErrorsView()
{
InitializeComponent();
}
public ImportErrorsView(ImportResult ir)
{
InitializeComponent();
((WBSImportErrorsViewModel)this.MainGrid.DataContext).ImportErrors = new ObservableCollection<ImportError>(ir.Errors);
}
}
}
视图模型:
namespace MyApp.Module.WBS.ViewModels
{
public class WBSImportErrorsViewModel : ViewModelBase
{
private ObservableCollection<ImportError> _importErrors;
public ObservableCollection<ImportError> ImportErrors
{
get
{
return this._importErrors;
}
set
{
this._importErrors = value;
this.OnPropertyChanged(() => this.ImportErrors);
}
}
}
}
和对象
public class ImportResult
{
public List<ImportError> Errors;
public ImportResult()
{
//other non-relevant fields
Errors = new List<ImportError>();
}
}
public class ImportError
{
public int? XlsxLine;
public string ErrorMessage;
public Exception Exception;
public ImportError(int? xlsxLine, string errorMessage, Exception exception)
{
XlsxLine = xlsxLine;
ErrorMessage = errorMessage;
Exception = exception;
}
}
class ImportError
应该将字段 XlsxLine, ErrorMessage, Exception
作为属性。如果您希望各个属性在发生更改时通知网格,您还应该实施 INotifyPropertyChange
。