我试图将2个项目绑定到xaml页面。一个是可观察的服务集合,另一个是专家项目的实例。得到错误发生了类型'System.ArgumentException'的第一次机会异常myapp.Client.DLL 当我尝试为专家项目提升INotifyProperty时。
SummaryPage.xaml
//为简洁专家省略了一些字段
<TextBlock Text="Full Name" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Expert.name,Mode=TwoWay}" Grid.Column="1" HorizontalAlignment="Center"/>
<TextBlock Text="Age" Grid.Row="1" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Expert.age}" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"/>
<TextBlock Text="National ID:" Grid.Row="2" HorizontalAlignment="Left"/>
//可观察列表服务
<ListView ItemsSource="{Binding Services}"
IsItemClickEnabled="False"
ScrollViewer.VerticalScrollMode="Enabled"
ScrollViewer.VerticalScrollBarVisibility="Visible"
TabIndex="1" Header="Service Request">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Cost}" Grid.Column="1" HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
SummaryPageViewModel.cs
public class SummaryPageViewModel : ViewModel, ISummaryPageViewModel
{
public ICommand ConfirmCommand { get; private set; }
public ExpertItem Expert { get { return this.GetValue<ExpertItem>(); } set { this.SetValue(value); } }
public ObservableCollection<ServiceItem> Services { get { return this.GetValue<ObservableCollection<ServiceItem>>(); } set { this.SetValue(value); } }
public SummaryPageViewModel()
{
}
public override void Initialize(IViewModelHost host)
{
base.Initialize(host);
// setup...
this.Services = new ObservableCollection<ServiceItem>();
this.Expert = new ExpertItem();
this.ConfirmCommand = new DelegateCommand((args) => GetSelected(args as CommandExecutionContext));
}
public override async void Activated(object args)
{
base.Activated(args);
var conn = Myapp.GetSystemDatabase();
using (this.EnterBusy())
{
IEnumerable<ServiceItem> servs = await conn.Table<ServiceItem>().ToListAsync();
foreach (ServiceItem s in servs)
this.Services.Add(s);
IEnumerable<ExpertItem> exps = await ExpertItem.GetAllFromCacheAsync();
this.Expert = await conn.Table<ExpertItem>().Where(v => v.NativeId == 3).FirstOrDefaultAsync();
}
}
ModelItem.cs
public abstract class ModelItem : INotifyPropertyChanged
{
private Dictionary<string, object> Values { get; set; }
protected ModelItem()
{
this.Values = new Dictionary<string, object>();
}
public event PropertyChangedEventHandler PropertyChanged;
protected T Get
Value<T>([CallerMemberName] string name = null)
{
if (this.Values.ContainsKey(name))
return (T)this.Values[name];
else
return default(T);
}
protected void SetValue(object value, [CallerMemberName] string name = null)
{
// set...
this.Values[name] = value;
// notify...
this.OnPropertyChanged(new PropertyChangedEventArgs(name));
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(name));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);//error raised here e- property name expert.Value does not fall in expected range
}
}
ExpertItem.cs
//code omitted for brevity
public class ExpertItem : ModelItem
{
public int Id { get; set; }
public int NativeId { get { return this.GetValue<int>(); } set { this.SetValue(value); } }
public string name { get { return this.GetValue<string>(); } set { this.SetValue(value); } }
public int age { get { return this.GetValue<int>(); } set { this.SetValue(value); } }
public string email { get { return this.GetValue<string>(); } set { this.SetValue(value); } }
}
栈跟踪
at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
at MFundi.Client.ModelItem.OnPropertyChanged(PropertyChangedEventArgs e)
at MFundi.Client.ModelItem.SetValue(Object value, String name)
at MFundi.Client.SummaryPageViewModel.set_Expert(ExpertItem value)
at MFundi.Client.SummaryPageViewModel.<Activated>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)