Xamarin SignaturePad - 生成SignaturePadView与XAML的代码的加载点

时间:2018-02-08 22:23:17

标签: c# xaml xamarin.forms

我将SignaturePad Points存储到数据库中,旨在提取并存储为PDF。除了向SignaturePadView对象添加点之外,它还能正常工作。

要添加积分,我只是使用signaturePad.Points = points。这种方法在插入显示到屏幕的XAML SignaturePad元素时工作正常......但是存储到代码生成的Object会导致零数组长度。

我的代码包含两种情况。

我的问题是:
我在尝试为生成的SignaturePadView代码分配点时错过了一步,和/或我是不是要渲染到屏幕(直接存储到PDF)这个潜在的问题

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:H1PaperlessApp"
            xmlns:forms="clr-namespace:SignaturePad.Forms;assembly=SignaturePad.Forms"
            x:Class="H1PaperlessApp.Views.Temppage"
            Padding="10">    
    <StackLayout>    
        <forms:SignaturePadView x:Name="PadView"
                            HeightRequest="150"
                            WidthRequest="240"
                            BackgroundColor="White"
                            StrokeColor="Black"
                            StrokeWidth="2"/>    
        <forms:SignaturePadView x:Name="ClonePadView"
                            HeightRequest="150"
                            WidthRequest="240"
                            BackgroundColor="White"
                            StrokeColor="Black"
                            StrokeWidth="2"/>    
        <Button Text="Save" FontSize="30" BackgroundColor="DodgerBlue" TextColor="White"
            Clicked="SaveButton_Clicked"/>    
    </StackLayout>
</ContentPage>

代码背后(从一个XAML生成元素克隆到另一个元素)。在这种情况下,第二个点设置正确

private async void SaveButton_Clicked(object sender, EventArgs e)
{
    var pnts = PadView.Points.ToArray(); // pnts = {Xamarin.Forms.Point[294]}
    var originalPoints = JsonConvert.SerializeObject(pnts);
    Xamarin.Forms.Point[] points = JsonConvert.DeserializeObject<Xamarin.Forms.Point[]>(originalPoints);
    ClonePadView.Points = points; // ClonePadView.Points = {Xamarin.Forms.Point[294]}
    Stream img = await ClonePadView.GetImageStreamAsync(SignatureImageFormat.Png);  
}

代码隐藏(通过代码创建目标SignaturePadView。请注意,第二个点的计数为0)

private async void SaveButton_Clicked(object sender, EventArgs e)
{
    var pnts = PadView.Points.ToArray();  // pnts = {Xamarin.Forms.Point[294]}
    var originalPoints = JsonConvert.SerializeObject(pnts);
    Xamarin.Forms.Point[] points = JsonConvert.DeserializeObject<Xamarin.Forms.Point[]>(originalPoints);
    SignaturePadView CodeClonePadView = new SignaturePadView { };
    CodeClonePadView.Points = points;  // ClonePadView.Points = {Xamarin.Forms.Point[0]}
    Stream img = await CodeClonePadView.GetImageStreamAsync(SignatureImageFormat.Png);
}

1 个答案:

答案 0 :(得分:0)

根据我的经验,SignaturePadView 必须可见并呈现,以便设置其 Points 或 Strokes。您可以在页面的 Appearing 事件中执行此操作。

这是 XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SignaturePadPOC.MainPage" Appearing="MainPage_OnAppearing">

    <StackLayout x:Name="MyStackLayout">
    </StackLayout>

</ContentPage>

这是背后的代码:

public partial class MainPage : ContentPage
{
    public Point[][] Strokes { get; set; } // Set this prior to the appearing event;
    private SignaturePadView signatureView;

    public MainPage()
    {
        InitializeComponent();

        signatureView = new SignaturePadView
        {
            BackgroundColor = Color.White,
            StrokeColor = Color.Black,
            StrokeWidth = 3
        };
        MyStackLayout.Children.Add(signatureView);
    }

    private void MainPage_OnAppearing(object sender, EventArgs e)
    {
        signatureView.Strokes = Strokes;
    }
}