在 Xamarin Forms 应用程序中显示操作菜单
Show Action Menu in Xamarin Forms Application
在我的应用程序中,我的屏幕右下角有一个按钮 Action,单击它时,应该会出现图像中的内容。我不确定如何获得它,看起来类似于 android 中的上下文菜单。需要帮助!
正如那里提到的评论,您可以使用 Rg.Plugins.Popup 来实现您的要求。我写了一些简单的代码,你可以看看:
操作菜单页面,它继承了PopupPage
,我用一个listView来显示选项,你可以自定义viewCell
在右侧添加图像。同时处理ListView_ItemSelected
:
中的点击事件
public partial class Page1 : PopupPage
{
public Page1 ()
{
InitializeComponent ();
listView.ItemsSource = new List<string>
{
"first",
"second",
"third",
"fourth",
"fifth",
};
}
private async void OnClose(object sender, EventArgs e)
{
await PopupNavigation.Instance.PopAsync();
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//
var index = (listView.ItemsSource as List<string>).IndexOf(e.SelectedItem as string);
Console.WriteLine(index);
}
}
并且在 Xaml 中:
<StackLayout VerticalOptions="End" HorizontalOptions="FillAndExpand" Padding="20, 20, 20, 20" >
<Frame CornerRadius="25" Padding="0" Margin="0,0,0,0" BorderColor="Red" HasShadow="False" IsClippedToBounds="True">
<StackLayout BackgroundColor="White" VerticalOptions="End" >
<ListView x:Name="listView" HeightRequest="250" RowHeight="50" ItemSelected="ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="Black" Text="{Binding .}"></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Frame>
<Frame CornerRadius="25" Padding="0" BackgroundColor="White" IsClippedToBounds="True">
<StackLayout BackgroundColor="White" VerticalOptions="End" HeightRequest="50">
<Button Text="Close" TextColor="#A9D1DE" Clicked="OnClose"></Button>
</StackLayout>
</Frame>
</StackLayout>
要使用此页面:
await PopupNavigation.Instance.PushAsync(new Page1());
您应该修改详细信息以满足您的要求。如自定义viewCell
、禁用ListView
的滚动能力等
这是 iOS 中的样子:
在我的应用程序中,我的屏幕右下角有一个按钮 Action,单击它时,应该会出现图像中的内容。我不确定如何获得它,看起来类似于 android 中的上下文菜单。需要帮助!
正如那里提到的评论,您可以使用 Rg.Plugins.Popup 来实现您的要求。我写了一些简单的代码,你可以看看:
操作菜单页面,它继承了PopupPage
,我用一个listView来显示选项,你可以自定义viewCell
在右侧添加图像。同时处理ListView_ItemSelected
:
public partial class Page1 : PopupPage
{
public Page1 ()
{
InitializeComponent ();
listView.ItemsSource = new List<string>
{
"first",
"second",
"third",
"fourth",
"fifth",
};
}
private async void OnClose(object sender, EventArgs e)
{
await PopupNavigation.Instance.PopAsync();
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//
var index = (listView.ItemsSource as List<string>).IndexOf(e.SelectedItem as string);
Console.WriteLine(index);
}
}
并且在 Xaml 中:
<StackLayout VerticalOptions="End" HorizontalOptions="FillAndExpand" Padding="20, 20, 20, 20" >
<Frame CornerRadius="25" Padding="0" Margin="0,0,0,0" BorderColor="Red" HasShadow="False" IsClippedToBounds="True">
<StackLayout BackgroundColor="White" VerticalOptions="End" >
<ListView x:Name="listView" HeightRequest="250" RowHeight="50" ItemSelected="ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="Black" Text="{Binding .}"></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Frame>
<Frame CornerRadius="25" Padding="0" BackgroundColor="White" IsClippedToBounds="True">
<StackLayout BackgroundColor="White" VerticalOptions="End" HeightRequest="50">
<Button Text="Close" TextColor="#A9D1DE" Clicked="OnClose"></Button>
</StackLayout>
</Frame>
</StackLayout>
要使用此页面:
await PopupNavigation.Instance.PushAsync(new Page1());
您应该修改详细信息以满足您的要求。如自定义viewCell
、禁用ListView
的滚动能力等
这是 iOS 中的样子: