simon
simon

Reputation: 1191

C# store app: Updating a ListView

I'm developing a Windows store App.

I have

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;
    }
}

that describes user and loads all data asynchronously from the server. I also have MainPage.xaml with the following code behind:

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 has a ListView that diplays ObservableCollection<User> Users. (I have DataTemplate that looks like:

        <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>

).

Unfortunately, I see only initial data (logins) in the list. All other data that's received asynchronously in the constructor (name, enabled) is not shown in the listview (I'm sure, it is loaded correctly).

How can I make ListView to show actual state of objects in Users collection? Or should I perform synchronous requests (how?)

Upvotes: 1

Views: 583

Answers (1)

erodewald
erodewald

Reputation: 1825

Your User class must implement INotifyPropertyChanged and raise property change notifications properly.

-HighCore

(This question needed an answer to be appropriately marked as "answered".)

Upvotes: 1

Related Questions