Xamarin forms app, SelectedItem event will not fire when pressing on a line, thus my alerts are not happening

Stephen H 1 Reputation point
2022-04-21T13:10:39.037+00:00

No matter what I do, I cannot get the SelectedItem event to fire.

Here is my view

 <ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="27"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="60"/>
                                    <ColumnDefinition Width="5"/>
                                    <ColumnDefinition Width="105"/>
                                    <ColumnDefinition Width="35"/>
                                    <ColumnDefinition Width="60"/>
                                    <ColumnDefinition Width="36"/>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="0" Grid.Row="0" FontSize="Medium" TextColor="White" Text="{Binding Year}" HorizontalTextAlignment="Center"/>
                                <Label Grid.Column="2" Grid.Row="0" FontSize="Medium" TextColor="White" Text="{Binding Name}" Grid.ColumnSpan="2"  HorizontalTextAlignment="Start"/>
                                <Switch x:Name="{Binding Id}" IsToggled="{Binding IsChecked, Mode=TwoWay}" Toggled="Handle_Toggled" Grid.Column="6" Grid.Row="0" />
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

Here is my View Model to process that UI

 public AutoWithSwitch SelectedItem
        {
            get
            {
                Analytics.TrackEvent("UpdateCarViewModel - SelectedItem - get");
                return _selectedItem;
            }
            set
            {
                _selectedItem = value;
                Analytics.TrackEvent("UpdateCarViewModel - SelectedItem - set");
                if (_selectedItem == null)
                    return;
                DisplayAlerts(value);
            }
        }

        public async void DisplayAlerts(AutoWithSwitch value)
        {
            Analytics.TrackEvent("UpdateCarViewModel - DisplayAlerts top");

            Application.Current.Properties["DeleteData"] = value;            
            DependencyService.Get<IMessage>().LongAlert("Stupid");

            Analytics.TrackEvent("UpdateCarViewModel - DisplayAlerts Back From Call");
        }

As you can see, I have alerts in there so I can see the event is not firing. I have also tried ItemSelected and cannot get that one to work either. Any help you could provide would be greatly appreciated. A test version of my code is located here if you want to see some of the code in its entirety. https://github.com/seattlesteve1999/MileageTest

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,289 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,197 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Stephen H 1 Reputation point
    2022-04-22T13:47:51.183+00:00

    I greatly appreciate your response. Keep in mind, I forgot to specify, this all works in the simulator, but does not work at all on my phone, I am referring to the SelectedItem code that is. In other words, I can go into UpdateCar in the simulator, it brings up the data and when I press on a row, it brings up the alert to cancel or delete, but on the iPhone is does not fire the SelectedItem code and I do not see any alert, it just jumps out of the application, I can see what it is doing by my log statements. Are you saying you did not change my code at all and the SelectedItem in the MainMenueViewModel fired and worked?

    So the issue with my code is, I have several places where a ListView is defined with a SelectedItem and it does not fire in ANY of them, so I must be doing something wrong, but don't know what. What I have mentioned on GetHub is a very small sample of my application. When you say share AutoData in MainMenuViewModel, are you asking for data or the definition of the object? If you need the definition of the data, it can be found on the GitHub link I mentioned in my first post.

    As I read closer, it looks like you are saying you got the alert to pop on the UpdateCar code on an iPhone. If so, is there a setup maybe I am missing on my phone that is preventing it from working because it does nothing on my phone but exit the app and return to the calling page on my phone. I do have other apps I have written on my iPhone and those work without issue, but on this app, I am using Xamarin Forms where I was not in those apps, so maybe I am missing something on my phone, I don't know. Any help would be greatly appreciated. Thanks so much and sorry for rambling.


  2. Stephen H 1 Reputation point
    2022-04-24T14:20:48.707+00:00

    I finally got it using ItemSelected. Here is my code that works,

    The View

     <ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" ItemSelected="Selected" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
    

    View Code Behind

     void Selected(object sender, SelectedItemChangedEventArgs e)
            {
                if (Convert.ToBoolean(Application.Current.Properties["FirstSelected"]))
                {
                    Application.Current.Properties["FirstSelected"] = false;
                    //Analytics.TrackEvent("UpdateCar.xaml.cs - Select top");
                    var ucvm = new UpdateCarsViewModel((AutoWithSwitch)e.SelectedItem);
                    //Analytics.TrackEvent("UpdateCar.xaml.cs - Select top2" + (AutoWithSwitch)e.SelectedItem);
    
                    if (e.SelectedItem == null)
                    {                    
                        return;
                    }
    
                    AutoView.SelectedItem = null;                
                }
            }
    

    The View Model

     public UpdateCarsViewModel(AutoWithSwitch data)
            {
                StackTrace stackTrace = new StackTrace();
                // Get calling method name
                var methodName = stackTrace.GetFrame(1).GetMethod().Name;
                Analytics.TrackEvent("In UpdateCarsViewModel MethodName = " + methodName);
                if (methodName == "Selected")
                {
                    DisplayAlerts(data);                
                }
            }
    
    public async void DisplayAlerts(AutoWithSwitch value)
            {           
                var answer = await (App.Current as App).MainPage.DisplayAlert("Action", "Delete", "Ok", "Cancel");
                if (answer)
                    DeleteCar(value);
                else
                    Application.Current.Properties["FirstSelected"] = true;            
            }
    

    Thank you to all for your help