【问题标题】:Xamarin Forms Switch XAMLXamarin 表单切换 XAML
【发布时间】:2023-03-04 12:21:01
【问题描述】:

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

其中一个组件是一个开关,它本身可以正常工作,但我想将基本文本“非活动/活动”更改为“男性/女性”

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

有什么想法吗?

谢谢!

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    已多次询问缺少内置开关选项,或者至少无法重命名开关选项。

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

    这个switch 是两个水平排列的按钮,分别是YesNo。选中的按钮有一个红色边框,未选中的有一个透明边框。

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

    【讨论】:

    • 当我尝试共同使用您的自定义开关时出现错误,似乎在当前上下文中找不到 RecoveryConnect_XamForms、AppStyling 和类,知道为什么吗?
    • 它特定于我从中提取的应用程序。您需要自己的枚举来设置/确定选择状态和边框颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2017-12-14
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多