首页 文章

Xamarin表单Listview和scrollview滚动条如何设置可见?

提问于
浏览
1

我正在使用xamarin表单应用程序(android目标) .

视图包含滚动视图 . 另一个视图包含listview .

在两个滚动工作中都很棒 .

问题是当用户用手指触摸屏幕时,滚动条仅在点击时出现 . 用户无法理解他必须滚动视图 .

我读了关于android native“ScrollView.setScrollbarFadingEnabled(false);”的其他帖子

在xamarain表单中找不到类似的属性或方法...有没有办法实现相同的目标?

2 回答

  • 1

    作为@MalokuS的补充 . 以下是您可以尝试的示例 .

    在您的共享/ PCL项目中创建一个新类 . 此类将包含制作渲染器所需的可绑定属性 .

    namespace YourProject.Views.CustomControls
    {
    #region CustomScrollview
    
    public class CustomScrollView : ScrollView
    {
        public static readonly BindableProperty IsHorizontalScrollbarEnabledProperty =
        BindableProperty.Create(
            nameof(IsHorizontalScrollbarEnabled),
            typeof(bool),
            typeof(CustomScrollView),
            true,
            BindingMode.Default,
            null);
        /// <summary>
        /// Gets or sets the Horizontal scrollbar visibility
        /// </summary>
        public bool IsHorizontalScrollbarEnabled
        {
            get { return (bool)GetValue(IsHorizontalScrollbarEnabledProperty); }
            set { SetValue(IsHorizontalScrollbarEnabledProperty, value); }
        }
    
    
        public static readonly BindableProperty IsVerticalScrollbarEnabledProperty =
        BindableProperty.Create(
            nameof(IsVerticalScrollbarEnabled),
            typeof(bool),
            typeof(BloopyScrollView),
            false,
            BindingMode.Default,
            null);
        /// <summary>
        /// Gets or sets the Vertical scrollbar visibility
        /// </summary>
        public bool IsVerticalScrollbarEnabled
        {
            get { return (bool)GetValue(IsVerticalScrollbarEnabledProperty); }
            set { SetValue(IsVerticalScrollbarEnabledProperty, value); }
        }
    }
    
    #endregion
    }
    

    在你的android项目中 . 你应该有一个名为“渲染器”的文件 . 在那里创建一个新类并添加以下代码:

    [assembly: ExportRenderer(typeof(CustomScrollView), 
    typeof(CustomScrollViewRenderer))]
    namespace YourProject.Droid.Renderer
    {
    class CustomScrollViewRenderer : ScrollViewRenderer
    {
    
        protected override async void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
    
            this.VerticalScrollBarEnabled = ((CustomScrollView)e.NewElement).IsVerticalScrollbarEnabled;
    
            this.HorizontalScrollBarEnabled = ((CustomScrollView)e.NewElement).IsHorizontalScrollbarEnabled;
    
        }
    
       }
    

    之后,您可以在xaml文件中自由使用新控件:

    <CustomControls:CustomScrollView IsHorizontalScrollbarEnabled = "true"
                    Orientation="Horizontal" HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
    

    希望能帮助到你!干杯

  • 2

    您引用的“问题”是ListView滚动条的默认行为,当屏幕上没有交互时,它会隐藏/淡化 .

    您可以为每个平台制作自定义ListView渲染器:Xamarin ListView Custom Renderers

    在Android平台上特定的自定义渲染器:

    Control.VerticalScrollbarEnabled = true;
    Control.ScrollbarFadingEnabled = false;
    

相关问题