【问题标题】:PropertyChanged event is null even after setting the Bindingcontext in xamarin forms即使在 xamarin 表单中设置了 Bindingcontext,PropertyChanged 事件也为空
【发布时间】:2015-01-01 09:09:01
【问题描述】:

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

这是我的代码。

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 事件为空,我在列表视图中看不到任何数据。

提前致谢。

【问题讨论】:

  • 我会尝试在 LoadFromXaml 之后设置 BindingContext。
  • 嗨米哈,感谢您的回复。我尝试向下移动绑定上下文。我仍然在 ViewModel 类中看到 PropertyChanged 事件为空,并且我的列表视图未更新。 :-(

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


【解决方案1】:

您的示例 code.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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-08
    • 2020-06-22
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2011-10-20
    • 1970-01-01
    相关资源
    最近更新 更多