将SignaturePad添加到片段/视图

时间:2014-08-12 12:22:08

标签: android android-fragments xamarin mvvmcross

首先我使用VisualStudio 2013,最新版本的xamarin和MvvmCross。 几天后我使用了适用于Android Xamarin的lib SignaturePad https://components.xamarin.com/view/signature-pad

我有很多Tabs,每个Tab都是片段。 在One Fragment中我想添加签名字段或其中2个。但它没有用 我想在CreateView方法上添加Field

这里的片段代码:

class TakePictureFragment : MvxFragment
{
    private File _dir;
    private File _file;
    private ImageView _imageView;

    public override void OnActivityResult(int requestCode, int resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        // make it available in the gallery
        Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
        Uri contentUri = Uri.FromFile(_file);
        mediaScanIntent.SetData(contentUri);
        Activity.SendBroadcast(mediaScanIntent);

        // display in ImageView. We will resize the bitmap to fit the display
        // Loading the full sized image will consume to much memory 
        // and cause the application to crash.
        int height = _imageView.Height;
        int width = Resources.DisplayMetrics.WidthPixels;
        using (Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height))
        {
            _imageView.RecycleBitmap();
            _imageView.SetImageBitmap(bitmap);
        }
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        var view = inflater.Inflate(Resource.Layout.TakePicture, null);
        var Layout = new LinearLayout(Activity)
        {
            Orientation = Orientation.Vertical,
            LayoutParameters = new LinearLayout.LayoutParams(container.Width, ViewGroup.LayoutParams.WrapContent)

        };
            //this.BindingInflate(Resource.Layout.TakePicture, null); 

        var signature = new SignaturePadView(Activity);
        signature.Visibility = ViewStates.Visible;
        signature.SetGravity(GravityFlags.Top);
        Layout.AddView(signature, 200, 200);
        container.AddView(Layout);
        /*AddContentView(signature,
            container.LayoutParams(200, 200));*/

        if (IsThereAnAppToTakePictures())
        {
            CreateDirectoryForPictures();

            Button button = view.FindViewById<Button>(Resource.Id.myButton);
            _imageView = view.FindViewById<ImageView>(Resource.Id.imageView1);

            button.Click += TakeAPicture;

        }

        return view;
    }



    private void CreateDirectoryForPictures()
    {
        _dir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");
        if (!_dir.Exists())
        {
            _dir.Mkdirs();
        }
    }

    private bool IsThereAnAppToTakePictures()
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);

        IList<ResolveInfo> availableActivities = Activity.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
        return availableActivities != null && availableActivities.Count > 0;
    }

    private void TakeAPicture(object sender, EventArgs eventArgs)
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);

        _file = new File(_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));

        intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));

        StartActivityForResult(intent, 0);
    }
}

}

我希望有人能帮助我

Greez

1 个答案:

答案 0 :(得分:0)

您是否尝试过返回容器而不是视图?如果这不起作用,我会在Resource.Layout.TakePicture中放置一个空的视图组,并将其添加到那里。

View insertPoint = findViewById(R.id.insert_point);
((ViewGroup) insertPoint).addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

更新:

要在xml中添加视图组,请执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/openCamera" />
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:id="@+id/imageView1"
        android:adjustViewBounds="true" />
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/insert_point"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

然后您可以使用view.findViewById(R.id.insert_point);获取并将数据添加到其中!

您的代码应如下所示:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    var view = inflater.Inflate(Resource.Layout.TakePicture, null);
    var Layout = new LinearLayout(Activity)
    {
        Orientation = Orientation.Vertical,
        LayoutParameters = new LinearLayout.LayoutParams(container.Width, ViewGroup.LayoutParams.WrapContent)

    };
        //this.BindingInflate(Resource.Layout.TakePicture, null); 

    var signature = new SignaturePadView(Activity);
    signature.Visibility = ViewStates.Visible;
    signature.SetGravity(GravityFlags.Top);
    var insertPoint = view.findViewById(R.id.insert_point);
    insertPoint.AddView(signature);


    if (IsThereAnAppToTakePictures())
    {
        CreateDirectoryForPictures();

        Button button = view.FindViewById<Button>(Resource.Id.myButton);
        _imageView = view.FindViewById<ImageView>(Resource.Id.imageView1);

        button.Click += TakeAPicture;

    }

    return view;
}