question

bilelmiled-6268 avatar image
0 Votes"
bilelmiled-6268 asked JarvanZhang-MSFT answered

i want by a switch lunch two functions in xamarin

Hi , i want to start two functions by the switch ,
i mean there are two function : startservice() ans stopservice(),
and when it's on startservice is started and when it's off stopservice is started.

 <Switch IsToggled="true"
                 OnColor="#B6E13D"
                 ThumbColor="WhiteSmoke"
                 Scale="0.9"
                 Margin="5"
            />
dotnet-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.

I'm wondering if you're familiar with event handlers in the codebehind? Start here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/switch

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

when it's on startservice is started and when it's off stopservice is started

Switch provides the Toggled which will be raised when the toggle state of the switch is changed. Try to detect the event to check the status to start or stop the service.

//page.xaml
<Switch Toggled="Switch_Toggled"/>

//the switch's toggled event
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
    var isToggled = e.Value; //the bool value to which the toggle control was toggled

    if (isToggled)
    {
        //switch is toggled, start the service
    }
    else
    {
        //switch isn't toggled, stop the service
    }
}

Tutorial: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/switch#respond-to-a-switch-state-change


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.