【发布时间】:2018-08-05 06:50:46
【问题描述】:
我有一个场景,我需要获取选定 TextCell 的值并使用它来更新 ListView 之外的标签。我注意到 ListView 有一个 SelectedItem 属性,而 TextCell 有一个 Command 属性。这些有什么区别?
作为一个更一般的设计问题(我正在使用 Xamarin MVVM),我应该如何进行更新?目前我正在考虑使用 ListView SelectedItem 属性并将其(双向)与我的 VM 绑定。然后在 Setter 中,我将更新标签绑定到的 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;
【问题讨论】:
标签: c# xaml xamarin mvvm xamarin.forms