使用ListView selectedItem vs textCell命令绑定



我有一个场景,我需要使用所选文本电池的值并使用它来更新ListView之外的标签。我注意到ListView具有SelectedItem属性,并且TextCell具有命令属性。这些有什么区别?

作为一个更一般的设计问题(我正在使用Xamarin MVVM(,我应该如何进行更新?目前,我正在考虑使用ListView SelectedItem属性并将其与我的VM绑定(双向(。然后,在设置器中,我将更新VM Propery,即标签被限制到....问题是我有一个异步任务,我需要执行的任务,因为它将将TextCell值转换为我需要的标签值。...我应该怎么做?

我已经看到了行为的提及,但这对于UI(而非逻辑(似乎更重要。我还玩了使用任务的想法。lun以解决六个方面的异步问题,但很明显,异步项目并不是针对二传剂的。我还考虑过使用消息中心,但这似乎是用于VM-> VM的。TextCell命令似乎合适,但已阅读以使用SeptionItems。

查看:

<ListView x:Name="ResultsList"
                          SelectedItem="{Binding SelectedDestinationItem,
                                                 Mode=TwoWay}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextCell Text="{Binding Description}" Detail="{Binding Place_ID}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
<Label Text="{Binding Current.Destination}"/>

和VM:

public AutoCompletePrediction SelectedDestinationItem
    {
        get => _selectedDestinationItem;
        set
        {
            SetProperty(ref _selectedDestinationItem, value, "SelectedDestinationItem");
            if (_selectedDestinationItem == null) return;
            //not valid
            var place = await Places.GetPlace(_selectedDestinationItem.Place_ID, Constants.PlacesApiKey); 
            Current.Destination = place;
            SelectedDestinationItem = null;

        }
    }
    private AutoCompletePrediction _selectedDestinationItem;
  • 您将标签绑定到您的属性vm的属性
  • 您将listView的selectedItem绑定到VM的属性,我们将其称为SelectedItem
  • 在SelectionItem的设定器中,您调用一个任务。run(((=> dowork(value((来完成您的工作或进行翻译
  • 在工作结束时,您设置了属性文本
  • 文本的属性设置器应调用属性销售
  • 就是这样

样本VM:

public class MyViewModel : ViewModelBase {
    private string _text;
    public string Text {
        get => _text;
        set {
            _text = value;
            OnPropertyChanged();
        }
    }
    private YourItemType _selectedItem;
    public YourItemType SelectedItem {
        get => _selectedItem;
        set {
            _selectedItem = value;
            Task.Run(() => GetValue(value));
            OnPropertyChanged();
        }
    }
    private readonly object _lockObject = new object();
    private void GetValue(YourItemType item){
        if(item == null) {
           Text = "invalid";
           return;
        }  
        //avoid entering twice here
        lock(_lockObject) {
            //Do your work here - it's in the background already
            Text = resultOfWork;
        }
    }
}

onProperTychanged((是InotifyPropertychanged的实现。类似:

public class ViewModelBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    protected virtual void OnPropertyChangedByName(string propertyName) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public void RefreshView() {
        foreach (var propertyInfo in GetType().GetRuntimeProperties()) {
            OnPropertyChangedByName(propertyInfo.Name);
        }
    }
}

最新更新