Vertical slider in Xamarin.forms?

余生颓废 提交于 2019-12-02 05:24:42

问题


I am trying to implement Vertical slider in Xamarin.forms. I know for that I need to create render classes in ios and android respectively. For ios, my renderer seems to be working fine. For android I am following the link https://forums.xamarin.com/discussion/69933/vertical-slider. However the solution provided in above link left me with a slider with no thumb. Is there any way to implement vertical slider for android in Xamarin.forms?


回答1:


This works, sort of. It still has a lot of irritating weirdness to it, but, does the core job of: Has vertical sliders, align next to each other.

<AbsoluteLayout x:Name="slidersAbs" HeightRequest="300" HorizontalOptions="CenterAndExpand" WidthRequest="800">
    <Slider Rotation="-90" Minimum="0" Maximum="50" Value="0"
        AbsoluteLayout.LayoutBounds="0,.5,300,50" AbsoluteLayout.LayoutFlags="PositionProportional"/>
    <Slider Rotation="-90" Minimum="0" Maximum="80" Value="0"
        AbsoluteLayout.LayoutBounds=".5,.5,300,50" AbsoluteLayout.LayoutFlags="PositionProportional"/>
    <Slider Rotation="-90" Minimum="0" Maximum="100" Value="0"
        AbsoluteLayout.LayoutBounds="1,.5,300,50" AbsoluteLayout.LayoutFlags="PositionProportional"/>
</AbsoluteLayout>

Some things I've gleaned from experimentation:

  1. I had to set that WidthRequest to an insanely high number to get the sliders to space out horizontally at all; putting a border on the abs layout might help sort out what's going on there.
  2. You'll notice only the Position (X,Y) is Proportional; if you let the height/width be, it freaks out the way you were running into in other layout approaches, where height and width invert themselves but are still proportional to the screen in the inverse, it's a mess. You just can't use width/height proportional when rotated, at all.
  3. You'd think the insane width and things would cause some kind of scrolling dilemma or other conflict but it actually works perfectly.
  4. I've set the Maximum to 50, 80, 100 so it's easy to tell which is which. As you'd hope, 0 is placed at the bottom of each (not the top - Rotation=90 puts 0 at top, -90 fixes this), and the last slider appears on the right.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/slider https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/absolute-layout




回答2:


I do this way. I think is intuitive and I don't have to worry about coordinates. X and Y are still relative to the wanted actual Left and Top of the Slider. Note the Rectangle bounds in SetLayoutBounds has Height and Width swapped on purpose.

  Slider thisfader = new Slider();
  //coordinates swapped to accomodate rotation
  AbsoluteLayout.SetLayoutBounds(thisfader, new Rectangle(X, Y + Height, Height, Width));
  thisfader.AnchorX = 0; 
  thisfader.AnchorY = 0; //these set the rotation anchor on top left
  thisfader.Rotation = -90;

  AbsoluteLayout.Children.Add(thisfader);



回答3:


You can also check this blog for complete source code of vertical sliders in xamarin forms in android and iOS

https://xamarinexperience.blogspot.com/search?q=Vertical+slider



来源:https://stackoverflow.com/questions/42972823/vertical-slider-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!