即使在xamarin表单中设置Bindingcontext后,PropertyChanged事件也为null

时间:2014-11-05 08:36:59

标签: wpf xaml xamarin.ios xamarin xamarin.forms

我正在使用列表视图绑定作为我的xamarin表单的一部分。即使在通过xaml完成数据绑定之后,我也无法看到数据更新到列表视图。通过调试我观察到,PropertyChanged事件为null。将此事件设为null的原因是 - “未正确设置数据上下文”。我也设置了数据上下文,但列表视图中没有填充数据。

这是我的代码。

SampleCode.Xaml

<ContentPage 
    Title="ListViewBinder" 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:SampleNS;assembly=SampleNS" 
    x:Class="SampleNS.SampleCode">
<ContentPage.Content>
        <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" >
            <ListView  x:Name= "listView" ItemsSource="{Binding lsData}" >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <ViewCell.View>
                                            <Label Text="{Binding Name}"></Label>
                                        </ViewCell.View>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
</ContentPage.Content>

ViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace SampleNS
{
    public class ViewModel : INotifyPropertyChanged
    {
        private string _name;

        public string Name
        {
            get {return _name;}
            set {
                    if (value.Equals(_name, StringComparison.Ordinal))
                    {
                        return;
                    }
                    _name = value;
                OnPropertyChanged(_name);
                }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged ([CallerMemberName] string propertyName=null)
        {
            var handler = PropertyChanged;
            if (handler != null) {
                handler (this, new PropertyChangedEventArgs (propertyName));
            }
        }
    }
}

SampleCode.Xaml.cs

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SampleNS
{   
    public partial class SampleCode : ContentPage
    {   
        public static List<ViewModel> lsData; 


        private void init()
        {   

            //this.BindingContext = lsData;
            this.BindingContext = new ViewModel();


            this.LoadFromXaml (typeof(SampleCode));

            lsData = new List<ViewModel> () {
                                new ViewModel { Name = "VM1" },
                                new ViewModel { Name = "VM2" },
                                new ViewModel { Name = "VM3" },
                                new ViewModel { Name = "VM4" },

        }

我假设BindingContext与WPF中的DataContext相同。 (如果我错了,请建议)

我怀疑设置BindingContext。 请建议我,我在做错误的地方。目前我的PropertyChanged事件为null,我在列表视图中看不到任何数据。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您的示例代码.xaml.cs应如下所示:

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SampleNS
{   
    public partial class SampleCode : ContentPage
    {   
        public static List<ViewModel> lsData; 

        public SampleCode ()
        {   
            InitializeComponent ();

            lsData = new List<ViewModel> () {
                            new ViewModel { Name = "VM1" },
                            new ViewModel { Name = "VM2" },
                            new ViewModel { Name = "VM3" },
                            new ViewModel { Name = "VM4" },
            this.BindingContext = lsData;
        }
    }
}