Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch.Toggled Triggering & Binding Problems #22137

Open
antonycartwright opened this issue Apr 30, 2024 · 1 comment
Open

Switch.Toggled Triggering & Binding Problems #22137

antonycartwright opened this issue Apr 30, 2024 · 1 comment
Labels
area-controls-switch Switch t/bug Something isn't working

Comments

@antonycartwright
Copy link

Description

Hello all,

I'm unfortunately finding so many simple problems with Maui. I'm not really sure how it's come this far without other people finding these problems. Maybe I'm doing something wrong. Anyway, here's another problem:

A bound switch control triggers the Toggled event on load, on manual input and on page closing. This is to be expected as it triggers whenever the toggled value changes, however, there's no way to determine whether the input is user-initiated.

You could have something like this, but it doesn't fully work:

` Switch sw = (Switch)sender;

    Word w = (Word)((Microsoft.Maui.Controls.Switch)sender).BindingContext;

    if (w != null)
    {
        //The page is not closing, it's either loading or loaded.

        if (!_loaded)
        {
            //Page is loading up, so don't attempt any updates.
        }
        else
        {
            //The page has loaded up, but it may still be populating the switches.
            //For the first x seconds, it's best not to update anything.
            int t = Database.Connection.Update(w);
        }
    }
    else
    {
        //If it's null, then the object has been disposed of (the page is closing), so don't attempt any updates.
    }`

The problem with this is that the listview is loaded before all the switches are populated with thei togged state values.

Basically, how do we know when the switch controls have been loaded with the initial values?

  1. The switch values are filled with their initial values. Each time the toggled event is triggered.
  2. A switch value is modified by a human. Each time the toggled event is triggered.
  3. The switch values are emptied on close. Each time the toggled event is triggered.

How are we supposed to deal with this?

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.0-preview.3.10457

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@antonycartwright antonycartwright added the t/bug Something isn't working label Apr 30, 2024
@antonycartwright
Copy link
Author

A working fix/workaround for this is as follows:

Use the Appearing/Disappearing events of the ViewCell to attach/remove the Toggled event from the Switch.

:-)

@dotnet-policy-service dotnet-policy-service bot added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Apr 30, 2024
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-switch Switch t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants