使用MVVM体系结构从SignaturePadView检索图像

Teo*_*r81 5 c# prism signaturepad xamarin.forms

我正在使用MVVM体系结构中的Prism框架开发Xamarin.Forms应用程序。我需要从屏幕上收集签名,因此我决定包括SignaturePad库。在NuGet中,我包括了Xamarin.Controls.SignaturePad和Xamarin.Controls.SignaturePad.Forms包。在页面布局(使用XAML构建)中,我具有签名小部件:

<signature:SignaturePadView 
             x:Name="padView"
             HeightRequest="130"                                
             CaptionText="Sign"
             CaptionTextColor="Black"
             ClearText="Clean"
             ClearTextColor="Black"
             BackgroundColor="White"
             SignatureLineColor="Black"
             StrokeWidth="2"
             StrokeColor="Black"
             BindingContext="{Binding Sign, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)

在ViewModel中,小部件绑定:

private SignaturePadView _sign;
public SignaturePadView Sign
{
    get { return _sign; }
    set { SetProperty(ref _sign, value); }
}
Run Code Online (Sandbox Code Playgroud)

在ViewModel构造函数中:

_sign = new SignaturePadView();
Run Code Online (Sandbox Code Playgroud)

还有一个按钮,在此按钮的动作中,我需要阅读标志图像并将其保存到数据库中。我尝试了这个:

Stream sig = await Sign.GetImageStreamAsync(SignatureImageFormat.Png);
var signatureMemoryStream = sig as MemoryStream;
byte[] data = signatureMemoryStream.ToArray();            
Run Code Online (Sandbox Code Playgroud)

所有这些代码都写在可移植项目中。不幸的是,它不起作用,因为sig对象始终为null。我认为问题在于小部件绑定,但我不确定。

hug*_*ugo 6

这是使用SignaturePad的另一种方式(这有助于避免将视图放入视图模型中)。我本可以使用事件聚合器系统将消息从VM发送到View,但是使用Func对我来说是最简单的解决方案。

注意,我根本不使用Prism,所以最终的解决方案可能会有所不同...

没有设置BindingContext的情况下,签名视图的XAML部分几乎相同(来自我的文件TestPage.xaml)

<signature:SignaturePadView Margin="-10, 0, -10, 0" 
    x:Name="SignatureView" 
    HorizontalOptions="FillAndExpand" 
    VerticalOptions="FillAndExpand" 
    HeightRequest="150" 
    CaptionText="Signature" 
    CaptionTextColor="Blue" 
    ClearText="Effacer" 
    ClearTextColor="Black" 
    PromptText=""
    PromptTextColor="Green" 
    BackgroundColor="Silver" 
    SignatureLineColor="Black" 
    StrokeWidth="3" 
    StrokeColor="Black" />
Run Code Online (Sandbox Code Playgroud)

在我页面的代码背后(TestPage.xaml.cs)

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        var vm = (TestViewModel)BindingContext; // Warning, the BindingContext View <-> ViewModel is already set

        vm.SignatureFromStream = async () =>
        {
            if (SignatureView.Points.Count() > 0)
            {
                using (var stream = await SignatureView.GetImageStreamAsync(SignaturePad.Forms.SignatureImageFormat.Png))
                {
                    return await ImageConverter.ReadFully(stream);
                }
            }

            return await Task.Run(() => (byte[])null);
        };
    }
Run Code Online (Sandbox Code Playgroud)

其中ImageConverter.ReadFully(...)只是流到字节转换器

public static class ImageConverter
{
    public static async Task<byte[]> ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (var ms = new MemoryStream())
        {
            int read;
            while ((read = await input.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而且viewmodel看起来像这样

public class TestViewModel : ViewModelBase
{
    public Func<Task<byte[]>> SignatureFromStream { get; set; }
    public byte[] Signature { get; set; }

    public ICommand MyCommand => new Command(async () =>
    {
        Signature = await SignatureFromStream();
        // Signature should be != null
    });
}
Run Code Online (Sandbox Code Playgroud)