作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个包含属性“注释”的 MapView。要在 MapView 上获取注释,您必须使用 AddAnotation 或 AddAnotations。
public class SiteItems
{
public string Title { get; set; }
public string SubTitle { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string Url { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class SiteViewModel : MvxViewModel
{
private IObservableCollection<Models.SiteItems> _siteItems;
public IObservableCollection<Models.SiteItems> SiteItems {
get{ return _siteItems; }
set{ _siteItems = value;
RaisePropertyChanged (() => SiteItems);
}
}
}
最佳答案
订阅更改集合是数据绑定(bind)的基石之一,它依赖于对 INotifyCollectionChanged
的一点了解。界面。
在 MvvmCross 源代码中,有一些示例类展示了如何订阅集合及其更改通知 - 例如MvxViewGroupExtensions.cs在 Droid 和 MvxTableViewSource.cs联系
该技术的核心是创建一个 Adapter
或 Source
监听整个列表或部分列表中的更改并采取相应措施的对象。
相同类型的方法适用于具有多个标记的 map ,尽管我们还没有任何辅助类。
在没有实际使用 Mac 或 iOS 设备的情况下,以下是我创建包装器的大致步骤......
假设我有一个 Model 对象,例如:
public class House
{
public double Lat { get; set; }
public double Lng { get; set; }
public string Name { get; set; }
}
public class FirstViewModel : MvxViewModel
{
public ObservableCollection<House> HouseList { get; set; }
}
public class HouseAnnotation : MKAnnotation
{
public HouseAnnotation(House house)
{
// Todo - the details of actually using the house here.
// in theory you could also data-bind to the house too (e.g. if it's location were to move...)
}
public override CLLocationCoordinate2D Coordinate { get; set; }
}
HouseAnnotationManager
谁负责管理
HouseList
中更改的翻译映射到 map 上显示的注释的变化。
private MKAnnotation CreateAnnotation(House house)
{
return new HouseAnnotation(house);
}
private void AddAnnotationFor(House house)
{
var annotation = CreateAnnotation(house);
_annotations[house] = annotation;
_mapView.AddAnnotation(annotation);
}
private void RemoveAnnotationFor(House house)
{
var annotation = _annotations[house];
_mapView.RemoveAnnotation(annotation);
_annotations.Remove(house);
}
private void AddAnnotations(IList newItems)
{
foreach (House house in newItems)
{
AddAnnotationFor(house);
}
}
private void RemoveAnnotations(IList oldItems)
{
foreach (House house in oldItems)
{
RemoveAnnotationFor(house);
}
}
INotifyCollection
变化:private void OnItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddAnnotations(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
RemoveAnnotations(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
RemoveAnnotations(e.OldItems);
AddAnnotations(e.NewItems);
break;
case NotifyCollectionChangedAction.Move:
// not interested in this
break;
case NotifyCollectionChangedAction.Reset:
ReloadAllAnnotations();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
// MvxSetToNullAfterBinding isn't strictly needed any more
// - but it's nice to have for when binding is torn down
[MvxSetToNullAfterBinding]
public virtual IEnumerable<House> ItemsSource
{
get { return _itemsSource; }
set { SetItemsSource(value); }
}
protected virtual void SetItemsSource(IEnumerable<House> value)
{
if (_itemsSource == value)
return;
if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
}
_itemsSource = value;
if (_itemsSource != null && !(_itemsSource is IList))
MvxBindingTrace.Trace(MvxTraceLevel.Warning,
"Binding to IEnumerable rather than IList - this can be inefficient, especially for large lists");
ReloadAllAnnotations();
var newObservable = _itemsSource as INotifyCollectionChanged;
if (newObservable != null)
{
_subscription = newObservable.WeakSubscribe(OnItemsSourceCollectionChanged);
}
}
_manager
字段,并且可以将其创建和数据绑定(bind)为:
_manager = new HouseAnnotationManager(myMapView);
var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(_manager).To(vm => vm.HouseList);
set.Apply();
关于xamarin - 使用 MvvmCross 如何将注释列表绑定(bind)到 MapView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17816684/
我是一名优秀的程序员,十分优秀!