gpt4 book ai didi

c# - Xamarin Forms - 点击标签时打开多个页面

转载 作者:行者123 更新时间:2023-11-30 20:34:38 27 4
gpt4 key购买 nike

我正在使用几个标签,当用户点击其中一个标签时,它会打开一个新页面。但问题是,只要有人点击其中一个标签,它就会打开多个页面。

我将在下面附上 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);
})
});
}

如果有人能提供帮助那就太好了。

最佳答案

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

关于c# - Xamarin Forms - 点击标签时打开多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38913476/

27 4 0