这是我的选择器模型类
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Mobile.iOS
{
public class PickerDataModel : UIPickerViewModel
{
public event EventHandler<EventArgs> ValueChanged;
public List<string> Items
{
get { return this._items;}
set { this._items = value;}
}
List<string> _items = new List<string>();
public string SelectedItem
{
get { return this._items[this._selectedIndex]; }
}
protected int _selectedIndex = 0;
public PickerDataModel()
{
}
public override int GetRowsInComponent (UIPickerView picker, int component)
{
return this._items.Count;
}
public override string GetTitle (UIPickerView picker, int row, int component)
{
return this._items[row];
}
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override void Selected (UIPickerView picker, int row, int component)
{
this._selectedIndex = row;
if (this.ValueChanged != null)
{
this.ValueChanged (this, new EventArgs());
}
}
}
}
然后我创建一个动作表来显示像这样的UIPicker
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Mobile.iOS
{
public class ActionSheetChildAgePicker
{
#region -= declarations =-
UIActionSheet actionSheet;
UIButton doneButton = UIButton.FromType (UIButtonType.RoundedRect);
UIView owner;
UILabel titleLabel = new UILabel ();
#endregion
#region -= properties =-
public UIPickerView AgePicker
{
get { return agePicker; }
set { agePicker = value; }
}
//create agepicker
UIPickerView agePicker = new UIPickerView();
/// <summary>
/// The title that shows up for the date picker
/// </summary>
public string Title
{
get { return titleLabel.Text; }
set { titleLabel.Text = value; }
}
#endregion
#region -= constructor =-
/// <summary>
///
/// </summary>
public ActionSheetChildAgePicker (UIView owner)
{
// save our uiview owner
this.owner = owner;
// configure the title label
titleLabel.BackgroundColor = UIColor.Clear;
titleLabel.TextColor = UIColor.LightTextColor;
titleLabel.Font = UIFont.BoldSystemFontOfSize (18);
// configure the done button
doneButton.SetBackgroundImage(UIImage.FromBundle ("ZurfersMovil-LoginBar-SignButton.png"), UIControlState.Normal);
doneButton.SetTitleColor(MonoTouch.UIKit.UIColor.White, UIControlState.Normal);
doneButton.SetTitle ("done", UIControlState.Normal);
doneButton.TouchUpInside += (s, e) => { actionSheet.DismissWithClickedButtonIndex (0, true); };
// create + configure the action sheet
actionSheet = new UIActionSheet () { Style = UIActionSheetStyle.BlackTranslucent };
actionSheet.Clicked += (s, e) => { Console.WriteLine ("Clicked on item {0}", e.ButtonIndex); };
//Set picker Model and items
PickerDataModel agePickerDataModel = new PickerDataModel();
agePickerDataModel.Items.Add("<2");
agePickerDataModel.Items.Add("3");
agePickerDataModel.Items.Add("4");
agePickerDataModel.Items.Add("5");
agePickerDataModel.Items.Add("6");
agePickerDataModel.Items.Add("7");
agePickerDataModel.Items.Add("8");
agePickerDataModel.Items.Add("9");
agePickerDataModel.Items.Add("10");
agePickerDataModel.Items.Add("11");
agePickerDataModel.Items.Add("12");
agePickerDataModel.Items.Add("13");
agePickerDataModel.Items.Add("14");
agePickerDataModel.Items.Add("15");
agePickerDataModel.Items.Add("16");
agePickerDataModel.Items.Add("17");
agePicker.Source = agePickerDataModel;
agePicker.UserInteractionEnabled = true;
agePicker.ShowSelectionIndicator = true;
// add our controls to the action sheet
//actionSheet.AddSubview (datePicker);
actionSheet.AddSubview(AgePicker);
actionSheet.AddSubview (titleLabel);
actionSheet.AddSubview (doneButton);
}
#endregion
#region -= public methods =-
/// <summary>
/// Shows the action sheet picker from the view that was set as the owner.
/// </summary>
public void Show ()
{
// declare vars
float titleBarHeight = 40;
SizeF doneButtonSize = new SizeF (71, 30);
SizeF actionSheetSize = new SizeF(owner.Frame.Width, agePicker.Frame.Height + titleBarHeight);
RectangleF actionSheetFrame = new RectangleF (0, owner.Frame.Height - actionSheetSize.Height
, actionSheetSize.Width, actionSheetSize.Height);
// show the action sheet and add the controls to it
actionSheet.ShowInView (owner);
// resize the action sheet to fit our other stuff
actionSheet.Frame = actionSheetFrame;
// move our picker to be at the bottom of the actionsheet (view coords are relative to the action sheet)
agePicker.Frame = new RectangleF(agePicker.Frame.X, titleBarHeight, agePicker.Frame.Width, agePicker.Frame.Height);
// move our label to the top of the action sheet
titleLabel.Frame = new RectangleF (10, 4, owner.Frame.Width - 100, 35);
// move our button
doneButton.Frame = new RectangleF (actionSheetSize.Width - doneButtonSize.Width - 10, 7, doneButtonSize.Width, doneButtonSize.Height);
}
/// <summary>
/// Dismisses the action sheet date picker
/// </summary>
public void Hide (bool animated)
{
actionSheet.DismissWithClickedButtonIndex (0, animated);
}
#endregion
}
}
现在在我的视图控制器上我尝试选择这样的项目,但它不起作用,因为我没有ValueChanged方法。
actionSheetChildAgePicker.AgePicker.ValueChanged += (s, e) => {
this.txtAge1Room1 = f.ToString ((s as UIPickerView).Select(1,0,true));
};
任何帮助都会受到赞赏。提前谢谢。
答案 0 :(得分:0)
在ActionSheetChildAgePicker中公开您的agePickerDataModel并将其公开。
我也会在ActionSheetChildAgePicker中公开您的UIButton doneButton ..
public UIButton doneButton = UIButton.FromType (UIButtonType.RoundedRect);
注释掉ActionSheetChildAgePicker中的按钮事件,以便我们可以在视图控制器中处理它...
doneButton.SetTitleColor(MonoTouch.UIKit.UIColor.White, UIControlState.Normal);
doneButton.SetTitle ("done", UIControlState.Normal);
//doneButton.TouchUpInside += (s, e) => { actionSheet.DismissWithClickedButtonIndex (0, true); };
然后在你的视图控制器中你可以做这样的事情
actionSheetChildAgePicker.doneButton.TouchUpInside += (s, e) => {
string sPickedAge = actionSheetChildAgePicker.agePickerDataModel.SelectedItem;
actionSheetChildAgePicker.Hide(true);
};