c# - 与 ListView SelectedItem 与 TextCell 命令绑定(bind)

标签 c# xaml xamarin mvvm xamarin.forms

我有一个场景,我需要获取所选 TextCell 的值并使用它来更新 ListView 之外的标签。我注意到 ListView 有一个 SelectedItem 属性,而 TextCell 有一个 Command 属性。这些有什么区别?

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

我见过提到行为,但这似乎更多地用于 UI(而不是逻辑)。我也尝试过使用 Task.Run 来解决 setter 中的异步问题,但很明显异步项不适合 setter。我还考虑过使用 MessagingCenter,但这似乎是针对 VM -> VM 的。 TextCell 命令似乎合适,但已阅读改为使用 SelectedItems。

看法:

<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}"/>

对于虚拟机:
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;

最佳答案

  • 你将你的标签绑定(bind)到你的虚拟机的一个属性上,比如说文本
  • 您将 ListView 的 SelectedItem 绑定(bind)到 VM 的属性,我们称之为 SelectedItem
  • 在 SelectedItem 的 setter 中,您调用 Task.Run(() => DoWork(value)) 来完成您的工作或获取您的翻译
  • 在工作结束时,您设置属性文本
  • Text 的 Property Setter 应该调用 PropertyChanged
  • 就是这样

  • 示例虚拟机:
    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);
            }
        }
    }
    

    关于c# - 与 ListView SelectedItem 与 TextCell 命令绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977929/

    相关文章:

    c# - 当用户在c#中单击它时获取图像的来源

    c# - WPF/XAML - 绑定(bind)到 TextBox PreviewTextInput 事件

    webview - Xamarin 表单 WebView 检查网站地址更改时

    c# - Xamarin.Forms 选择器默认值

    c# - 如何将 JSON 文件发布到 ASP.NET MVC 操作?

    c# - WPF 中的源绑定(bind),如 Asp.Net

    c# - Metro 应用程序的最简单的 DIY 日期选择器和时间选择器(c# 和 XAML)

    https - 如何忽略错误 : System. Security.Cryptography.CryptographicException:存储根不存在

    c# - 如何在谷歌地图中使用 jquery 动态绑定(bind)多个标记

    c# - 为什么 C# 在实现接口(interface)时不允许继承返回类型