从任务中显示WPF窗口

本文关键字:WPF 窗口 显示 任务 | 更新日期: 2023-09-27 18:17:55

我在应用程序的主窗口上有一个"import xlsx"函数,它返回一个带有导入结果信息的对象,我从这样的任务启动这个函数

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);

importterrorwindow是一个带有网格的简单窗口,我在其中绑定了错误列表。

但是当importterrorwindow显示时,它的网格包含正确的行数,但它们都是空的,在控制台我只能看到绑定错误,而我100%肯定绑定是正确的(我检查调试它一步一步)

我还试图删除列绑定并将AutoGenerateColums网格属性设置为True,但结果是相同的:正确的行数,但全部为空白

为什么会发生这种情况?它与任务有关吗?


这是我的窗口

的代码
<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);
        }        
    }
}

ViewModel:

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;
    }
}

从任务中显示WPF窗口

ImportError应具有XlsxLine, ErrorMessage, Exception字段作为属性。如果你想让单个属性在发生变化时通知网格,你也应该实现INotifyPropertyChange