I have a ListView on an Android platform, with Xamarin Forms. When clicking on an item in the list, the context menu for each item appears with some options; i'm working with pattern MVVM. So far, ball show.
I would like to change the layout or the way the menu is displayed. I want the context menu to appear the same in UWP, as a popup over the selected item, but in Android, which is the platform I'm using. As shown here, is it only possible to have this view on UWP or on Android too, with custom render, or something similar?
If you want the context menu to appear as a popup over the selected item, the Xamarin.Forms
DisplayActionSheet
method lets you include this control in cross-platforms apps, rendering native alternatives in Android and UWP with popup.
I make a simple example for your reference.
Model:
public class Person
{
public string Name { get; set; }
public string FirstName { get; set; }
public int Age { get; set; }
}
ViewModel:
public class PersonViewModel
{
public ObservableCollection<Person> peoples { get; set; }
public PersonViewModel()
{
peoples = new ObservableCollection<Person>()
{
new Person(){ Name = "A", FirstName ="a", Age =10 },
new Person(){ Name = "B", FirstName ="b", Age =11 },
new Person(){ Name = "C", FirstName ="c", Age =12 },
new Person(){ Name = "D", FirstName ="d", Age =13 },
new Person(){ Name = "E", FirstName ="e", Age =14 },
new Person(){ Name = "F", FirstName ="f", Age =15 },
};
}
}
Xaml:
<ListView ItemsSource="{Binding peoples}" ItemSelected="ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Name}"></Label>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
public Page5()
{
InitializeComponent();
this.BindingContext = new PersonViewModel();
}
private async void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var selectedItem = e.SelectedItem as Person;
await DisplayActionSheet("Details Page:", "Cancel", null, selectedItem.FirstName, selectedItem.Age.ToString());
}