Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Binding Update become blank on UWP

$
0
0

Hello all,

I am a beginner and just starting to use Xamarin.Forms for cross platform.

At first, the thing work fine in Android, but not working in Universal Windows Platform.

I have create the Xamarin.Forms follow UWP instruction at https://developer.xamarin.com/guides/xamarin-forms/windows/getting-started/universal

The problem:
- When I change the data in binding object, the label become empty.

FirstPage.xaml

<StackLayout Orientation="Vertical">
<ListView x:Name="LvUser">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <Grid Padding="5">
                  <Label Text="{Binding UserName}" />
                </Grid>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
      </ListView>
      <Button Text="change username" Clicked="changeUserNameClicked" />
</StackLayout>

User.cs

namespace XamarinPCL
{
    public class User
    {
        public string username { get; set; }
    }
}

UserViewModel.cs

namespace XamarinPCL
{
    public class UserViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private User _user;
        public String UserName
        {
            get
            {
                return _user.username;
            }
            set
            {
                _user.username = value;
                NotifyPropertyChanged("UserName");
            }
        }
        public UserViewModel(User user)
        {
            _user = user;
        }
    }
}

FirstPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Collections.ObjectModel;
namespace XamarinPCL
{
    public partial class FirstPage : ContentPage
    {
        ObservableCollection<UserViewModel> _userVM = new ObservableCollection<UserViewModel>();
        UserViewModel _uvm1 = new UserViewModel(new User() { username = "user 1" });
        UserViewModel _uvm2 = new UserViewModel(new User() { username = "user 2" });
        public FirstPage()
        {
            InitializeComponent();
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            _userVM.Add(_uvm1);
            _userVM.Add(_uvm2);
            LvUser.ItemsSource = _userVM;
        }
        private void changeUserNameClicked(object sender, EventArgs e)
        {
            _uvm1.UserName = "i changed";
        }
    }
}

When I click on the item, the row #1 become empty, any idea?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>