Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
Listview 如何在母版详细信息页中设置列表视图的默认选定项_Listview_Xamarin_Xamarin.forms - Fatal编程技术网

Listview 如何在母版详细信息页中设置列表视图的默认选定项

Listview 如何在母版详细信息页中设置列表视图的默认选定项,listview,xamarin,xamarin.forms,Listview,Xamarin,Xamarin.forms,我正在引用Xamarin Master Detail示例应用程序。在这种情况下,我们在母版详细信息页面中使用ListView。我们希望在启动MasterDetailPage时将第一项设置为选中项 我发现处理此问题的最佳方法是停止依赖内置的列表视图选择样式,并自行处理选择的背景更改。不幸的是,禁用默认选择样式需要自定义渲染 实现下面的ViewCell渲染器以禁用默认选择样式。然后,您只需将您的ViewCell子控件的BackgroundColor绑定到模型上的IsSelected属性,或者以您想要

我正在引用Xamarin Master Detail示例应用程序。在这种情况下,我们在母版详细信息页面中使用ListView。我们希望在启动MasterDetailPage时将第一项设置为选中项


我发现处理此问题的最佳方法是停止依赖内置的
列表视图
选择样式,并自行处理选择的背景更改。不幸的是,禁用默认选择样式需要自定义渲染

实现下面的
ViewCell
渲染器以禁用默认选择样式。然后,您只需将您的
ViewCell
子控件的
BackgroundColor
绑定到模型上的
IsSelected
属性,或者以您想要的方式进行绑定

要设置默认选定项目时:

mySelectedItem.IsSelected = true; //Assuming this bubbles up a PropertyChanged event to the ListView

MyListView.SelectedItem = mySelectedItem;
// Maybes scroll to the item here if necessary
下面的代码并不完美,所以请告诉我是否可以进行任何改进

Xamarin形成ViewCell

public class CustomViewCell : ViewCell { }
Android ViewCell自定义渲染器

public class CustomViewCellRenderer : ViewCellRenderer {

    protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context) {
        View cellCore = base.GetCellCore(item, convertView, parent, context);

        if(parent is ListView listView) {
            listView.SetSelector(Android.Resource.Color.Transparent);
            listView.CacheColorHint = Color.White;
        }

        cellCore.SetBackground(GetUnpressedBackground());

        return cellCore;
    }


    private StateListDrawable GetUnpressedBackground() {
        StateListDrawable states = new StateListDrawable();
        //states.AddState(new[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.White));
        //states.AddState(new[] { Android.Resource.Attribute.StateFocused }, new ColorDrawable(Color.White));
        //states.AddState(new int[] { }, new ColorDrawable(Color.Blue.ToAndroid()));

        return states;
    }
}
public class CustomViewCellRenderer : ViewCellRenderer {

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) {
        UITableViewCell  cell = base.GetCell(item, reusableCell, tv);

        if(cell != null) {
            // Disable native cell selection color style - set as *Transparent*
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            //cell.SelectedBackgroundView = new UIView(new RectangleF(0, 0, (float)cell.Bounds.Width, (float)cell.Bounds.Height)) {
            //    BackgroundColor = UIColor.Clear
            //};
        }

        return cell;
    }
}
iOS ViewCell自定义渲染器

public class CustomViewCellRenderer : ViewCellRenderer {

    protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context) {
        View cellCore = base.GetCellCore(item, convertView, parent, context);

        if(parent is ListView listView) {
            listView.SetSelector(Android.Resource.Color.Transparent);
            listView.CacheColorHint = Color.White;
        }

        cellCore.SetBackground(GetUnpressedBackground());

        return cellCore;
    }


    private StateListDrawable GetUnpressedBackground() {
        StateListDrawable states = new StateListDrawable();
        //states.AddState(new[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.White));
        //states.AddState(new[] { Android.Resource.Attribute.StateFocused }, new ColorDrawable(Color.White));
        //states.AddState(new int[] { }, new ColorDrawable(Color.Blue.ToAndroid()));

        return states;
    }
}
public class CustomViewCellRenderer : ViewCellRenderer {

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) {
        UITableViewCell  cell = base.GetCell(item, reusableCell, tv);

        if(cell != null) {
            // Disable native cell selection color style - set as *Transparent*
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            //cell.SelectedBackgroundView = new UIView(new RectangleF(0, 0, (float)cell.Bounds.Width, (float)cell.Bounds.Height)) {
            //    BackgroundColor = UIColor.Clear
            //};
        }

        return cell;
    }
}

到目前为止你尝试了什么?您可以设置SelectedItem属性。@Depechie:我尝试使用Selected Item属性,但菜单项没有突出显示。您可以使用文件设置默认的选定页面。在这个示例中,他们将联系人页面设置为选中状态。我们正在与UWP合作。其中我们需要在Xamarin.Forms中设置默认项。@AnkitJain查找UWP ViewCell渲染器(如果UWP上需要),然后按照上面的说明进行操作。