捕获仅发送空对象的 Xamarin CollectionView Selections 是怎么回事?
What is up with capturing Xamarin CollectionView Selections only sending null objects?
我一直在使用互联网上的每个示例让我的 CollectionView 将对象发送到我的 ViewModel,但没有成功!顺便说一句,这正在作为一个 Android 项目进行测试。
需要注意一件奇怪的事情:不是选定的单元格变成橙色(就像 MS 示例解决方案),而是
我的 CollectionView 项目只是闪烁灰色,非常快。我没有应用任何格式,所以我不知道为什么这与默认设置有任何不同。
我试图做到全面而简短,以了解什么不起作用。我使用术语“我的”来表示我的自定义 class,我相信您必须将 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 示例),但我并不挑剔。我真的很想了解这里发生了什么。谢谢!
One weird thing to note: instead of the selected cells turning orange (like the MS sample solutions), my CollectionView items just flash grey, real quick.
你是在哪个平台上测试的?所选项目的颜色在 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!看来 < Frame > 导致了 CollectionView 的选择问题。我报废了我的 DataTemplate,后端工作了。在...花了很多时间调整错误的部分之后,我可以说我学到了很多关于选择的知识!
这个 link,由 Jason 提供,可能是我读过的关于实际实现选择的最详尽的参考资料,以及您可能遇到的错误:
https://github.com/xamarin/Xamarin.Forms/issues/6891#issuecomment-511936153
更新以防你也碰到这个:
“框架没有选定状态”
更多讨论:https://forums.xamarin.com/discussion/174791/using-the-visualstatemanager-in-a-collectionviews-itemtemplate
感谢您的帮助和鼓励。
我一直在使用互联网上的每个示例让我的 CollectionView 将对象发送到我的 ViewModel,但没有成功!顺便说一句,这正在作为一个 Android 项目进行测试。
需要注意一件奇怪的事情:不是选定的单元格变成橙色(就像 MS 示例解决方案),而是 我的 CollectionView 项目只是闪烁灰色,非常快。我没有应用任何格式,所以我不知道为什么这与默认设置有任何不同。
我试图做到全面而简短,以了解什么不起作用。我使用术语“我的”来表示我的自定义 class,我相信您必须将 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 示例),但我并不挑剔。我真的很想了解这里发生了什么。谢谢!
One weird thing to note: instead of the selected cells turning orange (like the MS sample solutions), my CollectionView items just flash grey, real quick.
你是在哪个平台上测试的?所选项目的颜色在 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!看来 < Frame > 导致了 CollectionView 的选择问题。我报废了我的 DataTemplate,后端工作了。在...花了很多时间调整错误的部分之后,我可以说我学到了很多关于选择的知识!
这个 link,由 Jason 提供,可能是我读过的关于实际实现选择的最详尽的参考资料,以及您可能遇到的错误:
https://github.com/xamarin/Xamarin.Forms/issues/6891#issuecomment-511936153
更新以防你也碰到这个: “框架没有选定状态” 更多讨论:https://forums.xamarin.com/discussion/174791/using-the-visualstatemanager-in-a-collectionviews-itemtemplate
感谢您的帮助和鼓励。