Xamarin.ios 基本控件
Posted iZKang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xamarin.ios 基本控件相关的知识,希望对你有一定的参考价值。
1// 1、按钮 UIButton 2 UIButton btn = new UIButton(); 3 btn.Frame = new RectangleF(150,310,80,30); //按钮位置一件宽高 4 btn.SetTitle("Button",UIControlState.Normal); //显示的文字 5 btn.SetTitleColor(UIColor.Black,UIControlState.Normal);//文字的颜色 6 btn.TouchUpInside += this.SetBackGroudColor; //按钮 触摸事件 7 this.View.AddSubview(btn); //添加到当前视图 8 9 //2、图像视图 UIImageView 10 UIImageView img=new UIImageView(); 11 img.Frame = new RectangleF(0,0,375,667); //设置位置大小 12 img.ContentMode = UIViewContentMode.ScaleAspectFit; //设置图像视图模式 13 img.Image = UIImage.FromFile("img.jpg"); //设置图像 14 img.Transform = CGAffineTransform.MakeRotation(20);//图像旋转正数顺时针旋转,负数逆时针旋转 15 img.Transform = CGAffineTransform.MakeScale(-2,2); //图像缩放 16 this.View.AddSubview(img); //添加到当前视图 17 18 //3、label控件 UILabel 19 UILabel lbl1 = new UILabel(); 20 lbl1.Frame = new RectangleF(2,410,155,28); 21 lbl1.Text = "UILabel"; 22 lbl1.Lines = 3; //显示行数 配合 label高度 使用 23 lbl1.LineBreakMode = UILineBreakMode.MiddleTruncation; //内容显示样式 24 lbl1.Hidden = true; //是否显示 25 this.View.AddSubview(lbl1); 26 27 //4、输入框 UITextField 28 UITextField t1 = new UITextField(); 29 t1.BorderStyle = UITextBorderStyle.RoundedRect; 30 t1.Frame = new RectangleF(50,250,300,30); 31 t1.Placeholder = "账号"; 32 // 限制输入字符长度 33 t1.ShouldChangeCharacters = (textField, range, replacementString) => { 34 if (range.Location < 10) 35 return true; 36 else 37 return false; 38 }; 39 this.View.AddSubview(t1); 40 UITextField t2 = new UITextField(); 41 t2.BorderStyle = UITextBorderStyle.RoundedRect;//边框样式 42 t2.Frame = new RectangleF(50, 300, 300, 30); 43 t2.Placeholder = "密码"; 44 t2.SecureTextEntry = true; //隐藏文本 45 this.View.AddSubview(t2); 46 47 48 UITextView text = new UITextView(); 49 text.Frame = new RectangleF(9, 90, 302, 180); 50 51 this.View.AddSubview(text); 52 //5、UITextView 实现功能:点击完成按钮隐藏键盘,并且把text的Text值赋值给text1控件,并监控 text控件的状态(开始输入,结束输入,输入中) 53 UITextView text1 = new UITextView(); 54 text1.Frame = new RectangleF(9, 330, 302, 180); 55 text1.Editable = false; 56 this.View.AddSubview(text1); 57 text1.Hidden = true; 58 UIButton btn = new UIButton(); 59 btn.Frame = new RectangleF(137,56,46,30); 60 btn.SetTitle("完成", UIControlState.Normal); 61 btn.SetTitleColor(UIColor.Red,UIControlState.Normal); 62 this.View.AddSubview(btn); 63 btn.TouchUpInside += (sender, e) => { 64 text.ResignFirstResponder(); 65 text1.Hidden=false; 66 text1.Text = text.Text; 67 68 }; 69 text.Delegate = new MyDelegate(); 70 71 } 72 private class MyDelegate : UITextViewDelegate 73 { 74 public override void EditingEnded(UITextView textView) 75 { 76 Console.WriteLine("Edit end"); 77 } 78 public override void EditingStarted(UITextView textView) 79 { 80 Console.WriteLine("Edit start"); 81 } 82 public override void Changed(UITextView textView) 83 { 84 Console.WriteLine("Edit "); 85 } 86 } 87 //6、 输入框键盘 样式类型 设置 88 UITextField fied = new UITextField(); 89 fied.Frame = new RectangleF(50, 100, 300, 30); 90 fied.BorderStyle = UITextBorderStyle.Line; 91 this.View.AddSubview(fied); 92 fied.KeyboardType = UIKeyboardType.Url;//设置键盘类型 93 fied.KeyboardAppearance = UIKeyboardAppearance.Dark;// 设置键盘外观 94 fied.ReturnKeyType = UIReturnKeyType.Next;//设置键盘的Return键 95 96// 7、显示键盘 时改变输入框的位置 97 UITextField fied = new UITextField(); 98 private NSObject kbdWillShow, kbdDidHide; 99 100 public override void ViewDidLoad() 101 { 102 base.ViewDidLoad(); 103 fied.Frame = new RectangleF(10, 500, 300, 30); 104 fied.BorderStyle = UITextBorderStyle.RoundedRect; 105 this.View.AddSubview(fied); 106 //显示键盘前 (事件) 107 kbdWillShow = UIKeyboard.Notifications.ObserveWillShow((s, e) => { 108 RectangleF kbdRounds = (RectangleF)e.FrameEnd; 109 RectangleF textFrame = (RectangleF)fied.Frame; 110 textFrame.Y -= kbdRounds.Height; 111 fied.Frame = textFrame; 112 }); 113 //隐藏键盘后 (事件) 114 kbdDidHide = UIKeyboard.Notifications.ObserveDidHide((s, e) => 115 { 116 RectangleF kbdRounds = (RectangleF)e.FrameEnd; 117 RectangleF textFrame = (RectangleF)fied.Frame; 118 textFrame.Y += kbdRounds.Height; 119 fied.Frame = textFrame; 120 }); 121 //键盘上的return键 隐藏键盘 (事件) 122 fied.ShouldReturn=delegate(UITextField textField) { 123 return textField.ResignFirstResponder(); 124 }; 125 } 126 //8、 为输入框键盘 添加工具栏 127 UITextField fied = new UITextField(); 128 fied.Frame = new RectangleF(10, 200, 300, 30); 129 fied.BorderStyle = UITextBorderStyle.RoundedRect;//设置输入框边框 130 this.View.AddSubview(fied); 131 fied.KeyboardType = UIKeyboardType.EmailAddress;//设置键盘类型 132 //设施工具栏 133 UIToolbar bar = new UIToolbar();//实例化工具栏 134 bar.SizeToFit(); 135 bar.BackgroundColor = UIColor.DarkGray;//设置背景 136 //实例化 工具栏 按钮 137 UIBarButtonItem item = new UIBarButtonItem("完成", UIBarButtonItemStyle.Done, (s, e) => 138 { 139 //隐藏键盘 140 fied.ResignFirstResponder(); 141 }); 142 //为工具栏设置条目 143 bar.SetItems(new UIBarButtonItem[]{ item},true); 144 //为输入框 键盘添加工具栏 145 fied.InputAccessoryView = bar; 146 147 //9、文本视图菜单 退出按钮 148 public override void ViewDidLoad() 149 { 150 tf.Frame = new RectangleF(20,10,300,100); 151 this.View.AddSubview(tf); 152 UIMenuItem item = new UIMenuItem("退出", new Selector("hide"));//添加菜单按钮 153 154 UIMenuController c = UIMenuController.SharedMenuController; 155 c.MenuItems = new UIMenuItem[] { item }; 156 157 } 158 UITextView tf = new UITextView(); 159 [Export("hide")] 160 public void hide() { 161 //退出键盘 162 tf.ResignFirstResponder(); 163 } 164 165 //10、触摸背景隐藏键盘 166 public override void ViewDidLoad() 167 { 168 tf.Frame = new RectangleF(10,50,300,30); 169 tf.BorderStyle = UITextBorderStyle.RoundedRect; 170 this.View.AddSubview(tf); 171 172 } 173 /// <summary> 174 ///重写 ViewController 方法 TouchesBegan 175 /// </summary> 176 /// <param name="touches">Touches.</param> 177 /// <param name="evt">Evt.</param> 178 public override void TouchesBegan(NSSet touches, UIEvent evt) 179 { 180 tf.ResignFirstResponder(); 181 } 182// 11、进度条控件 UIProgressView 先在添加一个lable lbl 和一个button sta 183 public override void ViewDidLoad() 184 { 185 //按钮的触摸事件 186 sta.TouchUpInside+=delegate { 187 sta.Enabled = false; 188 pv.Progress = 0f; 189 //创建一个新的任务 用来计算 进度条的值 并更新视图 190 Task.Factory.StartNew(this.Start); 191 }; 192 /创建进度条 并添加到视图 193 pv = new UIProgressView(new RectangleF(60f,200f,200f,50f)); 194 pv.Progress = 0f; 195 f = 1f / 10f; 196 this.View.AddSubview(pv); 197 } 198 UIProgressView pv; 199 float f = 0f; 200 public void Start() { 201 float p = 0f; 202 while (p<1f) { 203 Thread.Sleep(1000); 204 //调用主线程 修改视图 205 this.InvokeOnMainThread(delegate { 206 pv.Progress += this.f; 207 p = pv.Progress; 208 lbl.Text = Math.Round(pv.Progress, 2).ToString(); 209 if (p >= 1f) 210 { 211 lbl.Text = "加载完成!"; 212 sta.Enabled = true; 213 } 214 }); 215 } 216 217 } 218// 12、滚动视图 219 (1). 220 UIImageView img; 221 UIScrollView scr; 222 public override void ViewDidLoad() 223 { 224 base.ViewDidLoad(); 225 img = new UIImageView(UIImage.FromFile("1.png")); 226 scr = new UIScrollView(); 227 scr.Frame = new RectangleF(0,0,320,568); 228 scr.ContentSize = img.Image.Size; //滚动范围 229 scr.ContentOffset = new PointF(200f,50f); //目前滚动位置 230 scr.PagingEnabled = true; //可以整页翻动 231 scr.MinimumZoomScale=0.25f; //缩放最小比例 232 scr.MaximumZoomScale=2f; //放大的最大比例 233 //获取要缩放的图像视图 234 scr.ViewForZoomingInScrollView=delegate (UIScrollView scrll){ 235 return this.img; 236 }; 237 scr.ZoomScale = 1f; //设置变化比例 238 scr.IndicatorStyle = UIScrollViewIndicatorStyle.Black; //滚动指示器的风格设置 239 scr.AddSubview(img); 240 this.View.AddSubview(scr); 241 } 242 (2). 243 UIScrollView scr = new UIScrollView(); 244 scr.Frame = new RectangleF(0,0,325,568); 245 scr.ContentSize = new SizeF(320,2000); 246 this.View.AddSubview(scr); 247 //滚动视图开始时调用 248 scr.Scrolled+=delegate { 249 Console.WriteLine("Start rolling"); 250 }; 251 //滚动视图结束时调用 252 scr.DecelerationEnded+=delegate { 253 Console.WriteLine("Start End"); 254 }; 255 float y = 10; 256 for (float i = 1; i < 21; i++) { 257 UILabel lab = new UILabel(); 258 lab.Frame = new RectangleF(0,y,320,50); 259 lab.BackgroundColor = UIColor.Cyan; 260 lab.Text = String.Format("{0}",i); 261 scr.AddSubview(lab); 262 y += 100; 263 264 } 265// 13、页面控件 266 UIImageView img1; 267 UIImageView img2; 268 UIImageView img3; 269 UIScrollView scr; 270 UIPageControl pagc; 271 public override void ViewDidLoad() 272 { 273 base.ViewDidLoad(); 274 //添加滚动视图 275 scr = new UIScrollView(); 276 scr.Frame = new RectangleF(0,0,320,495); 277 //滚动视图结束滚动时调用 278 scr.DecelerationEnded += this.scr_DecelerationEnded; 279 //添加页面 280 pagc = new UIPageControl(); 281 pagc.Frame = new RectangleF(0,540,320,37); 282 pagc.Pages = 3; //设置页面数值 及页面小白点 283 //当页面数值发生改变时调用 284 pagc.ValueChanged += this.pagc_ValueChanged; 285 //滚动视图的滚动事件 286 scr.Scrolled+=delegate { 287 Console.WriteLine("Scolled!"); 288 }; 289 scr.PagingEnabled = true; 290 RectangleF rf = (RectangleF)scr.Frame; 291 scr.ContentSize = new SizeF(rf.Width*3,rf.Height); 292 //添加图像视图 293 img1 = new UIImageView(); 294 img1.ContentMode = UIViewContentMode.ScaleAspectFit; 295 img1.Image = UIImage.FromFile("1.jpg"); 296 rf.X += (float)this.scr.Frame.Width; 297 img2 = new UIImageView(); 298 img2.ContentMode = UIViewContentMode.ScaleAspectFit; 299 img2.Image = UIImage.FromFile("2.jpg"); 300 rf.X += (float)this.scr.Frame.Width; 301 img3 = new UIImageView(); 302 img3.ContentMode = UIViewContentMode.ScaleAspectFit; 303 img3.Image = UIImage.FromFile("3.jpg"); 304 rf.X += (float)this.scr.Frame.Width; 305 306 scr.AddSubview(img1); 307 scr.AddSubview(img2); 308 scr.AddSubview(img3); 309 this.View.AddSubview(scr); 310 this.View.AddSubview(pagc); 311 312 } 313 314 public void scr_DecelerationEnded(object sender,EventArgs e) { 315 float x1 = (float)this.img1.Frame.X;//获取图像img1 的x位置 316 float x2 = (float)this.img2.Frame.X;//获取图像img2的x位置 317 float x = (float)this.scr.ContentOffset.X;//获取视图对象scrolview scr 目前滚动的x位置 318 319 //判断x是否和x1相等 320 if (x == x1) 321 { 322 this.pagc.CurrentPage = 0; 323 324 } 325 else if (x == x2) 326 { 327 this.pagc.CurrentPage = 1; 328 329 } 330 else 331 { 332 this.pagc.CurrentPage = 2; 333 334 } 335 336 } 337 public void pagc_ValueChanged(object sender, EventArgs e) 338 { 339 PointF pf = (PointF)this.scr.ContentOffset; 340 switch (this.pagc.CurrentPage) { 341 case 0: 342 pf.X = (float)this.img1.Frame.X; 343 this.scr.SetContentOffset(pf,true); 344 break; 345 case 1: 346 pf.X = (float)this.img2.Frame.X; 347 this.scr.SetContentOffset(pf, true); 348 break; 349 case 2: 350 pf.X = (float)this.img3.Frame.X; 351 this.scr.SetContentOffset(pf, true); 352 break; 353 default: 354 break; 355 } 356 } 357// 14、警告视图 358 UIAlertView a = new UIAlertView(); 359 a.Title = "提示"; 360 a.Message = "电量不足"; 361 a.AddButton("确定"); 362 a.Show();
UIButton btn = new UIButton();
btn.SetTitle("Button",UIControlState.Normal); //显示的文字
btn.SetTitleColor(UIColor.Black,UIControlState.Normal);//文字的颜色
btn.TouchUpInside += this.SetBackGroudColor; //按钮 触摸事件
this.View.AddSubview(btn); //添加到当前视图
2、图像视图 UIImageView
UIImageView img=new UIImageView();
img.Frame = new RectangleF(0,0,375,667); //设置位置大小
img.ContentMode = UIViewContentMode.ScaleAspectFit; //设置图像视图模式
img.Image = UIImage.FromFile("img.jpg"); //设置图像
img.Transform = CGAffineTransform.MakeRotation(20);//图像旋转正数顺时针旋转,负数逆时针旋转
img.Transform = CGAffineTransform.MakeScale(-2,2); //图像缩放
this.View.AddSubview(img); //添加到当前视图
3、label控件 UILabel
lbl1.Frame = new RectangleF(2,410,155,28);
lbl1.Text = "UILabel";
lbl1.Lines = 3; //显示行数 配合 label高度 使用
lbl1.LineBreakMode = UILineBreakMode.MiddleTruncation; //内容显示样式
lbl1.Hidden = true; //是否显示
this.View.AddSubview(lbl1);
4、输入框 UITextField
UITextField t1 = new UITextField();
t1.BorderStyle = UITextBorderStyle.RoundedRect;
t1.Frame = new RectangleF(50,250,300,30);
t1.Placeholder = "账号";
// 限制输入字符长度
t1.ShouldChangeCharacters = (textField, range, replacementString) => {
if (range.Location < 10)
return true;
else
return false;
};
this.View.AddSubview(t1);
UITextField t2 = new UITextField();
t2.Frame = new RectangleF(50, 300, 300, 30);
t2.Placeholder = "密码";
t2.SecureTextEntry = true; //隐藏文本
this.View.AddSubview(t2);
UITextView text = new UITextView();
text.Frame = new RectangleF(9, 90, 302, 180);
this.View.AddSubview(text);
5、UITextView 实现功能:点击完成按钮隐藏键盘,并且把text的Text值赋值给text1控件,并监控 text控件的状态(开始输入,结束输入,输入中)
UITextView text1 = new UITextView();
text1.Frame = new RectangleF(9, 330, 302, 180);
text1.Editable = false;
this.View.AddSubview(text1);
text1.Hidden = true;
UIButton btn = new UIButton();
btn.Frame = new RectangleF(137,56,46,30);
btn.SetTitle("完成", UIControlState.Normal);
btn.SetTitleColor(UIColor.Red,UIControlState.Normal);
this.View.AddSubview(btn);
btn.TouchUpInside += (sender, e) => {
text.ResignFirstResponder();
text1.Hidden=false;
text1.Text = text.Text;
};
text.Delegate = new MyDelegate();
}
private class MyDelegate : UITextViewDelegate
{
public override void EditingEnded(UITextView textView)
{
Console.WriteLine("Edit end");
}
public override void EditingStarted(UITextView textView)
{
Console.WriteLine("Edit start");
}
public override void Changed(UITextView textView)
{
Console.WriteLine("Edit ");
}
}
6、 输入框键盘 样式类型 设置
UITextField fied = new UITextField();
fied.Frame = new RectangleF(50, 100, 300, 30);
fied.BorderStyle = UITextBorderStyle.Line;
this.View.AddSubview(fied);
fied.KeyboardType = UIKeyboardType.Url;//设置键盘类型
fied.KeyboardAppearance = UIKeyboardAppearance.Dark;// 设置键盘外观
fied.ReturnKeyType = UIReturnKeyType.Next;//设置键盘的Return键
7、显示键盘 时改变输入框的位置
UITextField fied = new UITextField();
public override void ViewDidLoad()
{
base.ViewDidLoad();
fied.Frame = new RectangleF(10, 500, 300, 30);
fied.BorderStyle = UITextBorderStyle.RoundedRect;
this.View.AddSubview(fied);
//显示键盘前 (事件)
kbdWillShow = UIKeyboard.Notifications.ObserveWillShow((s, e) => {
RectangleF kbdRounds = (RectangleF)e.FrameEnd;
RectangleF textFrame = (RectangleF)fied.Frame;
textFrame.Y -= kbdRounds.Height;
fied.Frame = textFrame;
});
//隐藏键盘后 (事件)
kbdDidHide = UIKeyboard.Notifications.ObserveDidHide((s, e) =>
{
RectangleF kbdRounds = (RectangleF)e.FrameEnd;
RectangleF textFrame = (RectangleF)fied.Frame;
textFrame.Y += kbdRounds.Height;
fied.Frame = textFrame;
});
//键盘上的return键 隐藏键盘 (事件)
fied.ShouldReturn=delegate(UITextField textField) {
return textField.ResignFirstResponder();
};
}
8、 为输入框键盘 添加工具栏
UITextField fied = new UITextField();
fied.Frame = new RectangleF(10, 200, 300, 30);
fied.BorderStyle = UITextBorderStyle.RoundedRect;//设置输入框边框
this.View.AddSubview(fied);
fied.KeyboardType = UIKeyboardType.EmailAddress;//设置键盘类型
//设施工具栏
UIToolbar bar = new UIToolbar();//实例化工具栏
bar.SizeToFit();
bar.BackgroundColor = UIColor.DarkGray;//设置背景
//实例化 工具栏 按钮
UIBarButtonItem item = new UIBarButtonItem("完成", UIBarButtonItemStyle.Done, (s, e) =>
{
//隐藏键盘
fied.ResignFirstResponder();
});
//为工具栏设置条目
bar.SetItems(new UIBarButtonItem[]{ item},true);
//为输入框 键盘添加工具栏
fied.InputAccessoryView = bar;
9、文本视图菜单 退出按钮
public override void ViewDidLoad()
{
tf.Frame = new RectangleF(20,10,300,100);
this.View.AddSubview(tf);
UIMenuItem item = new UIMenuItem("退出", new Selector("hide"));//添加菜单按钮
UIMenuController c = UIMenuController.SharedMenuController;
c.MenuItems = new UIMenuItem[] { item };
}
UITextView tf = new UITextView();
[Export("hide")]
public void hide() {
//退出键盘
tf.ResignFirstResponder();
}
10、触摸背景隐藏键盘
public override void ViewDidLoad()
{
tf.Frame = new RectangleF(10,50,300,30);
tf.BorderStyle = UITextBorderStyle.RoundedRect;
this.View.AddSubview(tf);
}
/// <summary>
///重写 ViewController 方法 TouchesBegan
/// </summary>
/// <param name="touches">Touches.</param>
/// <param name="evt">Evt.</param>
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
tf.ResignFirstResponder();
}
11、进度条控件 UIProgressView 先在添加一个lable lbl 和一个button sta
public override void ViewDidLoad()
{
//按钮的触摸事件
sta.TouchUpInside+=delegate {
sta.Enabled = false;
pv.Progress = 0f;
//创建一个新的任务 用来计算 进度条的值 并更新视图
Task.Factory.StartNew(this.Start);
};
/创建进度条 并添加到视图
pv = new UIProgressView(new RectangleF(60f,200f,200f,50f));
pv.Progress = 0f;
f = 1f / 10f;
this.View.AddSubview(pv);
}
UIProgressView pv;
float f = 0f;
public void Start() {
float p = 0f;
while (p<1f) {
Thread.Sleep(1000);
//调用主线程 修改视图
this.InvokeOnMainThread(delegate {
pv.Progress += this.f;
p = pv.Progress;
lbl.Text = Math.Round(pv.Progress, 2).ToString();
if (p >= 1f)
{
lbl.Text = "加载完成!";
sta.Enabled = true;
}
});
}
}
12、滚动视图
(1).
UIImageView img;
UIScrollView scr;
public override void ViewDidLoad()
{
base.ViewDidLoad();
img = new UIImageView(UIImage.FromFile("1.png"));
scr = new UIScrollView();
scr.Frame = new RectangleF(0,0,320,568);
scr.ContentSize = img.Image.Size; //滚动范围
scr.ContentOffset = new PointF(200f,50f); //目前滚动位置
scr.PagingEnabled = true; //可以整页翻动
scr.MinimumZoomScale=0.25f; //缩放最小比例
scr.MaximumZoomScale=2f; //放大的最大比例
//获取要缩放的图像视图
scr.ViewForZoomingInScrollView=delegate (UIScrollView scrll){
return this.img;
};
scr.ZoomScale = 1f; //设置变化比例
scr.IndicatorStyle = UIScrollViewIndicatorStyle.Black; //滚动指示器的风格设置
scr.AddSubview(img);
this.View.AddSubview(scr);
}
(2).
UIScrollView scr = new UIScrollView();
scr.Frame = new RectangleF(0,0,325,568);
scr.ContentSize = new SizeF(320,2000);
this.View.AddSubview(scr);
//滚动视图开始时调用
scr.Scrolled+=delegate {
Console.WriteLine("Start rolling");
};
//滚动视图结束时调用
scr.DecelerationEnded+=delegate {
Console.WriteLine("Start End");
};
float y = 10;
for (float i = 1; i < 21; i++) {
UILabel lab = new UILabel();
lab.Frame = new RectangleF(0,y,320,50);
lab.BackgroundColor = UIColor.Cyan;
lab.Text = String.Format("{0}",i);
scr.AddSubview(lab);
y += 100;
13、页面控件
UIImageView img1;
UIImageView img2;
UIImageView img3;
UIScrollView scr;
UIPageControl pagc;
public override void ViewDidLoad()
{
base.ViewDidLoad();
//添加滚动视图
scr = new UIScrollView();
scr.Frame = new RectangleF(0,0,320,495);
//滚动视图结束滚动时调用
scr.DecelerationEnded += this.scr_DecelerationEnded;
//添加页面
pagc = new UIPageControl();
pagc.Frame = new RectangleF(0,540,320,37);
pagc.Pages = 3; //设置页面数值 及页面小白点
//当页面数值发生改变时调用
pagc.ValueChanged += this.pagc_ValueChanged;
//滚动视图的滚动事件
scr.Scrolled+=delegate {
Console.WriteLine("Scolled!");
};
scr.PagingEnabled = true;
RectangleF rf = (RectangleF)scr.Frame;
scr.ContentSize = new SizeF(rf.Width*3,rf.Height);
//添加图像视图
img1 = new UIImageView();
img1.ContentMode = UIViewContentMode.ScaleAspectFit;
img1.Image = UIImage.FromFile("1.jpg");
rf.X += (float)this.scr.Frame.Width;
img2 = new UIImageView();
img2.ContentMode = UIViewContentMode.ScaleAspectFit;
img2.Image = UIImage.FromFile("2.jpg");
rf.X += (float)this.scr.Frame.Width;
img3 = new UIImageView();
img3.ContentMode = UIViewContentMode.ScaleAspectFit;
img3.Image = UIImage.FromFile("3.jpg");
rf.X += (float)this.scr.Frame.Width;
scr.AddSubview(img1);
scr.AddSubview(img2);
scr.AddSubview(img3);
this.View.AddSubview(scr);
this.View.AddSubview(pagc);
}
public void scr_DecelerationEnded(object sender,EventArgs e) {
float x1 = (float)this.img1.Frame.X;//获取图像img1 的x位置
float x2 = (float)this.img2.Frame.X;//获取图像img2的x位置
float x = (float)this.scr.ContentOffset.X;//获取视图对象scrolview scr 目前滚动的x位置
//判断x是否和x1相等
if (x == x1)
{
this.pagc.CurrentPage = 0;
}
else if (x == x2)
{
this.pagc.CurrentPage = 1;
}
else
{
this.pagc.CurrentPage = 2;
}
}
public void pagc_ValueChanged(object sender, EventArgs e)
{
PointF pf = (PointF)this.scr.ContentOffset;
switch (this.pagc.CurrentPage) {
case 0:
pf.X = (float)this.img1.Frame.X;
this.scr.SetContentOffset(pf,true);
break;
case 1:
pf.X = (float)this.img2.Frame.X;
this.scr.SetContentOffset(pf, true);
break;
case 2:
pf.X = (float)this.img3.Frame.X;
this.scr.SetContentOffset(pf, true);
break;
default:
break;
}
}
14、警告视图
UIAlertView a = new UIAlertView();
a.Title = "提示";
a.Message = "电量不足";
a.AddButton("确定");
a.Show();
以上是关于Xamarin.ios 基本控件的主要内容,如果未能解决你的问题,请参考以下文章
使用啥控件从相机捕获图像并在 ui 上显示为带有事件 xamarin ios 的缩略图
如何使用 Xamarin iOS PickView 获取价值?