gpt4 book ai didi

xamarin - 在 C# 后端中,如何选择分段控件中的元素?

转载 作者:行者123 更新时间:2023-12-02 17:54:58 25 4
gpt4 key购买 nike

我正在使用自定义控件来创建显示如下的连接按钮:

********************************************
* * * *
* One * Two * Three *
* * * *
********************************************

当我单击按钮时,会触发一个事件:

void OnValueChanged(object sender, EventArgs e)
{
switch (segControl.SelectedValue)
{

这运行良好,但我想做的是能够从我的 C# 代码中选择这些按钮之一。

当我查看控件的源代码时,我不知道如何执行此操作。

如果有人对我应该做什么有任何建议,例如以与我单击它相同的方式选择第二个按钮,我将不胜感激。

这是我正在使用的代码:

<local:SegmentedControl ValueChanged="OnValueChanged" SelectedSegment="{Binding CustomPointsSwitch}" x:Name="segControl" HorizontalOptions="End">
<local:SegmentedControl.Children>
<local:SegmentedControlOption Text="Two" />
<local:SegmentedControlOption Text="Four" />
</local:SegmentedControl.Children>
</local:SegmentedControl>

共享代码

public class SegmentedControl : View, IViewContainer<SegmentedControlOption>
{
public IList<SegmentedControlOption> Children { get; set; }

public SegmentedControl()
{
Children = new List<SegmentedControlOption>();
}

public event ValueChangedEventHandler ValueChanged;

public delegate void ValueChangedEventHandler(object sender, EventArgs e);

private string selectedValue;

public string SelectedValue
{
get { return selectedValue; }
set
{
selectedValue = value;
if (ValueChanged != null)
ValueChanged(this, EventArgs.Empty);
}
}
public static readonly BindableProperty SelectedSegmentProperty = BindableProperty.Create("SelectedSegment", typeof(int), typeof(SegmentedControl), 0);
public int SelectedSegment
{
get
{
return (int)GetValue(SelectedSegmentProperty);
}
set
{
SetValue(SelectedSegmentProperty, value);
}
}
}

public class SegmentedControlOption : View
{
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SegmentedControlOption), string.Empty);

public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}

iOS 渲染器

public class SegmentedControlRenderer : ViewRenderer<SegmentedControl, UISegmentedControl>
{
public SegmentedControlRenderer()
{
}

protected override void OnElementChanged(ElementChangedEventArgs<SegmentedControl> e)
{
base.OnElementChanged(e);

var segmentedControl = new UISegmentedControl();
for (var i = 0; i < e.NewElement.Children.Count; i++)
{
segmentedControl.InsertSegment(e.NewElement.Children[i].Text, i, false);
}

segmentedControl.ValueChanged += (sender, eventArgs) => {
e.NewElement.SelectedSegment = (int)segmentedControl.SelectedSegment;
e.NewElement.SelectedValue = segmentedControl.TitleAt(segmentedControl.SelectedSegment);
};

SetNativeControl(segmentedControl);
}
}

Android 渲染器

public class SegmentedControlRenderer : ViewRenderer<SegmentedControl, RadioGroup>
{

protected override void OnElementChanged(ElementChangedEventArgs<SegmentedControl> e)
{
base.OnElementChanged(e);

var layoutInflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);

var g = new RadioGroup(Context);
g.Orientation = Orientation.Horizontal;
g.CheckedChange += (sender, eventArgs) => {
var rg = (RadioGroup)sender;
if (rg.CheckedRadioButtonId != -1)
{
var id = rg.CheckedRadioButtonId;
var radioButton = rg.FindViewById(id);
var radioId = rg.IndexOfChild(radioButton);
var btn = (RadioButton)rg.GetChildAt(radioId);
var selection = (String)btn.Text;
e.NewElement.SelectedValue = selection;
}
};

for (var i = 0; i < e.NewElement.Children.Count; i++)
{
var o = e.NewElement.Children[i];
var v = (SegmentedControlButton)layoutInflater.Inflate(Resource.Layout.SegmentedControl, null);
v.Text = o.Text;
if (i == 0)
v.SetBackgroundResource(Resource.Drawable.segmented_control_first_background);
else if (i == e.NewElement.Children.Count - 1)
v.SetBackgroundResource(Resource.Drawable.segmented_control_last_background);
g.AddView(v);
}

SetNativeControl(g);
}
}

public class SegmentedControlButton : RadioButton
{
private int lineHeightSelected;
private int lineHeightUnselected;
private Paint linePaint;

public SegmentedControlButton(Context context, IAttributeSet attributes) : this(context, attributes, Resource.Attribute.segmentedControlOptionStyle)
{
}

public SegmentedControlButton(Context context, IAttributeSet attributes, int defStyle) : base(context, attributes, defStyle)
{
Initialize(attributes, defStyle);
}

private void Initialize(IAttributeSet attributes, int defStyle)
{
var a = this.Context.ObtainStyledAttributes(attributes, Resource.Styleable.SegmentedControlOption, defStyle, Resource.Style.SegmentedControlOption);

var lineColor = a.GetColor(Resource.Styleable.SegmentedControlOption_lineColor, 0);
linePaint = new Paint();
linePaint.Color = lineColor;

lineHeightUnselected = a.GetDimensionPixelSize(Resource.Styleable.SegmentedControlOption_lineHeightUnselected, 0);
lineHeightSelected = a.GetDimensionPixelSize(Resource.Styleable.SegmentedControlOption_lineHeightSelected, 0);

a.Recycle();
}

protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);

if (linePaint.Color != 0 && (lineHeightSelected > 0 || lineHeightUnselected > 0))
{
var lineHeight = Checked ? lineHeightSelected : lineHeightUnselected;

if (lineHeight > 0)
{
var rect = new Rect(0, Height - lineHeight, Width, Height);
canvas.DrawRect(rect, linePaint);
}
}
}
}

如果有任何有关如何进行更改的建议,我将不胜感激。还有人知道 Xamarin 是否希望将其制作成表单组件吗?

最佳答案

为什么不在共享 xaml 中设置边距?

<local:SegmentedControl ValueChanged="OnValueChanged" SelectedSegment="{Binding CustomPointsSwitch}" x:Name="segControl" HorizontalOptions="End">
<local:SegmentedControl.Children>
<local:SegmentedControlOption Text="Two" Margin="8,0,8,0" />
<local:SegmentedControlOption Text="Four" Margin="8,0,8,0" />
</local:SegmentedControl.Children>
</local:SegmentedControl>

关于xamarin - 在 C# 后端中,如何选择分段控件中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47132181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com