1

I'm new to Xamarin (and new in coding in general).

I'm using xct TouchEffect within a ListView to try to get a LongPress menu.

Since the TouchEffect.LongPressCommand is a Command, I can only bound it to the model page from some reason. So... I'm trying to send information to the Code-behind via MessagingCenter.

The problem I have is the message is not receiving.

I read a lot and tried to figure it out, and I guess to be able to receive the message, the subscriber needs to be instantiate/initialize first.

The main problem I have is... I don't know how to do it lol.

Or the whole thing I'm trying to do is wrong?

I will add some code but if anything else is needed please let me know.

Note: the loading page (when the app start) is a GroupPage(which working fine), the problem is with the ItemsPage.

Thank you so much for everyone.

ItemsPage.xaml

<ContentPage.BindingContext>
    <localvm:ItemViewModel/>
</ContentPage.BindingContext>

<ListView ItemsSource="{Binding Items, Mode=TwoWay}" x:Name="lstView" 
                  AbsoluteLayout.LayoutBounds="0,0,1,1" 
                  AbsoluteLayout.LayoutFlags="All"
                  SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" RowHeight="50">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>

                <Grid Padding="0,0,8,0" Margin="4,0,4,0" xct:TouchEffect.LongPressCommand="{Binding LongPressItemCommand}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="7*"/>
                    </Grid.ColumnDefinitions>

                    <Label Text="{Binding ItemName}" TextColor="Black" Grid.Column="1" FontSize="Medium"></Label>

                    <Label Text="{Binding ItemDescription}" Grid.Column="1" VerticalTextAlignment="End"/>

                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ItemsPage.cs

namespace MobileApp2
{
    public partial class ItemsPage : ContentPage
    {
        public ItemsPage()
        {
            InitializeComponent();
            MessagingCenter.Subscribe<Item, Guid>(this, "PopupMenuItemMsg",
                 (page, itemId) =>
                 {
                     Main_PopupMenu(itemId);
                 });
        }
        public async void Main_PopupMenu(Guid itemId)
        {
            DisplayActionSheet("Test", "Test", "OK");
        }
    }
}

Items.cs (model)

namespace MobileApp2
{
    public class Item : INotifyPropertyChanged
    {
        public Command LongPressItemCommand { get; }

        public Guid ItemId { get; set; }

        public Guid GroupId { get; set; }

        private string itemName = string.Empty;
        public string ItemName
        {
            get { return itemName; }
            set
            {
                if (value != null) itemName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ItemName"));
            }
        }

        private string itemDescription = string.Empty;
        public string ItemDescription
        {
            get
            {

                return itemDescription.Trim();
            }
            set
            {
                if (value != null) itemDescription = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ItemDescription"));
            }
        }


        public Item(string itemName, string itemDescription)
        {
            ItemName = itemName;
            ItemDescription = itemDescription;
        }

        public Item()
        {
            LongPressItemCommand = new Command(() =>
            {
                MessagingCenter.Send<Item, Guid>(this, "PopupMenuItemMsg", ItemId);
            });
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
Noah
  • 15
  • 1
  • 6
  • Does the `LongPressItemCommand` triggered when you long press the grid in ItemsPage? – Wendy Zang - MSFT May 28 '21 at 08:00
  • Yes it does. if I long press the item (on the grid) its getting to this line: ```MessagingCenter.Send(this, "PopupMenuItemMsg", ItemId);``` and then continue without going to the Subscribe Message – Noah May 28 '21 at 08:53
  • I guess maybe there is something wrong with your binding. Please check the code in reply. – Wendy Zang - MSFT Jun 03 '21 at 08:04

1 Answers1

0

You could check the code below with relative binding of Command.

Model:

public class Item
{
    public string ItemName { get; set; }
    public string ItemDescription { get; set; }
   

}

ViewModel:

public class ItemViewModel
{
    public ICommand LongPressItemCommand { get; set; }
    public Guid ItemId { get; set; }
    public ObservableCollection<Item> Items { get; set; }
    public ItemViewModel()
    {
         LongPressItemCommand = new Command(() =>
        {

            MessagingCenter.Send<ItemViewModel, Guid>(this, "PopupMenuItemMsg", ItemId);
        });
        
        CreateCollection();
    }



    public void LongPress()
    {

    }
    public void CreateCollection()
    {
        Items = new ObservableCollection<Item>()
        {
            new Item(){ ItemName="A", ItemDescription="AA"},
            new Item(){ ItemName="B", ItemDescription="BB"},
            new Item(){ ItemName="C", ItemDescription="CC"},
        };

    }
}

Xaml:

  <ContentPage.BindingContext>
    <localvm:ItemViewModel></localvm:ItemViewModel>
</ContentPage.BindingContext>
<StackLayout>
    <ListView ItemsSource="{Binding Items, Mode=TwoWay}" x:Name="lstView" 
              AbsoluteLayout.LayoutBounds="0,0,1,1" 
              AbsoluteLayout.LayoutFlags="All"
              SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" RowHeight="50">

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="0,0,8,0" Margin="4,0,4,0" xct:TouchEffect.LongPressCommand="{Binding Path=BindingContext.LongPressItemCommand, Source={x:Reference Name=lstView}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <Label Text="{Binding ItemName}" TextColor="Black" Grid.Column="0" FontSize="Medium"></Label>

                        <Label Text="{Binding ItemDescription}" Grid.Column="1" VerticalTextAlignment="End"/>

                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
   
</StackLayout>

Code behind:

 MessagingCenter.Subscribe<ItemViewModel, Guid>(this, "PopupMenuItemMsg",
           (page, itemId) =>
           {
               Main_PopupMenu(itemId);
           });
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • Hey Wendy, thank you so much for replying. The thing is, if I do ```MainPage = new NavigationPage(new ItemPage());``` in ```App.cs``` then everything working fine. The problem is when i change the main page to ```MainPage = new NavigationPage(new GroupPage());``` then is not working :( – Noah Jun 03 '21 at 13:06
  • I realized when I do ```MainPage = new NavigationPage(new TabbedMenu());``` in the App.cs and add `````` in the xaml TabbedPage. everything is working good, but then I have another Icon (Tab) which I can't get rid of :( – Noah Jun 03 '21 at 17:10