简体   繁体   English

Xamarin Forms Switch XAML

[英]Xamarin Forms Switch XAML

I'm new in Xamarin and i'm trying to create a simple page with some components. 我是Xamarin的新手,我正在尝试创建一个包含一些组件的简单页面。

One of these component is a Switch it works fine by itself but i would like to change the basic text "inactive/active" by "male/female" 这些组件之一是Switch,它本身可以正常工作,但我想将基本文本“ inactive / active”改为“ male / female”

I've seen that in Xaml for windows phone there is a ToggleSwitch Component with a On/OffContent property but i can't seems to find an equivalent in XAML for Xamarin Forms 我已经看到在Windows Phone的Xaml中有一个带有On / OffContent属性的ToggleSwitch组件,但是我似乎找不到Xamarin Forms的XAML等效项。

any idea ? 任何想法 ?

Thank you! 谢谢!

The lack of built in switch options, or at least the lack of being able to rename the switch options, has been asked a few times. 有人问了缺少内置的开关选项,或者至少是不能重命名开关选项。

You could go with custom renders, modify the text at the OS level or do like I chose to do, just build your own switch. 您可以使用自定义渲染,在操作系统级别修改文本,或者像我选择的那样做,只需构建自己的开关即可。

This switch is two buttons laid out horizontally with the text Yes and No . switch是水平排列的两个按钮,文本为“ 是”和“ 否” The selected button gets a red border, and the unselected a transparent border. 选中的按钮显示红色边框,而未选择的显示透明边框。

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM