在后台更新datacontext时闪烁

时间:2017-03-28 10:10:23

标签: xaml binding uwp

我正在通过Windows 10开发为绝对的初学者学习UWP,我遇到了一些问题。 重新刷新我的ObservableCollection<>数据将导致屏幕闪烁。我如何解决它? 计划详情位于UWP beginner

    //CS FILE CODE

    public sealed partial class FinancialPage : Page
    {
        ObservableCollection<NewsItem> NewsItems;
        public FinancialPage()
        {
            NewsItems = new ObservableCollection<NewsItem>();
            this.InitializeComponent();
            GetNewsItemManager.GetNewItemsByCategory(NewsItems, "Financial");
        } 
    }

    // XAML FILE CODE
    <GridView ItemsSource="{x:Bind NewsItems}" Background="LightGray">
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:NewsItem">
                <local:NewsContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

    //MODELS NEWSITEMS CLASS FILE
    public static void GetNewItemsByCategory(ObservableCollection<NewsItem> NewsItems, string Category)
    {
        var allnewsitems = getNewsItems();
        var filteredNewsItems = allnewsitems.Where(p => p.Category == Category && IsExist(NewsItems, p.Id)).ToList();    
        filteredNewsItems.ForEach(p => NewsItems.Add(p));
    }

    private static Boolean IsExist(ObservableCollection<NewsItem> NewsItems, int Id)
    {
        return NewsItems.ToList().TrueForAll(p => Id == p.Id);
    }

    private static List<NewsItem> getNewsItems()
    {
        var items = new List<NewsItem>();

        items.Add(new NewsItem() { Id = 1, Category = "Financial", Headline = "Lorem Ipsum", Subhead = "doro sit amet", DateLine = "Nunc tristique nec", Image = "Assets/Financial1.png" });
        items.Add(new NewsItem() { Id = 2, Category = "Financial", Headline = "Etiam ac felis viverra", Subhead = "vulputate nisl ac, aliquet nisi", DateLine = "tortor porttitor, eu fermentum ante congue", Image = "Assets/Financial2.png" });
        items.Add(new NewsItem() { Id = 3, Category = "Financial", Headline = "Integer sed turpis erat", Subhead = "Sed quis hendrerit lorem, quis interdum dolor", DateLine = "in viverra metus facilisis sed", Image = "Assets/Financial3.png" });
        items.Add(new NewsItem() { Id = 4, Category = "Financial", Headline = "Proin sem neque", Subhead = "aliquet quis ipsum tincidunt", DateLine = "Integer eleifend", Image = "Assets/Financial4.png" }); items.Add(new NewsItem() { Id = 5, Category = "Financial", Headline = "Mauris bibendum non leo vitae tempor", Subhead = "In nisl tortor, eleifend sed ipsum eget", DateLine = "Curabitur dictum augue vitae elementum ultrices", Image = "Assets/Financial5.png" });

        items.Add(new NewsItem() { Id = 6, Category = "Food", Headline = "Lorem ipsum", Subhead = "dolor sit amet", DateLine = "Nunc tristique nec", Image = "Assets/Food1.png" });
        items.Add(new NewsItem() { Id = 7, Category = "Food", Headline = "Etiam ac felis viverra", Subhead = "vulputate nisl ac, aliquet nisi", DateLine = "tortor porttitor, eu fermentum ante congue", Image = "Assets/Food2.png" });
        items.Add(new NewsItem() { Id = 8, Category = "Food", Headline = "Integer sed turpis erat", Subhead = "Sed quis hendrerit lorem, quis interdum dolor", DateLine = "in viverra metus facilisis sed", Image = "Assets/Food3.png" });
        items.Add(new NewsItem() { Id = 9, Category = "Food", Headline = "Proin sem neque", Subhead = "aliquet quis ipsum tincidunt", DateLine = "Integer eleifend", Image = "Assets/Food4.png" });
        items.Add(new NewsItem() { Id = 10, Category = "Food", Headline = "Mauris bibendum non leo vitae tempor", Subhead = "In nisl tortor, eleifend sed ipsum eget", DateLine = "Curabitur dictum augue vitae elementum ultrices", Image = "Assets/Food5.png" });
        return items;
        }

2 个答案:

答案 0 :(得分:1)

假设

NewsItem.Clear();
filteredNewsItems.ForEach(p => NewsItem.Add(p));

应该是

NewsItems.Clear();
filteredNewsItems.ForEach(p => NewsItems.Add(p));

我认为您所看到的“闪光”(无法确定,因为您没有提供完整的复制品)是由于您正在做的事情来显示更新的列表。

是的,删除所有内容然后再添加一个新的(大部分类似的)列表可以创建一些人称为“闪存”的内容。

更好的方法是删除您不想再显示的项目,然后添加任何额外的项目。
像这样:

        foreach (var newsItem in NewsItems.Reverse())
        {
            if (newsItem.Category != Category)
            {
                NewsItems.Remove(newsItem);
            }
        }
        foreach (var fni in filteredNewsItems)
        {
            if (!NewsItems.Contains(fni))
            {
                NewsItems.Add(fni);
            }
        }

答案 1 :(得分:0)

好的......我想我已经找到了适合你的解决方案。

不要使用lambda表达式更新用户控件中的绑定,而是以这种方式尝试:

public NewsContentControl()
{
    this.InitializeComponent();
    this.DataContextChanged += OnDataCtxChanged; //+= (s, e) => Bindings.Update();
}

private void OnDataCtxChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
    System.Diagnostics.Debug.WriteLine("context 'changed'");
    if(NewsItem != args.NewValue as NewsItem)
    {
        Bindings.Update();
    }
}

仅当值实际不同时才会触发更新。此外,您可以使用&#34; Binding&#34;而不是x:Bind。

这些都没有解释为什么datacontext正在改变......但这应该摆脱你的闪烁。