Xamarin表格 - 标签点击时可打开多页

时间:2016-08-12 08:34:22

标签: c# .net wpf xaml xamarin.forms

我正在使用一些标签,当用户点击其中一个标签时,它会打开一个新页面。但问题出在这里,每当有人点击其中一个标签时,它就会打开多个页面。

我将附上下面的xaml和代码。

的Xaml

 <StackLayout x:Name="dropdownStack" Padding="10" VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Vertical" BackgroundColor="#033c73" IsVisible="False">  
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ddm_Home"/>  
      <Label x:Name="ddmenu_Home" HorizontalOptions="FillAndExpand" Text="Home" TextColor="#ffffff"/>
    </StackLayout> 
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ddm_Social"/> 
      <Label x:Name="ddmenu_Social" HorizontalOptions="FillAndExpand" Text="Social" TextColor="#ffffff"/>
    </StackLayout>   
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ddm_Careers"/>  
      <Label x:Name="ddmenu_Careers" HorizontalOptions="FillAndExpand" Text="Careers" TextColor="#ffffff"/>
    </StackLayout>  
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ddm_Schedule"/>
      <Label x:Name="ddmenu_MySchedule" HorizontalOptions="FillAndExpand" Text="My Schedule" TextColor="#ffffff"/>
    </StackLayout>
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ddm_Contact"/>
      <Label x:Name="ddmenu_Contact" HorizontalOptions="FillAndExpand" Text="Contact" TextColor="#ffffff"/>
    </StackLayout> 
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ddm_Details"/>
      <Label x:Name="ddmenu_MyDetails" HorizontalOptions="FillAndExpand" Text="My Details" TextColor="#ffffff"/>
    </StackLayout>
    <StackLayout VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
      <Image Source="ic_video"/>
      <Label x:Name="ddmenu_Videos" HorizontalOptions="FillAndExpand" Text="Videos" TextColor="#ffffff"/>
    </StackLayout>
  </StackLayout>

C#

public void OnDropdownItemPressed()
{
    dropdownStack.IsVisible = !dropdownStack.IsVisible;

    ddmenu_Home.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() =>
        {
            Navigation.PushAsync(new MainPage());
        })
    });

    ddmenu_Social.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() =>
        {
            Navigation.PushAsync(new SocialPage());
        })
    });

    ddmenu_Careers.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() =>
        {
            Navigation.PushAsync(new CareersPage());
        })
    });

    ddmenu_MySchedule.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() =>
        {
            RegisterDeviceWithWebview(ProjectVariables.URL + ProjectVariables.URL_EXT_SCHEDULE, ProjectVariables.regID);
        })
    });

    ddmenu_Contact.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() =>
        {
            RegisterDeviceWithWebview(ProjectVariables.URL + ProjectVariables.URL_EXT_CONTACT, ProjectVariables.regID);
        })
    });

    ddmenu_MyDetails.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() =>
        {
            RegisterDeviceWithWebview(ProjectVariables.URL + ProjectVariables.URL_EXT_DETAILS, ProjectVariables.regID);
        })
    });

    ddmenu_Videos.GestureRecognizers.Add(new TapGestureRecognizer
    {
        Command = new Command(() => 
        {
            RegisterDeviceWithWebview(ProjectVariables.URL + ProjectVariables.URL_EXT_VIDEOS, ProjectVariables.regID);
        })
    });
}

如果有人可以提供帮助,那就太棒了。

4 个答案:

答案 0 :(得分:2)

我有假设。每次调用方法OnDropdownItemPressed时,您都会添加GestureRecognizers,因此每个Label都有许多识别器而不是一个识别器。所以,我想建议在页面的构造函数中执行所有Recognizers内容。

答案 1 :(得分:1)

删除包含图片和标签的每个VerticalOptions="Fill"上的StackLayout。使用此选项,您可以告诉StackLayout它应填充空格。

我认为这就是问题所在,所有的StackLayout都是堆叠的(它们不应该堆叠在一起,只是逐个堆叠)。

答案 2 :(得分:1)

您应该在点击后立即停用按钮或图片并等待按下页面然后重新启用

ddmenu_Home.GestureRecognizers.Add(new TapGestureRecognizer
            {
                Command = new Command(() =>
                {
                    ddmenu_Home.IsEnabled = false;
                    await Navigation.PushAsync(new MainPage());
                    ddmenu_Home.IsEnabled = true;
                })
            });

答案 3 :(得分:0)

请参考以下方法。

if (Application.Current.MainPage.Navigation.NavigationStack.Count == 0 || Application.Current.MainPage.Navigation.NavigationStack.Last().GetType() != typeof(YourPage))
{
    await navigationService.PushTo(new YourPage);
}