首页 文章

Xamarin Forms Switch XAML

提问于
浏览
1

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

这些组件中的一个是Switch,它本身可以正常工作,但我想通过“男/女”更改基本文本“不活动/活动”

我已经看到在Xaml for windows phone中有一个带有On / OffContent属性的ToggleSwitch组件,但我似乎无法在XAML中为Xamarin Forms找到一个等价物

任何的想法 ?

谢谢!

1 回答

  • 0

    有人曾多次询问缺少内置开关选项或至少缺乏重命名swtich选项 . 您可以使用自定义渲染,修改操作系统级别的文本,或者像我选择只构建自己的开关一样 . 此开关是两个水平布局的按钮,文本是和否 . 所选按钮获得红色边框,未选择透明 .

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

相关问题