Upgrading to .NET 4.5: An ItemsControl is inconsistent with its items source

后端 未结 4 1758
心在旅途
心在旅途 2020-12-08 07:24

I\'m building an application, which uses many ItemControls(datagrids and listviews). In order to easily update these lists from background threads I used this extension to O

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 07:57

    This is for Windows 10 Version 1607 users using the release version of VS 2017 that may have this issue.

    Microsoft Visual Studio Community 2017
    Version 15.1 (26403.3) Release
    VisualStudio.15.Release/15.1.0+26403.3
    Microsoft .NET Framework
    Version 4.6.01586
    

    You didn't need the lock nor EnableCollectionSynchronization.

    
        
            
                
                    
    
                
            
        
    
    
    public ObservableCollection fontFamilyItems;
    public ObservableCollection FontFamilyItems
    {
        get { return fontFamilyItems; }
        set { SetProperty(ref fontFamilyItems, value, nameof(FontFamilyItems)); }
    }
    
    public string fontFamilyItem;
    public string FontFamilyItem
    {
        get { return fontFamilyItem; }
        set { SetProperty(ref fontFamilyItem, value, nameof(FontFamilyItem)); }
    }
    
    private List GetItems()
    {
        List fonts = new List();
        foreach (System.Windows.Media.FontFamily font in Fonts.SystemFontFamilies)
        {
            fonts.Add(font.Source);
            ....
            other stuff..
        }
        return fonts;
    }
    
    public async void OnFontFamilyViewLoaded(object sender, EventArgs e)
    {
        DisposableFontFamilyViewLoaded.Dispose();
        Task> getItemsTask = Task.Factory.StartNew(GetItems);
    
        try
        {
            foreach (string item in await getItemsTask)
            {
                FontFamilyItems.Add(item);
            }
        }
        catch (Exception x)
        {
            throw new Exception("Error - " + x.Message);
        }
    
        ...
        other stuff
    }
    

提交回复
热议问题