如何解决如何捕获仅发送空对象的Xamarin CollectionView选择?
我一直在使用Internet上的每个示例来使我的CollectionView将对象发送到我的viewmodel,但是没有运气!顺便说一下,这正在作为Android项目进行测试。
需要注意的一件奇怪的事情:不是选定的单元格变成橙色(如MS样品溶液), 我的CollectionView项目只是闪烁灰色,非常快。我没有应用任何格式,所以我不知道为什么这与默认格式没有什么不同。
我试图做到详尽而简短,以了解什么不起作用。我用“我的”一词来代表我的自定义类,我相信您必须将CollectionView的输出转换为该类。
我的Xaml ...
<CollectionView
x:Name="MyCollectionView"
SelectionMode="Multiple" //Why don't my cells remain highlighted?
ItemsSource="{Binding MyItemSource}"
SelectedItem="{Binding SelectedItem,Mode=TwoWay}" //This doesn't seem to do anything
SelectedItems="{Binding SelectedItems,Mode=TwoWay}" //This doesn't seem to do anything
SelectionChangedCommand="{Binding SelectionChangedCommand}"> //This gets called on tapping
<CollectionView.ItemTemplate> <!--Contains a Grid with labels and an icon-->
我的视图模型...
private ObservableCollection<MyClass> _myItemSource;
public ObservableCollection<MyClass> MyItemSource {get... set... data from database}
//Isn't this the selected items? I think I have to cast this list's objects to my class.
public IList<object> ItemsSelected { get; set; }
//To cast objects returned from CollectionView into my class:
public ObservableCollection<MyClass> MyItemsSelected { get; set; }
//Shouldn't SelectedItem be the most recently tapped item?
private MyClass _selectedItem;
public MyClass SelectedItem
{
get
{
return _selectedItem;
//Should this be : return (MyClass)_selectedItem?
}
set
{
_selectedItem = value;
OnPropertyChanged();
}
}
//No problem calling this,but it's arg is always null:
public ICommand SelectionChangedCommand { get; set; }
//My Constructor...
ItemsSelected = new List<object>();
MyItemsSelected = new ObservableCollection<MyClass>();
SelectionChangedCommand = new Command<object>(OnSelectionChangedCommand); //object is always null!
//My One Method... gets called dependably,but obj is always null!
private void OnSelectionChangedCommand(object obj)
{
//if (obj != null)
{
var x = (MyClass)obj;
if (MyItemsSelected .Contains(x))
{
MyItemsSelected .Remove(x);
}
else
{
MyItemsSelected .Add(x);
}
}
Debug.WriteLine("~~~ OnSelectionChangedCommand Called");
}
我认为我遵循的是一个非常基本的MVVM模式,但是任何正常运行的示例似乎都不适合我的项目...我只想抓住所选的项目...我宁愿将多个选择保持突出显示直到重新点击(例如MS Docs示例),但我并不挑剔。我真的很想在这里了解发生了什么。谢谢!
解决方法
一件奇怪的事情要注意:我的CollectionView项只是闪烁灰色,而且很快,而不是选定的单元变为橙色(如MS样品溶液)。
您在哪个平台上进行了测试?所选项目的颜色在Android上为橙色,在iOS上为灰色。
我测试了有关该功能的基本演示,并且工作正常。这是示例link,您可以参考它。
public class CustomViewModel : INotifyPropertyChanged
{
readonly IList<CustomModel> source;
public ObservableCollection<CustomModel> DataCollection { get; private set; }
ObservableCollection<object> theSelectedItems;
public ObservableCollection<object> TheSelectedItems
{
get
{
return theSelectedItems;
}
set
{
if (theSelectedItems != value)
{
theSelectedItems = value;
}
}
}
public CustomViewModel()
{
source = new List<CustomModel>();
//add the items
DataCollection = new ObservableCollection<CustomModel>(source);
TheSelectedItems = new ObservableCollection<object>() { DataCollection[1],DataCollection[3],DataCollection[4] };
}
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
更新:
我用Frame
测试了代码,并且遇到相同的问题,所选项目将不会突出显示。这可能是潜在问题,您可以在github上将其报告给产品团队。
有解决此问题的方法,请尝试使用VisualStateManager添加所选效果。
<DataTemplate>
<Frame>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="DarkOrange" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="White" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackLayout>
...
</StackLayout>
</Frame>
</DataTemplate>
,
问题实际上在Xaml中!似乎导致CollectionView的选择出现问题。我报废了DataTemplate,后端工作了。经过...大量的时间调整了错误的部分之后,我可以说我从选区中学到了很多!
贾森(Jason)提供的此链接可能是我阅读的关于实际实现选择的最详尽的参考,以及您可能会遇到的错误:
https://github.com/xamarin/Xamarin.Forms/issues/6891#issuecomment-511936153
也更新以防万一您遇到此情况: “框架没有处于选定状态” 更多讨论:https://forums.xamarin.com/discussion/174791/using-the-visualstatemanager-in-a-collectionviews-itemtemplate
感谢您的帮助和鼓励。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。