I want to display an Action Sheet from the ViewModel

Ronald Rex 1,666 Reputation points
2024-01-04T15:48:51.6366667+00:00

I am trying to display an action sheet from the viewmodel when the user taps on an Item in the ListView using commanding. Currently I have this in my Xaml. But I get a compiler error message stating that Event ItemTapped can only be bound to properties of delegate type EventHandler

 <ListView x:Name="listView" ItemsSource="{Binding CustomerList}" FlexLayout.Grow="1" HasUnevenRows="True" ItemTapped="{Binding LVItemTappedCommand}" >

This is the Method in my ViewModel

 [RelayCommand]   
 private void LVItemTapped()  
 { 
     // Handle the tapped item here  

 }

This is how it looks in the code behind but since I am using MVVM I only want code in my ViewModel

private async void listview_ItemTapped(object sender, ItemTappedEventArgs e)
{
    var customer = (Customer)e.Item;
    var action = await DisplayActionSheet("Action", "Cancel", null, "Edit", "Delete");

    switch (action)
    {
        case "Edit":
            _editCustomerId = customer.Id;
            //nameEntryField.Text = customer.customer_name;
            //emailEntryField.Text = customer.Email;
            //mobileEntryField.Text = customer.Mobile;

            break;
        case "Delete":
            //await _dbService.Delete(customer);
            //listView.ItemsSource = await _dbService.GetCustomers();
            break;
    }
}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,890 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,268 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,576 Reputation points Microsoft Vendor
    2024-01-05T01:18:16.6966667+00:00

    Hello,

    To call Event in MVVM mode, you need to use the ToolKit plug-in provided by Maui to complete this function.

    Please refer to the following official documentation and instructions.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful