C#store app:更新ListView

时间:2013-10-15 19:45:56

标签: c# listview microsoft-metro windows-store-apps async-await

我正在开发Windows商店App。

我有

class User
{
    public string Login { get; set; }
    public string Name { get; set; }
    public string Enabled { get; set; }

    public User(string Login)
    {
        this.Login = Login;
        this.Load();
    }

    async public Task Load()
    {
        HttpResponseMessage response = await App.http.GetAsync("https://example.com/user?login=" + Login);
        var xml = XDocument.Parse(await response.Content.ReadAsStringAsync());
        var user = (from entry in xml.Descendants("user")
                        select new
                        {
                            Name       = (string)entry.Element("name"),
                            Enabled    = (string)entry.Element("enabled")
                        }).First();
        this.Name = user.Name;
        this.Enabled = user.Enabled;
    }
}

描述用户并从服务器异步加载所有数据。我还有MainPage.xaml,后面有以下代码:

public sealed partial class MainPage : Mail.Common.LayoutAwarePage
{        
    ObservableCollection<User> Users = new ObservableCollection<User>();

    public MainPage()
    {
        this.InitializeComponent();            
        itemListView.ItemsSource = Users;
    }

    private async void pageRoot_Loaded(object sender, RoutedEventArgs e)
    {
        HttpResponseMessage response = await App.http.GetAsync("https://example.com/listusers");
        var xml = XDocument.Parse(await response.Content.ReadAsStringAsync());
        foreach (var item in from entry in xml.Descendants("email")
                             select new
                             {
                                 Login = (string)entry.Element("name")
                             })
        {
            Users.Add(new User(item.Login));
        }
    }
}

MainPageListView偶像ObservableCollection<User> Users。 (我有DataTemplate看起来像:

        <DataTemplate x:Key="UserTemplate">
        <StackPanel Orientation="Horizontal" Width="470" Height="85">
            <Border Height="40" Width="40" Margin="10,10,0,10" VerticalAlignment="Top">
                <Image Source="/SampleImage.png" Stretch="UniformToFill"/>
            </Border>
            <StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="0,10,0,0">
                <TextBlock Text="{Binding Login}" FontSize="20" FontWeight="Semilight" 
                   Margin="10,0,0,0" Width="320" Height="26" TextTrimming="WordEllipsis" 
                   HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <TextBlock Text="{Binding Name}" 
                   Margin="10,2,0,0" Width="320" TextTrimming="WordEllipsis" TextWrapping="Wrap" 
                   HorizontalAlignment="Left"/>
            </StackPanel>
            <TextBlock Text="{Binding Enabled}" FontSize="16" Margin="20,0,0,0" VerticalAlignment="Center"/>
        </StackPanel>
    </DataTemplate>

)。

不幸的是,我只看到列表中的初始数据(登录)。在构造函数(nameenabled)中异步接收的所有其他数据未显示在listview中(我确定,它已正确加载)。

如何让ListView显示Users集合中对象的实际状态?或者我应该执行同步请求(如何?)

1 个答案:

答案 0 :(得分:1)

  

您的User类必须实现INotifyPropertyChanged并正确引发属性更改通知。

- HighCore

(这个问题需要一个适当标记为“已回答”的答案。)