Xamarin 表单切换 XAML [英] Xamarin Forms Switch XAML

查看:38
本文介绍了Xamarin 表单切换 XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new in Xamarin and i'm trying to create a simple page with some components.

其中一个组件是一个 Switch,它本身可以正常工作,但我想将基本文本inactive/active"更改为male/female"

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"

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

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

有什么想法吗?

谢谢!

推荐答案

缺少内置开关选项,或者至少缺少能够重命名开关选项的问题,已经被问过几次了.

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.

这个switch 是两个水平排列的按钮,文本为YesNo.选中的按钮有一个红色边框,未选中的按钮有一个透明边框.

This switch is two buttons laid out horizontally with the text Yes and No. 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();
    }

}

这篇关于Xamarin 表单切换 XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆