i want by a switch lunch two functions in xamarin

bilel miled 21 Reputation points
2021-03-18T14:54:51.377+00:00

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-03-19T02:44:56.14+00:00

    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://learn.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.

    0 comments No comments