WPF MVVM - 如何调用外部服务 (WCF) 来更新模型 OnPropertyChanged?
WPF MVVM - How to call external service (WCF) to update model OnPropertyChanged?
我的 WPF 应用程序中有模型和视图模型,如下所示。
我的目的是在 GivenPrice
更改时用 WCF 中的一些字符串更新 HumanizedPrice
属性。 HumanizedPrice
基于GivenPrice
,需要WCF才能获取。
为此,最好的 MVVM 兼容方法是什么?
型号:
class Price : INotifyPropertyChanged
{
public string GivenPrice
{
get
{
return _givenPrice;
}
set
{
if (_givenPrice != value)
{
_givenPrice = value;
RaisePropertyChanged("GivenPrice");
RaisePropertyChanged("HumanizedPrice");
}
}
}
public string HumanizedPrice
{
get
{
return _humanizedPrice;
}
set
{
if (_humanizedPrice != value)
{
_humanizedPrice = value;
RaisePropertyChanged("HumanizedPrice");
}
}
}
private string _givenPrice;
private string _humanizedPrice;
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
视图模型:
class PriceViewModel
{
public PriceViewModel()
{
LoadPrices();
}
public ObservableCollection<Price> Prices
{
get;
set;
}
public void LoadPrices()
{
ObservableCollection<Price> prices = new ObservableCollection<Price>();
prices.Add(new Price { GivenPrice = "221 331,44" });
prices.Add(new Price { GivenPrice = "2 331,44" });
prices.Add(new Price { GivenPrice = "331,44" });
prices.Add(new Price { GivenPrice = "0,44" });
Prices = prices;
}
}
以下是我的建议:
public void LoadPrices()
{
// Prices loading...
Prices = prices;
foreach (var price in Prices)
{
price.PropertyChanged += (s, e) =>
{
if (e.PropertyName.Equals(nameof(Price.GivenPrice)))
{
// do your WCF call here...
}
};
}
}
注意!如果您要添加/删除价格,您还应该订阅 ObservableCollection 的 CollectionChanged 并手动处理订阅:
Prices.CollectionChanged += (s, e) =>
{
// Subscribing on property changed event for newly added prices
if (e.Action == NotifyCollectionChangedAction.Add)
foreach (Price item in e.NewItems)
item.PropertyChanged += WCFCallHandler;
// Unsubscribing from property changed event of removed prices
if (e.Action == NotifyCollectionChangedAction.Remove)
foreach (Price item in e.OldItems)
item.PropertyChanged -= WCFCallHandler;
};
我的 WPF 应用程序中有模型和视图模型,如下所示。
我的目的是在 GivenPrice
更改时用 WCF 中的一些字符串更新 HumanizedPrice
属性。 HumanizedPrice
基于GivenPrice
,需要WCF才能获取。
为此,最好的 MVVM 兼容方法是什么?
型号:
class Price : INotifyPropertyChanged
{
public string GivenPrice
{
get
{
return _givenPrice;
}
set
{
if (_givenPrice != value)
{
_givenPrice = value;
RaisePropertyChanged("GivenPrice");
RaisePropertyChanged("HumanizedPrice");
}
}
}
public string HumanizedPrice
{
get
{
return _humanizedPrice;
}
set
{
if (_humanizedPrice != value)
{
_humanizedPrice = value;
RaisePropertyChanged("HumanizedPrice");
}
}
}
private string _givenPrice;
private string _humanizedPrice;
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
视图模型:
class PriceViewModel
{
public PriceViewModel()
{
LoadPrices();
}
public ObservableCollection<Price> Prices
{
get;
set;
}
public void LoadPrices()
{
ObservableCollection<Price> prices = new ObservableCollection<Price>();
prices.Add(new Price { GivenPrice = "221 331,44" });
prices.Add(new Price { GivenPrice = "2 331,44" });
prices.Add(new Price { GivenPrice = "331,44" });
prices.Add(new Price { GivenPrice = "0,44" });
Prices = prices;
}
}
以下是我的建议:
public void LoadPrices()
{
// Prices loading...
Prices = prices;
foreach (var price in Prices)
{
price.PropertyChanged += (s, e) =>
{
if (e.PropertyName.Equals(nameof(Price.GivenPrice)))
{
// do your WCF call here...
}
};
}
}
注意!如果您要添加/删除价格,您还应该订阅 ObservableCollection 的 CollectionChanged 并手动处理订阅:
Prices.CollectionChanged += (s, e) =>
{
// Subscribing on property changed event for newly added prices
if (e.Action == NotifyCollectionChangedAction.Add)
foreach (Price item in e.NewItems)
item.PropertyChanged += WCFCallHandler;
// Unsubscribing from property changed event of removed prices
if (e.Action == NotifyCollectionChangedAction.Remove)
foreach (Price item in e.OldItems)
item.PropertyChanged -= WCFCallHandler;
};