在 xamarin 中创建自定义 iOS 控件

sparksy64

我在 Xamarin 项目中创建了一个自定义 iOS 控件。但是,当它添加到 Xamarin.Forms 页面上的 XAML 时,它当前不会绘制到页面上。

<OnPlatform.iOS>
    <iOSControls:CellContent BindingContext="{Binding}" CellData="{Binding VM.CellCollection}"/>
</OnPlatform.iOS>

我也不确定这是否是将数据从 ViewModel 绑定到控件的正确方法,并且由于它没有加载我无法对此进行测试。

CellContent 类如下

   `Public partial class CellContent : UIView
    {

    #region [ Private Fields ]

    /// <summary>
    /// The cell data model used for generating each cell
    /// </summary>
    private ICell cellData;

    #endregion

    #region [ Constructor ]

    /// <summary>
    /// Initializes a new instance of the <see cref="CellContent" /> class.
    /// </summary>
    /// <param name="handle">handle pointer passed to the base class</param>
    public CellContent(IntPtr handle) : base(handle)
    {
    }

    #endregion

    #region [ Public Properties ]

    /// <summary>
    /// Gets or sets the cell data model
    /// </summary>
    [Export("CellData"), Browsable(true)]
    public ICell CellData
    {
        get
        {
            return this.cellData;
        }
        set
        {
            this.cellData = value;
        }
    }

    #endregion

    #region [ UIView Events ]

    /// <summary>
    /// Static control generator
    /// </summary>
    /// <returns>A control instance</returns>
    public static CellContent Create()
    {
        var arr = NSBundle.MainBundle.LoadNib("CellContent", null, null);
        var v = Runtime.GetNSObject<CellContent>(arr.ValueAt(0));

        return v;
    }

    /// <summary>
    /// Initialises the sub controls
    /// </summary>
    public override void AwakeFromNib()
    {
        base.AwakeFromNib();

        HorizontalGridLines horizontalRows = HorizontalGridLines.Create();
        VerticalGridLines verticalRows = VerticalGridLines.Create();
        PlottedActivity plottedActivity = PlottedActivity.Create();
        horizontalRows.VM = this.CellData;
        verticalRows.VM = this.CellData;
        plottedActivity.VM = this.CellData;

        this.AddSubview(horizontalRows);
        this.AddSubview(verticalRows);
        this.AddSubview(plottedActivity);
    }

    #endregion`

水平/垂直网格线和绘制的活动文件都基本相同,我已经验证它们在单独的项目中工作(没有绑定或从 XAML 实例化),但 Horizo​​ntalGridLines 文件也可以在下面看到以供参考

`public partial class HorizontalGridLines : UIView
{
    /// <summary>
    /// Initializes a new instance of the <see cref="HorizontalGridLines" /> class.
    /// </summary>
    /// <param name="handle">handle pointer passed to the base class</param>
    public HorizontalGridLines(IntPtr handle) : base(handle)
    {
    }

    /// <summary>
    /// Gets or sets the data context cast as an interface for binding
    /// </summary>
    [Export("CellData"), Browsable(true)]
    public ICell CellData{ get; set; }

    /// <summary>
    /// Static control generator
    /// </summary>
    /// <returns>A control instance</returns>
    public static HorizontalGridLines Create()
    {
        var arr = NSBundle.MainBundle.LoadNib("HorizontalGridLines", null, null);
        var v = Runtime.GetNSObject<HorizontalGridLines>(arr.ValueAt(0));

        return v;
    }

    /// <summary>
    /// Drawing override
    /// </summary>
    /// <param name="rect">The content bounds</param>
    public override void Draw(CGRect rect)
    {
        base.Draw(rect);
        int numOfLines = this.CellData.ActivityRows.Count;

        var context = UIGraphics.GetCurrentContext();

        context.SetLineWidth(2);
        context.SetLineDash(0, new nfloat[] { 3, 4 });
        UIColor.Black.SetStroke();

        int y = 0;

        int width = (int)this.Frame.Width;
        int height = (int)this.Frame.Height;
        int heightPerLine = y = height / numOfLines;

        for (int i = 0; i < numOfLines - 1; i++)
        {
            var path = new CGPath();
            CGPoint[] lines = new CGPoint[2];
            lines[0] = new PointF(0, y);
            lines[1] = new PointF(width, y);
            y += heightPerLine;
            path.AddLines(lines);
            path.CloseSubpath();
            context.AddPath(path);
        }

        context.DrawPath(CGPathDrawingMode.FillStroke);
    }
}`

任何帮助将不胜感激,谢谢!:)

sparksy64

所以我发现了这样做的方法:

  • 在您创建的自定义控件上,创建一个您要将数据分配到的公共属性。
  • 在 XAML 上,将{Binding .}分配给您刚刚创建的公共属性,前提是它们的类型相同。
  • LayoutSubviews()方法期间/之后,可以从自定义控件中访问数据

例如:

<iOSControls:CellContent CellData="{Binding .}"/>

BindingContext 属性似乎是标准 Xamarin 控件的一部分,而不是自定义本机控件的一部分。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Xamarin.iOS和Xamarin.Droid创建本机自定义控件

Xamarin IOS自定义控件属性不显示

如何在 Xamarin 中创建自定义控件?

自定义控件中的 Xamarin BindableProperty

xamarin 中的自定义控件示例?

Xamarin.iOS自定义控件不遵守高度限制

自定义控件中的Xamarin.Forms绑定集合

Xamarin Forms中的自定义控件不起作用

[Xamarin.Forms] ListView中的自定义控件

Xamarin iOS中的自定义振动

xamarin.forms中我的自定义控件的HeightRequest的自定义BindableProperty

iOS的自定义控件始终偏移

Xamarin 表单 - 创建复杂的自定义控件(创建多个交通标志)

在自定义 UIView 类中创建的 Xamarin iOS 子视图未显示

在CustomRenderer(Xamarin.Forms iOS)中获取控件的尺寸

在 Xamarin.iOS 中隐藏和显示控件

Xamarin Forms XAML-创建没有UI的自定义控件?

Xamarin Forms自定义控件数据绑定问题

Xamarin 自定义控件绑定不起作用

Xamarin:将参数从绑定传递到自定义控件

如何使我的自定义控件以Xamarin形式浮动?

Xamarin.iOS中的自定义SQLite函数

在Xamarin表单中更改BindableProperty时不调用自定义控件OnElementChanged

Xamarin Forms Framework中的自定义控件导致Null引用异常

Xamarin.Forms自定义控件中引发的序列异常

Xamarin自定义控件,如何添加属性以在Xcode面板中显示?

我可以使用 BindableProperty 在我的 Xamarin ContentView 自定义控件中禁用按钮吗?

如何使 xamarin 自定义控件中的 2 个可绑定属性相互识别?

ReactiveUI中的Xamarin.Forms控件是否需要自定义绑定?