代码之家  ›  专栏  ›  技术社区  ›  loop

异步方法中的可观察集合

  •  2
  • loop  · 技术社区  · 11 年前

    你好,朋友们,我在使用时遇到了一个非常奇怪的问题 可观测集合 异步 方法。我正试图以异步方法在ObservableCollection中添加项,如果我在wait关键字所在的行上方定义ObservableCollections,但在该行下方初始化,它就不起作用了。我已经为这个问题做了样本。 我的xaml代码是。。

    <Page
    x:Class="observableCollectionTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:observableCollectionTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding SomeCollection}" Background="Pink" HorizontalAlignment="Left" Width="500" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="200" Height="200" Background="Red" >
                        <Button Content="click me" Name="btn" ></Button>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>            
        </ListView>
    </Grid>
    

    我的主页反手工作代码是。。

    public sealed partial class MainPage : Page
    {
        public ObservableCollection<string> SomeCollection { get; set; }
    
        public MainPage()
        {
            this.InitializeComponent();
    
    
            FillCollection();
            this.DataContext = this;
        }
    
        public async Task FillCollection()
        {
            SomeCollection = new ObservableCollection<string>(); // it is working..
            HttpClient client = new HttpClient();
            HttpResponseMessage message = await client.GetAsync("https://www.google.co.in/");
    
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
    
        }
    

    我的主页反手FillCollection代码不起作用。。

     public async Task FillCollection()
        {
    
            HttpClient client = new HttpClient();
            HttpResponseMessage message = await client.GetAsync("https://www.google.co.in/");
            SomeCollection = new ObservableCollection<string>(); // this is not working
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
            SomeCollection.Add("asd");
    
        }
    

    我不明白为什么会发生这种事。我遗漏了一些概念,请告诉我。任何帮助或建议都将不胜感激。。

    1 回复  |  直到 11 年前
        1
  •  2
  •   Rafal    11 年前

    您缺少从您的 SomeCollection 属性-请参阅 this 关于如何实现这一点。

    之所以需要它,是因为 FillCollection 方法,然后在分配之前执行async DataContext 属性,另一半在web请求完成后。所以在你不工作的例子中 View 不知道整个集合都被更改了。

    另一方面,考虑分配的新实例会失去什么 ObservableCollection 在每次刷新列表时(您想在某个时间点刷新它吗?)。这个类的基础是提供UI方式来优化刷新中的项 ItemsControl 如果你把一个新的集合装在板条箱里,就知道哪些元素被添加/删除/移动了 ListView 将重新招标,这可能很昂贵。我建议你先把作业交给建造师 填充集合 出于代码清晰的原因进行调用,并很好地处理内容更改——这对您来说是更多的工作,但如果视图很复杂,那么这是值得的。