【问题标题】:Disable ScrollView in Xamarin Forms without disabling buttons在不禁用按钮的情况下禁用 Xamarin 表单中的 ScrollView
【发布时间】:2019-03-24 21:16:25
【问题描述】:

我有以下 XAML。我想以滚动视图为目标手机,并希望在平板电脑上禁用滚动。

 <ScrollView InputTransparent="False" Orientation="Both" >

            <ScrollView.IsEnabled>
                <OnIdiom x:TypeArguments="x:Boolean">

                    <OnIdiom.Phone>True</OnIdiom.Phone>
                    <OnIdiom.Tablet>True</OnIdiom.Tablet>
                </OnIdiom>
            </ScrollView.IsEnabled>

    <StackLayout VerticalOptions="FillAndExpand" BackgroundColor="White" >
                <StackLayout.HorizontalOptions>
                    <OnIdiom x:TypeArguments="LayoutOptions">
                        <OnIdiom.Tablet>FillAndExpand</OnIdiom.Tablet>
                        <OnIdiom.Phone>Start</OnIdiom.Phone>
                    </OnIdiom>

                </StackLayout.HorizontalOptions>

                <Grid BackgroundColor="White" HeightRequest="65" MinimumHeightRequest="65">
                    <Grid.HorizontalOptions>
                        <OnIdiom x:TypeArguments="LayoutOptions">
                            <OnIdiom.Tablet>CenterAndExpand</OnIdiom.Tablet>
                            <OnIdiom.Phone>Start</OnIdiom.Phone>
                        </OnIdiom>

                    </Grid.HorizontalOptions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  Width="Auto"  />
                    </Grid.ColumnDefinitions>
                    <WebView x:Name="webViewBtn1" HeightRequest="65" Grid.Column="0" Grid.ColumnSpan="1"  VerticalOptions="FillAndExpand" BackgroundColor="White">
                    <WebView.HorizontalOptions>
                        <OnIdiom x:TypeArguments="LayoutOptions">
                            <OnIdiom.Tablet>CenterAndExpand</OnIdiom.Tablet>
                            <OnIdiom.Phone>Start</OnIdiom.Phone>
                        </OnIdiom>
                    </WebView.HorizontalOptions>
                        <WebView.WidthRequest>
                            <OnIdiom x:TypeArguments="x:Double">
                                <OnIdiom.Tablet>770</OnIdiom.Tablet>
                                <OnIdiom.Phone>300</OnIdiom.Phone>

                            </OnIdiom>

                        </WebView.WidthRequest>
                    </WebView>
                    <Button Grid.Column="0" Grid.ColumnSpan="1" x:Name="btn1" Clicked="btn1_Clicked" BackgroundColor="Transparent" TextColor="Transparent"  BorderColor="White" />
                </Grid>
    </StackLayout>
        </ScrollView>

如果我按以下方式设置 ScrollView.IsEnabled,则按钮不再允许用户单击它们:

  <OnIdiom.Tablet>False</OnIdiom.Tablet>

我假设使用 InputTransparent 是不正确的。有没有办法让按钮在禁用滚动的滚动视图中可点击?

我基本上是在寻找类似 Orientation=None 的东西,但这不是一个选择。

【问题讨论】:

  • 如果滚动视图的内容在滚动视图的范围内,默认情况下滚动将被禁用,因此没有理由区分手机和平板电脑。

标签: xaml xamarin.forms xamarin.ios scrollview


【解决方案1】:

您需要编写一个 CustomRenderer 来禁用滚动。

在 iOS 上 UIScrollView 有一个 ScrollEnabled 属性

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);
    // IsScrollEnabled just a custom property
    // handled it in OnPropertyChanged too
    ScrollEnabled = Element.IsScrollEnabled;
}

Android有点棘手,没有直接属性。我们拦截触摸事件并返回而不处理它。

public override bool OnInterceptTouchEvent(MotionEvent ev)
{
    if (Element.IsScrollEnabled)
    {
        return base.OnInterceptTouchEvent(ev);
    }
    else
    {
        return false;
    }
}

public override bool OnTouchEvent(MotionEvent ev)
{
    if (Element.IsScrollEnabled)
    {
        return base.OnTouchEvent(ev);
    }
    else
    {
        return false;
    }
}

【讨论】:

  • iOS 的解决方案很棒,谢谢!我们能否在 Android 上区分用于滚动和点击子按钮的触摸事件?
  • 我还没有尝试过,但是触摸事件应该通过所有包含的元素。
  • 它确实会传播到子元素,但它仍然会滚动。至少它对我有用。在OnTouchEvent 中返回true 而不是false 似乎不会影响这一点。从OnInterceptTouchEvent 返回true 确实会阻止滚动发生,但也会阻止孩子。
【解决方案2】:

我最终使用这种方法来禁用 iPad 上的垂直滚动,这是我的目标设备。不适合 android 7 英寸平板电脑,但哦,好吧:

            <ScrollView.Orientation>
                <OnPlatform x:TypeArguments="ScrollOrientation">
                    <On Platform="iOS">
                        <OnIdiom x:TypeArguments="ScrollOrientation">
                            <OnIdiom.Phone>Both</OnIdiom.Phone>
                            <OnIdiom.Tablet>Horizontal</OnIdiom.Tablet>
                        </OnIdiom>
                    </On>
                    <On Platform="Android">
                        <OnIdiom x:TypeArguments="ScrollOrientation">
                            <OnIdiom.Phone>Both</OnIdiom.Phone>
                            <OnIdiom.Tablet>Both</OnIdiom.Tablet>
                        </OnIdiom>
                    </On>
                    <On Platform="UWP">Both</On>
                </OnPlatform>
            </ScrollView.Orientation>

【讨论】:

    【解决方案3】:

    在最新版本的 Xamarin Forms 中,您可以将 Orientation 设置为两者。

    scrollV.Orientation = ScrollOrientation.Neither;
    

    【讨论】:

      【解决方案4】:

      通过 ScrollEnabled 扩展方法在 Xamarin Forms 中禁用滚动而不影响其子项的酷而紧凑的方法:

      public static class ScrollViewEx
      {
          /// <summary>
          /// Disables scrollview by modifying Scrolled Event and attaching itself to ScrollView's binding context
          /// Scrolled event sends it back to the original x,y
          /// </summary>
          public class DisabledScrollClass : IDisposable
          {
              private double ScrollX;
              private double ScrollY;
              private object OldContext;
              private ScrollView Parent;
      
              public DisabledScrollClass(ScrollView parent)
              {
                  Parent = parent;
                  ScrollX = parent.ScrollX;
                  ScrollY = parent.ScrollY;
                  OldContext = parent.BindingContext;
                  parent.Scrolled += Scrolled;
                  parent.BindingContext = this;
              }
      
              private void Scrolled(object sender, ScrolledEventArgs e)
              {
                  (sender as ScrollView)?.ScrollToAsync(ScrollX, ScrollY, false);
              }
      
              public void Dispose()
              {
                  Parent.Scrolled -= Scrolled;
                  Parent.BindingContext = OldContext;
              }
          }
      
          public static ScrollView ScrollEnabled(this ScrollView scroll, bool isEnabled)
          {
              DisabledScrollClass binding = scroll.BindingContext as DisabledScrollClass;
              if (isEnabled && binding != null)
                  binding.Dispose();
              if (!isEnabled && binding == null)
                  _ = new DisabledScrollClass(scroll);
              return scroll;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-14
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-19
        • 1970-01-01
        相关资源
        最近更新 更多