【问题标题】:Enable and disable Xamarin Forms Switch by MVVM通过 MVVM 启用和禁用 Xamarin Forms Switch
【发布时间】:2019-09-08 10:26:28
【问题描述】:

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

如果我的其他 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;
                }


            }
        }

【问题讨论】:

  • 嗨,你爱过吗?如果答案有效记得标记它,否则继续表示怀疑。

标签: xamarin xamarin.forms xamarin.ios xamarin.ios-binding


【解决方案1】:

我根据您的要求创建了一个快速示例。

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

我通过禁用 SelectAll 开关并仅在其中一个选项设置为 false 时启用它来完成上述操作。为此,我只是绑定了 SelectAll 属性的反转值。为了反转值,我使用了Converter,但也可以使用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 转换器的更多信息,请参阅here

希望这会有所帮助。-

【讨论】:

  • 感谢在我的场景中,用户不应该一次全部设为假。理论上,虽然用户将全选设置为全部,但用户不应该将其设为假(在一次)
  • @Black 在上面的代码中,用户不能一次全部false。选择 All-The-Options 时,SelectAll 按钮将被禁用。
【解决方案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();

效果如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2017-12-04
    • 2017-08-13
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多