我正在开发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));
}
}
}
MainPage
有ListView
偶像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>
)。
不幸的是,我只看到列表中的初始数据(登录)。在构造函数(name
,enabled
)中异步接收的所有其他数据未显示在listview
中(我确定,它已正确加载)。
如何让ListView
显示Users
集合中对象的实际状态?或者我应该执行同步请求(如何?)
答案 0 :(得分:1)