Xamarin Forms Switch切换块ui [英] Xamarin Forms Switch toggles block ui

查看:406
本文介绍了Xamarin Forms Switch切换块ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试实现两个链接的 Switch 元素. 当一个切换时,另一个则不切换.

I currently trying to implement two linked Switch element. When one is toggled the other is untoggled.

但是当我的事件被触发时,点击的切换未完成会被切换.

But when my event is triggered, the clicked Switch do not finish is toggled.

XAML:

<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                    <Switch x:Name="WomanGenderSwitch" IsToggled="true"  Toggled="HandleWomanToggled" Scale="0.5">
                    </Switch>
                    <Label FontSize="12" x:Name="WomanSwitchLabel" TextColor="#ddd" VerticalTextAlignment="Center">
                    </Label>
                    <Switch x:Name="ManGenderSwitch" Toggled="HandleManToggled" Scale="0.5">
                    </Switch>
                    <Label FontSize="12" x:Name="ManSwicthLabel" TextColor="#ddd" VerticalTextAlignment="Center">
                    </Label>
                </StackLayout>

背后的代码:

void HandleManToggled(object sender, Xamarin.Forms.ToggledEventArgs e)
        {
            var s = sender as Switch;
            if (s.IsToggled)
                WomanGenderSwitch.IsToggled = false;
            else
                WomanGenderSwitch.IsToggled = false;
        }



  void HandleWomanToggled(object sender, Xamarin.Forms.ToggledEventArgs e)
    {
        var s = sender as Switch;
        if (s.IsToggled)
            ManGenderSwitch.IsToggled = false;
        else
            ManGenderSwitch.IsToggled = false;
    }

推荐答案

您可以为两个Switch重新使用相同的处理程序,并且由于切换另一个Switch将导致其处理程序被调用,因此您需要禁用它,否则最终出现无休止的事件波动:

You can re-use the same Handler for both Switches and since toggling the other Switch will cause its handler to be invoked you need to disable it otherwise you end up with a non-ending event ripple:

<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
    <Switch x:Name="WomanGenderSwitch" IsToggled="true"  Toggled="HandleToggled" Scale="0.5" />
    <Label FontSize="12" x:Name="WomanSwitchLabel" TextColor="#ddd" VerticalTextAlignment="Center" />
    <Switch x:Name="ManGenderSwitch" Toggled="HandleToggled" Scale="0.5" />
    <Label FontSize="12" x:Name="ManSwicthLabel" TextColor="#ddd" VerticalTextAlignment="Center" />
</StackLayout>

删除/重新添加处理程序以中断事件链:

void HandleToggled(object sender, Xamarin.Forms.ToggledEventArgs e)
{
    var aSwitch = ((sender == WomanGenderSwitch) ? ManGenderSwitch : WomanGenderSwitch);
    aSwitch.Toggled -= HandleToggled;
    aSwitch.IsToggled = !aSwitch.IsToggled;
    aSwitch.Toggled += HandleToggled;
}

或使用标记来中断事件链:

bool busy;
void HandleToggled(object sender, Xamarin.Forms.ToggledEventArgs e)
{
    if (busy == true) return;
    busy = true;
    var aSwitch = ((sender == WomanGenderSwitch) ? ManGenderSwitch : WomanGenderSwitch);
    aSwitch.IsToggled = !aSwitch.IsToggled;
    busy = false;
}

这篇关于Xamarin Forms Switch切换块ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆