在Xamarin android中从右向左滑动抽屉

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Xamarin android中从右向左滑动抽屉相关的知识,希望对你有一定的参考价值。

我有问题从右到左为xamarin android打开滑动抽屉。但是下面用于显示滑动的代码

抽屉从左到右。如果MasterDetailPage的任何属性从右向左打开,请与我分享您的观点。这里滑动抽屉从左到右打开,但我想从右到左打开Xamarin Android任何关于此的想法。请分享你的想法。我们还使用MenuRootPage在Sliding Drawer中添加项目:

     Code for items view:
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                               >

        <ContentPage.Content>
            <StackLayout   BackgroundColor="{StaticResource ToolbarStatusBackgroundColor}"  Orientation="Vertical" Spacing="20">
                <StackLayout>
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{mm:Command SliderClose}" />
                    </StackLayout.GestureRecognizers>
                    <Image Source="menu.png" HorizontalOptions="Start" Margin="10,40,0,0" />
                </StackLayout>
                <BoxView HorizontalOptions="EndAndExpand"  HeightRequest="1" WidthRequest="1000" Color="{StaticResource SeparatorColor}"  />
                <Label Text="My account" Style="{StaticResource ToolbarStatusDescriptionLabel}" Margin="10"  >
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer Command="{mm:Command SliderClose}" />
                    </Label.GestureRecognizers>
                </Label>
                <BoxView HorizontalOptions="EndAndExpand"  HeightRequest="1" WidthRequest="1000" Color="{StaticResource SeparatorColor}"  />

                <Label Text="Settings" Style="{StaticResource ToolbarStatusDescriptionLabel}" Margin="10">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer Command="{mm:Command SliderClose}" />
                    </Label.GestureRecognizers>
                </Label>
                <BoxView HorizontalOptions="EndAndExpand"  HeightRequest="1" WidthRequest="1000" Color="{StaticResource SeparatorColor}"  />

                <Label Text="Logout" Style="{StaticResource ToolbarStatusDescriptionLabel}" HorizontalTextAlignment="Center"  VerticalOptions="EndAndExpand" Margin="10">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer  Command="{mm:Command Logout}" />
                    </Label.GestureRecognizers>
                </Label>

            </StackLayout>
        </ContentPage.Content>
    </ContentPage>

        In MenuRootPage .xaml file we are declare master page only UI section
        In MenuRootPage .cs file: here we are inherit MasterDetailPage properties

            Using code:
                     Root page in MenuRootPage .xaml file:
                    <MasterDetailPage  xmlns="http://xamarin.com/schemas/2014/forms"
                                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                                 x:Class="Ansible.Vektor.App.Shared.Views.MenuRootPage" >
                    </MasterDetailPage> 



                    Root MenuRootPage .cs file:
                    public partial class MenuRootPage : MasterDetailPage
                        {
                            public MenuRootPage ()
                            {
                                InitializeComponent();
                                MasterBehavior = MasterBehavior.Popover;

                            }
                        }



            App.xaml.cs:

             var menuPage = new MenuPages() { BindingContext = MenuPagesViewModel };
                        NavigationPage = new NavigationPage(new MainView() { 
                        BindingContext = MainViewModel });
                        MenuRootPage = new MenuRootPage();
                        MenuRootPage.Master = menuPage;
                        MenuRootPage.Detail = NavigationPage;
                        MenuIsGestureEnabled = false;
                        MainPage = MenuRootPage;


                Here Sliding Drawer open from left to right but i want to open from right to left for Xamarin Android
                Any idea regarding this. please share your idea's.
答案

在Android中为主细节页面进行自定义渲染。

public class MyMasterDetailPageRenderer : MasterDetailPageRenderer
{
    protected override void OnElementChanged(VisualElement oldElement, VisualElement newElement)
    {
        base.OnElementChanged(oldElement, newElement);
        var fieldInfo = GetType().BaseType.GetField("_masterLayout", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        var _masterLayout = (ViewGroup)fieldInfo.GetValue(this);
        var lp = new DrawerLayout.LayoutParams(_masterLayout.LayoutParameters);
        lp.Width = 300;
        lp.Gravity = (int)GravityFlags.Right;
        _masterLayout.LayoutParameters = lp;
    }
 }

以上是关于在Xamarin android中从右向左滑动抽屉的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Xamarin(iOS) 中将按钮从右向左移动

使用 animate() 从右向左滑动 div

jQuery从右向左滑动

如何使div从右向左滑动

未调用时滑动手势从右向左移动

如何以编程方式显示/隐藏导航抽屉