Xamarin Forms Switch XAML

用户名

我是Xamarin的新手,我正在尝试创建一个包含一些组件的简单页面。

这些组件之一是一个Switch,它本身可以很好地工作,但是我想通过“ male / female”将基本文本“ inactive / active”更改为

我已经看到在Windows Phone的Xaml中有一个带有On / OffContent属性的ToggleSwitch组件,但是我似乎找不到Xamarin Forms的XAML中的等效项。

任何的想法 ?

谢谢!

克林特

有人问过缺少内置的开关选项,或者至少是不能重命名开关选项。

您可以使用自定义渲染,在OS级别上修改文本,或者像我选择的那样进行操作,只需构建自己的开关即可。

switch是两个水平放置的按钮,文本为“是”和“否”选定的按钮显示红色边框,而未选择的显示透明边框。

class CustomSwitch : Grid
{

    public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
    private Button negative;
    private Button positive;

    public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create<CustomSwitch, Object>(t => t.SelectedItem, null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);

    public CustomSwitch()
    {

        try
        {
            this.HorizontalOptions = LayoutOptions.Center;
            this.VerticalOptions = LayoutOptions.Center;

            negative = new Button();
            negative.Text = "No";
            negative.Style = <YourNameSpace>.AppStyling.Style_Button_Switch;
            negative.Clicked += (o,s) => OnSelectedItemChanged(this, ItemSelected, (int)Classes.Collections.Enums.SelectionStatus.False);

            positive = new Button();
            positive.Text = "Yes";
            positive.Style = <YourNameSpace>.AppStyling.Style_Button_Switch;
            positive.Clicked += (o, s) => OnSelectedItemChanged(this, ItemSelected, (int)Classes.Collections.Enums.SelectionStatus.True);               

            this.Children.Add(negative, 0,0);
            this.Children.Add(positive, 1,0);
        }
        catch(System.Exception ex)
        {
            <YourNameSpace>.Classes.Helpers.Helper_ErrorHandling.SendErrorToServer(ex, this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }

    }

    public Object SelectedItem
    {
        get
        {
            return base.GetValue(SelectedItemProperty);
        }
        set
        {
            if (SelectedItem != value)
            {
                base.SetValue(SelectedItemProperty, value);
                InternalUpdateSelected();
            }
        }
    }

    private void InternalUpdateSelected()
    {
        if((int)SelectedItem == (int)Classes.Collections.Enums.SelectionStatus.False)
        {
            negative.BorderColor = <YourNameSpace>.AppStyling.Color_Selected;
            positive.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
            positive.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
        }
        else if ((int)SelectedItem == (int)Classes.Collections.Enums.SelectionStatus.True)
        {
            negative.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
            negative.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
            positive.BorderColor = <YourNameSpace>.AppStyling.Color_Selected;
        }
        else
        {
            negative.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
            negative.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
            positive.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
            positive.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
        }
    }

    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        CustomSwitch boundSwitch = (CustomSwitch)bindable;

        if((int)newValue != (int)Classes.Collections.Enums.SelectionStatus.Unselected)
        {
            boundSwitch.SelectedItem = (int)newValue == (int)Classes.Collections.Enums.SelectionStatus.False ? (int)Classes.Collections.Enums.SelectionStatus.False : (int)Classes.Collections.Enums.SelectionStatus.True;
        }


        if (boundSwitch.ItemSelected != null)
        {
            boundSwitch.ItemSelected(boundSwitch, new SelectedItemChangedEventArgs(newValue));
        }
        boundSwitch.InternalUpdateSelected();
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章