Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios MvvmCross和UICollectionView如何将SelectedItem从VM绑定到视图_Ios_Xamarin.ios_Xamarin_Mvvmcross - Fatal编程技术网

Ios MvvmCross和UICollectionView如何将SelectedItem从VM绑定到视图

Ios MvvmCross和UICollectionView如何将SelectedItem从VM绑定到视图,ios,xamarin.ios,xamarin,mvvmcross,Ios,Xamarin.ios,Xamarin,Mvvmcross,我正在将MvvmCross与UICollectionView一起使用。 绑定工作得很好,我的所有数据都正确显示,即使我在CollectionView中选择了一个项目,它也会在我的ViewModel中正确设置。 对于SelectedItem,我使用以下绑定: set.Bind(_collectionViewSource).For(x => x.SelectedItem).To(vm => vm.SelectedMachine); 我唯一的问题是,我希望最初选择第一个Collectio

我正在将MvvmCross与UICollectionView一起使用。 绑定工作得很好,我的所有数据都正确显示,即使我在CollectionView中选择了一个项目,它也会在我的ViewModel中正确设置。 对于SelectedItem,我使用以下绑定:

set.Bind(_collectionViewSource).For(x => x.SelectedItem).To(vm => vm.SelectedMachine);
我唯一的问题是,我希望最初选择第一个CollectionViewItem。 正如MvvmCross的来源所说,目前不支持(在
SelectedItem
的setter中):

那么,执行项目初始预选的最佳方式是什么?我可以在什么地方调用
\u collectionView。从中选择Item


我尝试在集合更改时调用它,但似乎不起作用。

如果需要此功能,应该能够从MvxCollectionViewSource继承并添加类似的属性

    public event EventHandler SelectedItemExChanged;

    public object SelectedItemEx
    {
        get { return base.SelectedItem; }
        set
        {
            base.SelectedItem = value;
            var index = FindIndexPath(value); // find the NSIndexPath of value in the collection
            if (index != null)
               _collectionView.SelectItem(index, true, UICollectionViewScrollPosition.CenteredHorizontally);
            var handler = SelectedItemExChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);    
        }
    }
然后可以绑定,而不是
SelectedItem

我可以在什么地方调用_collectionView.SelectItem from?我试着在集合更改时调用它,但似乎不起作用


如果这不起作用,那么我不确定-您可能会遇到动画计时问题-请参阅以下问题-可能尝试编辑您的问题以发布更多的代码-人们可以更容易地通过调试实现这一点。

谢谢Stuart,我将此标记为答案,因为它完全正确。对我来说,我只是愚蠢地使用
nsindepath.FromIndex
而不是
nsindepath.FromItemSection
。现在使用后者,一切都很好。
    public event EventHandler SelectedItemExChanged;

    public object SelectedItemEx
    {
        get { return base.SelectedItem; }
        set
        {
            base.SelectedItem = value;
            var index = FindIndexPath(value); // find the NSIndexPath of value in the collection
            if (index != null)
               _collectionView.SelectItem(index, true, UICollectionViewScrollPosition.CenteredHorizontally);
            var handler = SelectedItemExChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);    
        }
    }