如何在MasterDetailPage XamarinForms

时间:2018-01-04 11:19:19

标签: c# xamarin.forms

我有masterdetailpage里面有Logout功能(所以导航到其他页面它会显示显示警告),但我不知道如何在masterdetailpage中插入logout方法,我已经尝试过使用ICommand但似乎它没有工作并使我的申请人关闭。 这是我的MasterPageItem模型

 public class MasterPageItem
    {
        public string Title { get; set; }
        public string Icon { get; set; }
        public Type TargetType { get; set; }
        public ICommand Commando { get; set; }



    }

这是MasterDetailPage的列表视图

 <ListView x:Name="navigationDrawerList"
                  RowHeight="45"
                  SeparatorVisibility="None"
                  BackgroundColor="#000000"
                  ItemSelected="OnMenuItemSelected">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>

                                <!-- Main design for our menu items -->
                                <StackLayout BackgroundColor="#000000" VerticalOptions="FillAndExpand"
                             Orientation="Horizontal"
                             Padding="20,10,0,10"
                             Spacing="20">

                                    <Image Source="{Binding Icon}"
                         WidthRequest="60"
                         HeightRequest="60"
                         VerticalOptions="Center" />

                                    <Label FontFamily="Panton-LightCaps.otf#Panton-LightCaps" Text="{Binding Title}"
                         FontSize="Medium"
                         VerticalOptions="Center"
                         TextColor="White"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

我尝试插入像这样的方法

 public ICommand GetOff { get; private set; }
    public MainPage()
    {
      GetOff = new Command(LogoutCommand) 
        var page9 = new MasterPageItem() { Title = "LOGOUT",  Commando = GetOff  };


}


  public async void LogoutCommand ()
        {
            var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No");
            if (result == true)
            {
                App.AuthenticationClient.UserTokenCache.Clear(Constants.ApplicationID);
                Application.Current.MainPage = new NavigationPage(new NewPageLogin());


            }
        }

还有另一种在MasterdetailPage中插入方法的方法吗?任何建议将不胜感激

1 个答案:

答案 0 :(得分:1)

选项1

您可以在列表视图中添加页脚并为其添加点击手势识别器,如下所示:

<ListView.Footer>
    <StackLayout BackgroundColor="#000000" 
                VerticalOptions="FillAndExpand"
                Orientation="Horizontal"
                Padding="20,10,0,10"
                Spacing="20">
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding LogoutCommand}" />
        </StackLayout.GestureRecognizers>

        <Image Source="YourIcon"
        WidthRequest="60"
        HeightRequest="60"
        VerticalOptions="Center" />

        <Label FontFamily="Panton-LightCaps.otf#Panton-LightCaps" 
        Text="{Binding Title}"
        FontSize="Medium"
        VerticalOptions="Center"
        TextColor="White"/>
    </StackLayout>
</ListView.Footer>

它应该放在ListView标记内。 如您所见,它支持Command,因此您可以使用已有的那个。

选项2

您可以将注销项目的TargetType设置为null,并执行以下操作:

private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var item = e.SelectedItem as MasterPageItem;
    if (item == null)
        return;

    //  Check if sign out was tapped
    if (item.TargetType != null)
    {
        var page = (Page)Activator.CreateInstance(item.TargetType);
        page.Title = item.Title;

        Detail = new NavigationPage(page);
        IsPresented = false;
    }
    else
    {
        //  Manage your sign out action
        var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No");
        if (result == true)
        {
            App.AuthenticationClient.UserTokenCache.Clear(Constants.ApplicationID);
            Application.Current.MainPage = new NavigationPage(new NewPageLogin());
        }
    }
}