有没有比在XAML中使用ObservableCollection进行快速绑定更好的方法?

时间:2013-12-04 08:22:24

标签: xaml mvvm binding windows-phone observablecollection

我们正在开发Windows Phone应用程序(CurrencyExchange),并且有一个页面包含文本框和列表框。 ListBox的itemsource是viewmodel的可观察集合的属性类型。 Textbox的textchange事件正在更改可观察集合的所有项目,但是当可观察的更改并尝试绑定列表框的项目时,页面将被锁定。我见过一个名为Fastobservablecollection的customobservable集合,它没有在viewmodel中运行,因为它使用了在viewmodel中无法使用的DispatcherObject和Dispatcherprioty。有没有比ObservableCollection更好的选择?

List<Currency> newList = new List<Currency>(CurrencyConversions.ToList());
foreach (var item in newList)
{
    Double result;
    if (Double.TryParse(AmountPhone, NumberStyles.Any, new System.Globalization.CultureInfo("tr-TR"), out result))
        item.CalculatedValue = Math.Round(result * (Direction == "0" ? item.ConversionRateSell : item.ConversionRateBuy), 2);
    else
        item.CalculatedValue = 0;
}
CurrencyConversions = new ObservableCollection<Currency>(newList);

 List<Currency> newList = new List<Currency>();
 foreach (var item in CurrencyConversions)
 {
     Double result;
     if (Double.TryParse(AmountPhone, NumberStyles.Any, new System.Globalization.CultureInfo("tr-TR"), out result))
         item.CalculatedValue = Math.Round(result * (Direction == "0" ? item.ConversionRateSell : item.ConversionRateBuy), 2);
     else
         item.CalculatedValue = 0;
     newList.Add(item);
 }
 CurrencyConversions = new ObservableCollection<Currency>(newList);

感谢。

使用SmartCollection

List<Currency> newList = new List<Currency>(CurrencyConversions.ToList());
foreach (var item in newList)
{
    Double result;
    if (Double.TryParse(Amount, NumberStyles.Any, new System.Globalization.CultureInfo("tr-TR"), out result))
        item.CalculatedValue =Math.Round( result * (Direction == "0" ? item.ConversionRateSell : item.ConversionRateBuy),2);
    else
        item.CalculatedValue = 0;
 }
 CurrencyConversions = new SmartCollection<Currency>(newList);

1 个答案:

答案 0 :(得分:2)

您不应每次都创建一个新的SmartCollection,但更多只使用一个实例并对集合进行修改。我建议将属性CurrencyConversions的实现更改为以下内容。

private SmartCollection<Currency> _conversions;
public SmartCollection<Currency> CurrencyConversions{
  get{
    if (_conversions == null) {
      _conversions = new SmartCollection<Currency>();
    }

    return _conversions;
  }
}

然后你可以在你的代码中这样调用它:

CurrencyConversions.Reset(newList);

由于我目前了解您的代码,因此您不会更改(添加和删除项目)集合,而是更改CalculatedValue类型的属性Currency。当类型Currency实现接口INotifyPropertyChanged时,无需更改集合以更新UI,因此将代码更改为

foreach (var item in CurrencyConversions) {
  Double result;
  if (Double.TryParse(Amount, NumberStyles.Any, new System.Globalization.CultureInfo("tr-TR"), out result))
    item.CalculatedValue =Math.Round( result * (Direction == "0" ? item.ConversionRateSell : item.ConversionRateBuy),2);
  else
    item.CalculatedValue = 0;
  }
当你的类型实现INotifyPropertyChanged时,

就足够了。要使其工作,属性CalculatedValue应如下所示:

private double _value;
public double CalculatedValue{
  get{
    return _value;
  }
  set{
    _value = value;
    if (PropertyChanged != null) {
      PropertyChanged(this, new PropertyChangedEventArgs("CalculatedValue"));
    }
  }
 }