基于MVVM的Windows Phone 8应用程序中的绑定问题
这是我的第一个windows8应用程序,它非常基于VisualStudio提供的默认模板。问题是当我试图通过在LoadData()中创建新的Observable collection实例来分配Items属性值时,数据没有绑定,但当我使用Items.add方法将项目添加到列表中时,我可以在UI中看到数据。我希望有人能解释我的行为,如果我遗漏了任何非常明显的东西基于MVVM的Windows Phone 8应用程序中的绑定问题,mvvm,windows-phone-8,Mvvm,Windows Phone 8,这是我的第一个windows8应用程序,它非常基于VisualStudio提供的默认模板。问题是当我试图通过在LoadData()中创建新的Observable collection实例来分配Items属性值时,数据没有绑定,但当我使用Items.add方法将项目添加到列表中时,我可以在UI中看到数据。我希望有人能解释我的行为,如果我遗漏了任何非常明显的东西 namespace Sample.ViewModels { public class MainViewModel : INotif
namespace Sample.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
}
/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>
public ObservableCollection<ItemViewModel> Items { get; private set; }
private string _sampleProperty = "Sample Runtime Property Value";
/// <summary>
/// Sample ViewModel property; this property is used in the view
/// to display its value using a Binding
/// </summary>
/// <returns></returns>
public string SampleProperty
{
get
{
return _sampleProperty;
}
set
{
if (value != _sampleProperty)
{
_sampleProperty = value;
NotifyPropertyChanged("SampleProperty");
}
}
}
/// <summary>
/// Sample property that returns a localized string
/// </summary>
public string LocalizedSampleProperty
{
get
{
return AppResources.SampleProperty;
}
}
public bool IsDataLoaded
{
get;
private set;
}
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
public void LoadData()
{
try
{
using (IQContext context = new IQContext("isostore:/Test.sdf"))
{
var query = (from c in context.Categories
select new ItemViewModel
{
CategoryId = c.CategoryId,
CategoryName = c.CategoryName
});
// Items present in the list.
this.Items =
new ObservableCollection<ItemViewModel>(query);
// this.Items.Add(new ItemViewModel()
// { CategoryId = 1, CategoryName = "Rishi"}); // This Works
this.IsDataLoaded = true;
}
}
catch (Exception ex)
{
throw ex;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
namespace Sample.ViewModels
{
公共类MainViewModel:INotifyPropertyChanged
{
公共主视图模型()
{
this.Items=新的ObservableCollection();
}
///
///ItemViewModel对象的集合。
///
公共可观测集合项{get;private set;}
私有字符串_sampleProperty=“示例运行时属性值”;
///
///示例ViewModel属性;此属性在视图中使用
///使用绑定显示其值的步骤
///
///
公共字符串SampleProperty
{
得到
{
返回sampleProperty;
}
设置
{
如果(值!=\u sampleProperty)
{
_样本属性=值;
NotifyPropertyChanged(“SampleProperty”);
}
}
}
///
///返回本地化字符串的示例属性
///
公共字符串本地化采样属性
{
得到
{
返回AppResources.SampleProperty;
}
}
公共布尔值已加载
{
得到;
私人设置;
}
///
///创建一些ItemViewModel对象并将其添加到Items集合中。
///
公共void LoadData()
{
尝试
{
使用(IQContext上下文=新的IQContext(“isostore:/Test.sdf”))
{
var query=(来自context.Categories中的c)
选择新项目视图模型
{
CategoryId=c.CategoryId,
CategoryName=c.CategoryName
});
//列表中的项目。
此项。项目=
新的ObservableCollection(查询);
//this.Items.Add(新的ItemViewModel()
//{CategoryId=1,CategoryName=“Rishi”});//这很有效
this.IsDataLoaded=true;
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
您的项
属性应该实现INPC,因为您正在更改加载数据中的引用,并且需要通知UI:
public ObservableCollection<ItemViewModel> Items { get; private set; }
publicobservableCollection项{get;private set;}
到
私有可观察收集项目;
公共可观测收集项目
{
得到
{
归还此物品;
}
专用设备
{
这个项目=价值;
通知财产变更(“项目”);
}
}
澄清一下:ObservableCollection有一个事件CollectionChanged,一些标准视图(ListBox、LongListSelector等)订阅该事件,以便进行增量更新。当您更改引用时,订阅的侦听器无法知道这一点,因此您必须NotifyPropertyChanged(之后视图将删除其旧集合Changed侦听器,并为新集合添加一个)
private ObservableCollection<ItemViewModel> items;
public ObservableCollection<ItemViewModel> Items
{
get
{
return this.items;
}
private set
{
this.items = value;
NotifyPropertyChanged("Items");
}
}