3

我有 5 个表单开关,其中一个是全选,应该选择所有其他 switesh 为真。I have a logic when after select all switch is on it cannot be switch back to off manually. 可以关闭按钮(全选)的唯一方法是将另一个按钮更改为假/关闭。

如果我的其他 4 个按钮为真,我已经用逻辑从视图模型处理它我想让用户通过设置逻辑将按钮全选设置为假。但是如果用户可以强制更改并按住开关到 IOS,则使用 IOS假几秒钟按钮停止在假。我尝试过的行为也给了我相同的结果,即使我可以禁用按钮并启用按钮也很好,但根据 xamarine 文档,无法添加命令

 bool _selectAll;

        public bool SelectAll
        {
            get
            {
                return _selectAll;
            }
            set
            {
                SetProperty(ref _selectAll, value);
                if (_activeAll && !(_button1 && _button2 && _button3 && _button4))
                {

                    Button1= true;
                    Button2= = true;
                    Button3= = true;
                    Button4= = true;
                }

                if (!_selectAll && (_emailStatus && _textStatus && _callStatus && _postStatus))
                {
                    SelectAll= true;
                }


            }
        }

4

2 回答 2

3

我使用从您的请求中可以理解的内容创建了一个快速示例。

SelectAll 会将所有 4 个选项都切换为真,但用户将无法切换全选开关。

我通过禁用SelectAll Switch 并仅在选项之一设置为false时启用它来完成上述操作。为此,我只是绑定了SelectAll属性的反转值。为了反转我使用的值 aConverter但它也可以使用来自的另一个属性ViewModel

检查这是否适合您。

在此处输入图像描述

代码:

视图模型

public class MainViewModel : ViewModelBase
{
    private bool option1;
    public bool Option1
    {
        get => option1;
        set
        {
            if (!value) SelectAll = false;
            SetProperty(ref option1, value, nameof(Option1));
        }
    }

    private bool option2;
    public bool Option2
    {
        get => option2;
        set
        {
            if (!value) SelectAll = false;
            SetProperty(ref option2, value, nameof(Option2));
        }
    }

    private bool option3;
    public bool Option3
    {
        get => option3;
        set
        {
            if (!value) SelectAll = false;
            SetProperty(ref option3, value, nameof(Option3));
        }
    }

    private bool option4;
    public bool Option4
    {
        get => option4;
        set
        {
            if (!value) SelectAll = false;
            SetProperty(ref option4, value, nameof(Option4));
        }
    }

    private bool selectAll;
    public bool SelectAll
    {
        get => selectAll;
        set
        {
            SetProperty(ref selectAll, value, nameof(SelectAll));

            if (value)
            {
                Option1 = true;
                Option2 = true;
                Option3 = true;
                Option4 = true;
            }
        }
    }

  ....

}

XAML

<StackLayout Orientation="Vertical"
                Padding="20,0"
                Spacing="10"
                VerticalOptions="CenterAndExpand">

    <Switch HorizontalOptions="FillAndExpand"
            IsToggled="{Binding Option1}" />

    <Switch HorizontalOptions="FillAndExpand"
            IsToggled="{Binding Option2}" />

    <Switch HorizontalOptions="FillAndExpand"
            IsToggled="{Binding Option3}" />

    <Switch HorizontalOptions="FillAndExpand"
            IsToggled="{Binding Option4}" />

    <StackLayout Orientation="Horizontal"
                    HorizontalOptions="FillAndExpand">                         
        <Switch HorizontalOptions="Start"
                IsEnabled="{Binding SelectAll, Converter={StaticResource Invert}}"
                IsToggled="{Binding SelectAll}"
                VerticalOptions="CenterAndExpand"/>
        <Label Text="Select All"
                VerticalOptions="CenterAndExpand" />
    </StackLayout>
</StackLayout>

但在 XAML 中,您还需要:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:InvertBooleanConverter x:Key="Invert" />
    </ResourceDictionary>
</ContentPage.Resources>

这是转换器注册。

和转换器代码:

public class InvertBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;
    }
}

抱歉,我将其Converter引入了解决方案,但我相信这样更干净。可以在此处查看有关 Xamarin Forms 转换器的更多信息。

希望这可以帮助。-

于 2019-09-08T20:09:51.927 回答
2

您可以按如下方式创建模型:

public class ViewModel : INotifyPropertyChanged
{

    private bool swithOne;
    public bool SwithOne
    {
        set
        {
            if (swithOne != value)
            {
                swithOne = value;
                OnPropertyChanged("SwithOne");
            }
        }
        get
        {
            return swithOne;
        }
    }

    private bool swithTwo;
    public bool SwithTwo
    {
        set
        {
            if (swithTwo != value)
            {
                swithTwo = value;
                OnPropertyChanged("SwithTwo");
            }
        }
        get
        {
            return swithTwo;
        }
    }

    private bool swithThree;
    public bool SwithThree
    {
        set
        {
            if (swithThree != value)
            {
                swithThree = value;
                OnPropertyChanged("SwithThree");
            }
        }
        get
        {
            return swithThree;
        }
    }

    private bool swithFour;
    public bool SwithFour
    {
        set
        {
            if (swithFour != value)
            {
                swithFour = value;
                OnPropertyChanged("SwithFour");
            }
        }
        get
        {
            return swithFour;
        }
    }

    private bool swithFive;
    public bool SwithFive
    {
        set
        {
            if (swithFive != value)
            {
                swithFive = value;
                OnPropertyChanged("SwithFive");
                if(value == true)
                {
                    swithOne = true;
                    swithTwo = true;
                    swithThree = true;
                    swithFour = true;
                    OnPropertyChanged("SwithOne");
                    OnPropertyChanged("SwithTwo");
                    OnPropertyChanged("SwithThree");
                    OnPropertyChanged("SwithFour");
                }
                else
                {
                    swithOne = false;
                    swithTwo = false;
                    swithThree = false;
                    swithFour = false;
                    OnPropertyChanged("SwithOne");
                    OnPropertyChanged("SwithTwo");
                    OnPropertyChanged("SwithThree");
                    OnPropertyChanged("SwithFour");
                }
            }
        }
        get
        {
            return swithFive;
        }
    }

    public ViewModel()
    {
        swithOne = false;
        swithTwo = false;
        swithThree = false;
        swithFour = false;
        swithFive = false;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后在XAML中添加Switch

<StackLayout Padding="50">
    <!-- Place new controls here -->
    <Label Text="Welcome to Xamarin.Forms!" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />

    <Switch IsToggled="{Binding SwithOne}" HorizontalOptions="Center"/>
    <Switch IsToggled="{Binding SwithTwo}" HorizontalOptions="Center"/>
    <Switch IsToggled="{Binding SwithThree}" HorizontalOptions="Center"/>
    <Switch IsToggled="{Binding SwithFour}" HorizontalOptions="Center"/>
    <StackLayout Orientation="Vertical">
        <Label Text="Select All" HorizontalOptions="CenterAndExpand"/>
        <Switch IsToggled="{Binding SwithFive}" HorizontalOptions="Center"/>
    </StackLayout>

</StackLayout>

最后,在ContentPage绑定模型中:

BindingContext = new ViewModel();

这是效果:

在此处输入图像描述

于 2019-09-09T08:43:04.853 回答