Xamarin.Forms PCL / SAP自定义渲染,支持paintcode 3

时间:2017-02-24 17:51:50

标签: c# xamarin xamarin.ios xamarin.forms paint-code

专家!我有一个非常好的问题,我正在使用Paintcode 3,我正在使用Xamarin.Forms(PCL / SAP)项目。我现在有一个示例项目正在运行,我在paintcode 3中有一个自定义绘图。但是,据我所知,paintcode假设与c#Xamarin一起本地工作。但是,我想我可以创建一个自定义渲染器我的xamarin.forms项目然后,提取自定义paintcode 3绘图然后,创建一个渲染并将其粘贴在那里。在我发布任何代码之前我的问题是这种方法之前已经完成或有人试过吗?

到目前为止,这是我的代码非常简单:

我的主页:

   public class MainPageCS :ContentPage
{
    public MainPageCS()
    {

        // The root page of your application

        Title = "PCTest";
        Padding = new Thickness(0, 20, 0, 0);

        Content = new TestView
        {


            // draw object will go here once i render it.

            BackgroundColor = Color.Blue,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center

        };

    }

}

简单内容页面,Test视图继承自Xamarin.forms视图。

 public class TestView : View
{
    public TestView()
    {
    }
}

现在看起来很简单但是,这里是它变得怪异的地方这里是我创建的paintcode项目。这是图像。 PaintCodeProjectImage

现在,这是生成该图像的代码。 TestChatBox.cs

namespace PaintCodeStyleKitTest
{
[Register("TestChatBox")]
public class TestChatBox : NSObject
{

    //// Cache

    private static UIImage imageOfChatIcon;
    private static NSObject[] chatIconTargets;

    //// Initialization

    static TestChatBox()
    {

        DrawChatIcon();
    }

    //// Drawing Methods

    public static void DrawChatIcon()
    {

        //// Oval 2 Drawing
        var oval2Path = new UIBezierPath();
        oval2Path.MoveTo(new CGPoint(105.65f, 43.5f));
        oval2Path.AddCurveToPoint(new CGPoint(55.59f, 56.97f), new CGPoint(87.51f, 43.64f), new CGPoint(69.43f, 48.13f));
        oval2Path.AddCurveToPoint(new CGPoint(35.44f, 96.92f), new CGPoint(38.57f, 67.85f), new CGPoint(31.85f, 82.81f));
        UIColor.Gray.SetStroke();
        oval2Path.LineWidth = 9.0f;
        oval2Path.LineCapStyle = CGLineCap.Round;
        oval2Path.Stroke();


        //// Bezier 2 Drawing
        var bezier2Path = new UIBezierPath();
        bezier2Path.MoveTo(new CGPoint(166.1f, 48.28f));
        bezier2Path.AddCurveToPoint(new CGPoint(166.1f, 131.72f), new CGPoint(199.3f, 71.32f), new CGPoint(199.3f, 108.68f));
        bezier2Path.AddCurveToPoint(new CGPoint(89.68f, 147.91f), new CGPoint(145.34f, 146.13f), new CGPoint(116.5f, 151.53f));
        bezier2Path.AddCurveToPoint(new CGPoint(43.5f, 169.5f), new CGPoint(71.56f, 159.66f), new CGPoint(43.5f, 169.5f));
        bezier2Path.AddCurveToPoint(new CGPoint(58.33f, 138.86f), new CGPoint(43.5f, 169.5f), new CGPoint(53.16f, 153.11f));
        bezier2Path.AddCurveToPoint(new CGPoint(45.9f, 131.72f), new CGPoint(53.95f, 136.8f), new CGPoint(49.78f, 134.42f));
        bezier2Path.AddCurveToPoint(new CGPoint(45.9f, 48.28f), new CGPoint(12.7f, 108.68f), new CGPoint(12.7f, 71.32f));
        bezier2Path.AddCurveToPoint(new CGPoint(166.1f, 48.28f), new CGPoint(79.09f, 25.24f), new CGPoint(132.91f, 25.24f));
        bezier2Path.ClosePath();
        UIColor.Red.SetStroke();
        bezier2Path.LineWidth = 9.0f;
        bezier2Path.Stroke();
    }

    //// Generated Images

    public static UIImage ImageOfChatIcon
    {
        get
        {
            if (imageOfChatIcon != null)
                return imageOfChatIcon;

            UIGraphics.BeginImageContextWithOptions(new CGSize(210.0f, 190.0f), false, 0);
            TestChatBox.DrawChatIcon();
            imageOfChatIcon = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            return imageOfChatIcon;
        }
    }

    //// Customization Infrastructure

    [Outlet]
    public NSObject[] ChatIconTargets
    {
        get { return chatIconTargets; }
        set
        {
            chatIconTargets = value;
            foreach (NSObject target in value)
            {
                target.PerformSelector(new ObjCRuntime.Selector("setImage:"), ImageOfChatIcon, 0);
            }
        }
    }

}

现在,上面是我完成那个简单图像时为我生成的paintcode。 从阅读paintcode文档开始,生成的代码必须覆盖drawrect方法才能绘制图片,看看我的Test.cs

 using System;   
    using Xamarin.Forms;
    using CoreGraphics;
    using UIKit;
    using System.Drawing;
    using Foundation;
    using PaintCodeStyleKitTest;
    namespace PCTest
    {
        public class Test : UIView
        {
            public Test()
            {
                TestChatBox.DrawChatIcon();
            }

            public override void Draw(CGRect rect)
            {

                base.Draw(rect);
                TestChatBox.DrawChatIcon();


            }


        }
    }

这会覆盖draw方法并且应该成功绘制图片,但是,当我尝试使用我的测试渲染器文件完成自定义渲染时。

    using System;
using Xamarin.Forms;
using PCTest;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using PaintCodeStyleKitTest;
using PCTest.iOS;
using Foundation;
using UIKit;
using CoreGraphics;

[assembly: ExportRenderer(typeof(TestView), typeof(TestRenderer))]
namespace PCTest.iOS
{
    public class TestRenderer : ViewRenderer<TestView, Test>
    {
        Test txt;
        protected override void OnElementChanged(ElementChangedEventArgs <TestView> e )
            {
                base.OnElementChanged(e);
        if (Control == null)
        {
            //txt = new Test( tes);
            Test tests = new Test();

            SetNativeControl(new Test());

        }
            if (e.OldElement != null)
            {
                return;
            }

        }


 }


}

我运行我的示例应用程序,没有任何内容被绘制到视图中。我想知道为什么。我试图应用一个简单的内容页面,只是为了看看我的样本视图是否会呈现,但它没有渲染,任何人都可以指出我正确的方向。我对此采取的方法有什么不妥吗?请帮助!

1 个答案:

答案 0 :(得分:0)

您没有为自定义控件分配任何维度,因此您的自定义Frame的{​​{1}}永远不会被设置,因此它位于UIView,其大小为{{1} }。

0,0

enter image description here