【问题标题】:Picker renderer for iOS Xamarin.Forms?iOS Xamarin.Forms 的选取器渲染器?
【发布时间】:2018-01-27 00:47:31
【问题描述】:

我是 Xamarin.Forms 编程的新手。想要自定义/更改 Picker 的方式

在图片中: - 带有文本“选择”的按钮,单击时调用 picker.Focus() - 选择按钮后面有黑色背景和文本颜色的选择器 - 空列表视图 -The picker options wheel pane, pops up when a picker is 'Focused'

选择器实现代码:

Picker picker = new Picker
{
    HorizontalOptions = LayoutOptions.Start,
    VerticalOptions = LayoutOptions.Center,
    WidthRequest = 73,
    HeightRequest = 25,
    BackgroundColor = Color.Black,
    TextColor = Color.Black
};

List<string> myList = new List<string>();
myList.Add("OPTIONS 1");
myList.Add("OPTIONS 2");
myList.Add("OPTIONS 3");
myList.Add("OPTIONS 4");
myList.Add("OPTIONS 5");

foreach (string str in myList)
{
    picker.Items.Add(str);
}

代码自定义用户单击以将选择器选项轮窗格显示在视图中的“框”,但没有(或至少看起来)提供任何用于自定义选择器窗格的选项。

如何将选择轮弹出窗格放置在 ListView 内? 如何更改选择器轮内选项的字体大小并更改显示选项的窗格的高度?

tl;dr - 如何更改选择器窗格高度、项目字体大小、窗格位置?

这是我所知道的……

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using AppName.iOS;
using AppName;

[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace AppName.iOS
{
    public class MyPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Unsubscribe from event handlers and cleanup any resources
            }

            if (e.NewElement != null)
            {
                // Configure the native control and subscribe to event handlers
            }
        }
    }
}

【问题讨论】:

    标签: c# ios xamarin.forms uipickerview


    【解决方案1】:

    iOS 中名为 UIPickerView 的选择器轮,我们可以在渲染器中获取此控件,如下所示:

    UITextField textField = Control;
    UIPickerView pickerView = textField.InputView as UIPickerView;
    

    然后我们可以使用Delegate 自定义它。

    首先我们应该得到 dataSource 在你的情况下我们可以得到它:

    Picker myPicker = Element;
    itemList = myPicker.Items.ToList();
    

    第二次创建委托并将列表设置为参数:

    pickerView.Delegate = new MyPickerViewDelegate(itemList);
    
    public class MyPickerViewDelegate: UIPickerViewDelegate
    {
    
        List<string> itemList;
    
        public MyPickerViewDelegate(List<string> list)
        {
            itemList = list;
        }
    }
    

    最后我们可以开始自定义以下事件:

    //Define the Font size or style
    public override NSAttributedString GetAttributedTitle(UIPickerView pickerView, nint row, nint component)
    {
        var text = new NSAttributedString(
            itemList[(int)row],
            font: UIFont.SystemFontOfSize(24),
            foregroundColor: UIColor.Red,
            strokeWidth: 4
        );
    
        return text;
    }
    //Define the row height
    public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
    {
        return 80;
    }
    

    另外,如果你想定制更灵活(包括放置),你可以使用以下方法:

    public override UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
    {
        UIView contentView = new UIView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 80));
    
        UILabel label = new UILabel();
        label.Frame = contentView.Bounds;
        contentView.AddSubview(label);
    
        label.Text = itemList[(int)row];
        //Change the label style
        label.Font = UIFont.SystemFontOfSize(24);
        label.TextColor = UIColor.Red;
    
        return contentView;
    }
    

    这是我的自定义渲染器供您参考:

    public class MyPickerRenderer : PickerRenderer
    {
        List<string> itemList;
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
    
            Picker myPicker = Element;
            itemList = myPicker.Items.ToList();
    
            UITextField textField = Control;
            UIPickerView pickerView = textField.InputView as UIPickerView;
            pickerView.Delegate = new MyPickerViewDelegate(itemList);
        }
    }
    

    【讨论】:

    • 我可以按照你的类实现,但我是 Xamarin.Forms 的新手,我不明白前五行代码的去向。对 textField 的“控制”分配,控制分配仅在函数中有效吗? (在原始帖子的“OnElementChanged”方法中可以看到控制)。 Picker = Element 肯定是在引用我自己的选择器分配?如果是,那么我仍然不明白如何使用 Control。
    • @jtth 你可以把它们放在OnElementChanged(ElementChangedEventArgs&lt;Picker&gt; e)
    • @jtth 我已经更新了我的答案,发布我的渲染器。请看上面。
    • @jtth 很高兴它帮助了你:)。您可以修改标签的框架以更改文本应该出现的位置。
    • 是的,谢谢,我现在有我想要的文字,但我希望这也能控制轮子窗格弹出窗口的位置和/或能够调整其高度。这个渲染器可以做到吗?
    【解决方案2】:

    您必须创建自己的UiPickerView 实现。这将允许您使用this guide 或这个设置字体大小。至于高度和位置;看来你可以set the placement 但是高度有点困难,我只能在objective C 或swift 中找到示例。

    祝你好运!

    【讨论】:

    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多