question

StephenH-0383 avatar image
0 Votes"
StephenH-0383 asked RobCaplan commented

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

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


[1]: https://github.com/seattlesteve1999/MileageTest+

dotnet-csharpdotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You mean the listview in the bottom of Mainpage and its background color is DarkGoldenrod in your sample(in the code snippet it's purple), right? Would you mind sharing the AutoData in MainMenuViewModel with me? I make some faker data and test on my iPhone, the SelectedItem event can be fired, I can't reproduce your issue. In addition, DisplayAlerts method is in UpdateCarsViewModel not MainMenuViewModel, I also test the UpdateCars page, it still could pop an alert to delete the item. Would you mind sharing more details?

 public void GetAutoData() {
  for (int i = 0; i < 10; i++)     {......
                 MyList.Add(new AutoWithSwitch
    
                 {
                     Name = i.ToString(),
    
                 });
             }
     ......    }
0 Votes 0 ·
StephenH-0383 avatar image
0 Votes"
StephenH-0383 answered RobCaplan commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This appears to be clarification to your question rather than an answer. Can you please edit it into your initial question so it doesn't confuse others who run into similar problems?

Please see: When to use answers or comments

0 Votes 0 ·
StephenH-0383 avatar image
0 Votes"
StephenH-0383 answered WenyanZhang-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm so glad you solved this issue, thanks for your sharing.

0 Votes 0 ·