protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set the view from the Main.axml layout resource SetContentView(Resource.Layout.Main); // Get a reference to the FingerPaintCanvasView from the Main.axml file fingerPaintCanvasView = FindViewById <FingerPaintCanvasView>(Resource.Id.canvasView); // Set up the Spinner to select stroke color Spinner colorSpinner = FindViewById <Spinner>(Resource.Id.colorSpinner); colorSpinner.ItemSelected += OnColorSpinnerItemSelected; var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.colors_array, Android.Resource.Layout.SimpleSpinnerItem); adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem); colorSpinner.Adapter = adapter; // Set up the Spinner to select stroke width Spinner widthSpinner = FindViewById <Spinner>(Resource.Id.widthSpinner); widthSpinner.ItemSelected += OnWidthSpinnerItemSelected; var widthsAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.widths_array, Android.Resource.Layout.SimpleSpinnerItem); widthsAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem); widthSpinner.Adapter = widthsAdapter; // Set up the Clear button Button clearButton = FindViewById <Button>(Resource.Id.clearButton); clearButton.Click += OnClearButtonClick; }
public override void LoadView() { base.LoadView(); // White view covering entire screen UIView contentView = new UIView() { BackgroundColor = UIColor.White }; View = contentView; // Vertical UIStackView offset from status bar CGRect rect = UIScreen.MainScreen.Bounds; rect.Y += 20; rect.Height -= 20; UIStackView vertStackView = new UIStackView(rect) { Axis = UILayoutConstraintAxis.Vertical, }; contentView.Add(vertStackView); // Horizontal UIStackView for tools UIStackView horzStackView = new UIStackView { Axis = UILayoutConstraintAxis.Horizontal, Alignment = UIStackViewAlignment.Center, Distribution = UIStackViewDistribution.EqualSpacing }; vertStackView.AddArrangedSubview(horzStackView); // FingerPaintCanvasView for drawing FingerPaintCanvasView canvasView = new FingerPaintCanvasView(); vertStackView.AddArrangedSubview(canvasView); // Add space at left to horizontal UIStackView horzStackView.AddArrangedSubview(new UILabel(new CGRect(0, 0, 10, 10))); // Construct UIPickerView for choosing color, but don't add it to any view PickerDataModel <UIColor> colorModel = new PickerDataModel <UIColor> { Items = { new NamedValue <UIColor>("Red", UIColor.Red), new NamedValue <UIColor>("Green", UIColor.Green), new NamedValue <UIColor>("Blue", UIColor.Blue), new NamedValue <UIColor>("Cyan", UIColor.Cyan), new NamedValue <UIColor>("Magenta", UIColor.Magenta), new NamedValue <UIColor>("Yellow", UIColor.Yellow), new NamedValue <UIColor>("Black", UIColor.Black), new NamedValue <UIColor>("Gray", UIColor.Gray), new NamedValue <UIColor>("White", UIColor.White) } }; UIPickerView colorPicker = new UIPickerView { Model = colorModel }; // Ditto for UIPickerView for stroke thickness PickerDataModel <float> thicknessModel = new PickerDataModel <float> { Items = { new NamedValue <float>("Thin", 2), new NamedValue <float>("Thinish", 5), new NamedValue <float>("Medium", 10), new NamedValue <float>("Thickish", 20), new NamedValue <float>("Thick", 50) } }; UIPickerView thicknessPicker = new UIPickerView { Model = thicknessModel }; // Create UIToolbar for dismissing picker when it's displayed var toolbar = new UIToolbar(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 44)) { BarStyle = UIBarStyle.Default, Translucent = true }; // Set Font to be used in tools UIFont font = UIFont.SystemFontOfSize(24); // Create a NoCaretField text field for invoking color picker & add to horizontal UIStackView // (technique from Xamarin.Forms iOS PickerRenderer UITextField colorTextField = new NoCaretField { Text = "Red", InputView = colorPicker, InputAccessoryView = toolbar, Font = font }; horzStackView.AddArrangedSubview(colorTextField); // Use ValueChanged handler to change the color colorModel.ValueChanged += (sender, args) => { colorTextField.Text = colorModel.SelectedItem.Name; canvasView.StrokeColor = colorModel.SelectedItem.Value.CGColor; }; // Ditto for the thickness UITextField thicknessTextField = new NoCaretField { Text = "Thin", InputView = thicknessPicker, InputAccessoryView = toolbar, Font = font }; horzStackView.AddArrangedSubview(thicknessTextField); thicknessModel.ValueChanged += (sender, args) => { thicknessTextField.Text = thicknessModel.SelectedItem.Name; canvasView.StrokeWidth = thicknessModel.SelectedItem.Value; }; // Now add a Done button to the toolbar to rest text fields var spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace); var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (o, a) => { colorTextField.ResignFirstResponder(); thicknessTextField.ResignFirstResponder(); }); toolbar.SetItems(new[] { spacer, doneButton }, false); // Create the Clear button UIButton button = new UIButton(UIButtonType.RoundedRect) { Font = font }; horzStackView.AddArrangedSubview(button); button.Layer.BorderColor = UIColor.Black.CGColor; button.Layer.BorderWidth = 1; button.Layer.CornerRadius = 10; button.SetTitle("Clear", UIControlState.Normal); button.SetTitleColor(UIColor.Black, UIControlState.Normal); button.TouchUpInside += (sender, args) => { canvasView.Clear(); }; // Add space at right to horizontal UIStackView horzStackView.AddArrangedSubview(new UILabel(new CGRect(0, 0, 10, 10))); }