xamarin/Xamarin.Forms

XAML Switch Toggled event behaviour OnAppearing and Toggled not making sense.

Open

#9,099 创建于 2020年1月6日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)C# (5,644 star) (1,926 fork)batch import
a/Xaml </>a/listviewfeedback-tickethelp wantedinactivep/Androidt/bug :bug:up-for-grabs

描述

This issue has been moved from a ticket on Developer Community.


Recreating the Issue

I found this issue in 2019.4.0 and it persists in 2019.4.2.

  1. Using VS2019 create a new Mobile App Xamarin forms -- Master Detail
  2. Add "public bool Selected { get; set; }" to Models/Item.cs
  3. Edit Services/MockDataStore.cs 'Items' to include the new field "Selected" alternating with true and false values;
  4. Replace the ViewCell in ItemsPage.xaml with the following ViewCell
  1. Generate the Toggled="Switch_Toggled" event and add the following code to the new event:

private void Switch_Toggled(object sender, ToggledEventArgs e) { ViewCell cell = (sender as Switch). Parent.Parent as ViewCell; var model = cell. BindingContext as Item; if (model != null) { var s =$"Switch {model. Text} is now {e.Value}"; } }

  1. put a breakpoint on the closing parenthesis of the if statement, run and observe 's' at each break.

You will see that: a) As the form is created the Switched_togged event of fired once for each "Selected=false" and twice for every "Selected=true" value; b) When the form is Updated If a true item is set to false then Switched_togged is fired once If a False value is set to true Switched_togged is fired once for the change and then once again for each item in the list as the form is re-drawn. c) Depeneding on whether the original value was true or false, no matter how many time you change value, if the original value was false then the colour of the switch when true is different than the other way around

Further notes: ** I have tried this with different ways of accessing the changed value: - Binding Toggled to an event - using +=Event to bind an event as the viewmodel is constrcted - Appeding code via the IsToggled - Consuming a XAML Extension In all cases the bound event is processed as described above so the trigger seems to be in the core code

** There does not appear to be a way to descrimiante when the actions is being fired OnCreate/OnAppearing or OnClicked so I can't actually pick up that specific time when the user choses to change the value.


Original Comments

Visual Studio Feedback System on 12/19/2019, 08:32 PM:


Original Solutions

(no solutions)

贡献者指南

XAML Switch Toggled event behaviour OnAppearing and Toggled not making sense. · xamarin/Xamarin.Forms#9099 | Good First Issue