我是Prism MVVM的新手并尝试创建ViewModel属性,这些属性具有Model属性的支持字段。
public class SomeViewModel : BindableBase, INavigationAware
{
private Person model;
public Person Model
{
get { return model; }
set { SetProperty(ref model, value); }
}
public string DisplayName
{
get { return Model.DisplayName; }
set { SetProperty(ref Model.DisplayName, value); }
}
但它会在行生成编译器错误:
set { SetProperty(ref Model.DisplayName, value); }
“属性或索引器不能作为out或ref参数传递”。
那我该怎么做呢?
答案 0 :(得分:3)
您需要手动提升OnPropertyChanged并在没有帮助程序的情况下进行设置。
<?php
foreach ($products as $product) {
echo $product['price'].'<br>';
echo $product['quantity'].'<br>';
$subtotal = $product['price']*$product['quantity'];
echo $subtotal.'<br>';
echo $total; //??????
}
?>
答案 1 :(得分:1)
您可以使用Reflection
创建类似BindableBaseExtended
的内容,这些内容来自Prism.Mvvm.BindableBase
。
public class SomeViewModel : BindableBaseExtended
{
private Person model;
public Person Model
{
get { return model; }
set { SetProperty(ref model, value); }
}
public string MyProperty
{
get { return Model.DisplayName; }
set { SetProperty(Model, m => m.DisplayName, value); }
}
}
public class BindableBaseExtended : BindableBase
{
protected virtual bool SetProperty<TClass, TMember>(TClass target, Expression<Func<TClass, TMember>> expression, TMember value, [CallerMemberName] string propertyName = null)
{
var expr = (MemberExpression)expression.Body;
var prop = (PropertyInfo)expr.Member;
var propValue = prop.GetValue(target, null);
if (object.Equals(propValue, value)) return false;
prop.SetValue(target, value, null);
this.RaisePropertyChanged(propertyName);
return true;
}
protected virtual bool SetProperty<TClass, TMember>(TClass target, Expression<Func<TClass, TMember>> expression, TMember value, Action onChanged, [CallerMemberName] string propertyName = null)
{
var expr = (MemberExpression)expression.Body;
var prop = (PropertyInfo)expr.Member;
var propValue = prop.GetValue(target, null);
if (object.Equals(propValue, value)) return false;
prop.SetValue(target, value, null);
onChanged?.Invoke();
this.RaisePropertyChanged(propertyName);
return true;
}
}