代码:
using System;
//
using MonoTouch.UIKit;
using System.Drawing;
namespace xxxx
{
public class LoadingOverlay : UIView
{
// control declarations
UIActivityIndicatorView activitySpinner;
UILabel loadingLabel;
public LoadingOverlay (RectangleF frame) : base (frame)
{
// configurable bits
BackgroundColor = UIColor.Black;
Alpha = 0.75f;
AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
float labelHeight = 22;
float labelWidth = Frame.Width - 20;
// derive the center x and y
float centerX = Frame.Width / 2;
float centerY = Frame.Height / 2;
// create the activity spinner, center it horizontall and put it 5 points above center x
activitySpinner = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.WhiteLarge);
activitySpinner.Frame = new RectangleF (
centerX - (activitySpinner.Frame.Width / 2),
centerY - activitySpinner.Frame.Height - 20,
activitySpinner.Frame.Width,
activitySpinner.Frame.Height);
activitySpinner.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
AddSubview (activitySpinner);
activitySpinner.StartAnimating ();
// create and configure the "Loading Data" label
loadingLabel = new UILabel (new RectangleF (
centerX - (labelWidth / 2),
centerY + 20,
labelWidth,
labelHeight
));
loadingLabel.BackgroundColor = UIColor.Clear;
loadingLabel.TextColor = UIColor.White;
loadingLabel.Text = "Loading Data...";
loadingLabel.TextAlignment = UITextAlignment.Center;
loadingLabel.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
AddSubview (loadingLabel);
}
/// <summary>
/// Fades out the control and then removes it from the super view
/// </summary>
public void Hide ()
{
UIView.Animate (
0.5, // duration
() => { Alpha = 0; },
() => { RemoveFromSuperview (); }
);
}
//...
}
}
调用:
/// <summary>
/// 刷新
/// </summary>
private void RefashBtnClick(object sender, System.EventArgs e)
{
//show loading...
xxxx.LoadingOverlay loadingOverlay = new xxxx.LoadingOverlay (this.View.Bounds);
this.View.AddSubview (loadingOverlay);
NSUserDefaults.StandardUserDefaults.RemoveObject (KeyCenter.Key_NewsList); //移除缓存
ListAT = ArticleBusiness.GetData ();
if (ListAT == null) {
Tools.Alert ("数据获取失败!");
return;
}
//*=================================>test
ListAT.Add (new ArticleModel () {
Articleid="132",
Articletitle = "i just test it for show",
ArticleContent = "http://xxxxxxxxx/article/padarticle.aspx?id=xxx",
Articlechannel = "80",
Articlecreatedate="xxxxx",
Articleimg="",
ArticleAuthor="jon",
Outline="jonjonjonjonjonjonjon"
});
DownLoadFile.BindTableView (this, TBView, ListAT, KeyCenter.Key_LoadedImgPath);
//hide loading...
loadingOverlay.Hide ();
}
结合上一篇文章,优化显示处理:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace GCForum
{
/// <summary>
/// 详细页面
/// </summary>
public partial class NewsDetail : BaseViewBodyController
{
private UIButton RefashBtn;
private string ArticleURL;
private xxxxx.LoadingOverlay loadingOverlay;
public NewsDetail () : base ("NewsDetail", null)
{
}
/// <summary>
/// 初始化详情页面
/// </summary>
/// <param name="url">url地址.</param>
public NewsDetail(string url)
{
this.ArticleURL = url;
this.ThisViewTitle = "前沿资讯";
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
//刷新
RefashBtn = chongqing.ControlCenter.RefreshButton ("");
RefashBtn.TouchUpInside += RefashBtnClick;
this.NavigationItem.RightBarButtonItem = new UIBarButtonItem (RefashBtn);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
ShowNewsInfo ();
}
/// <summary>
/// 刷新
/// </summary>
private void RefashBtnClick(object sender, System.EventArgs e)
{
ShowNewsInfo ();
}
private void ShowNewsInfo()
{
loadingOverlay = new xxxxx.LoadingOverlay (this.View.Bounds);
this.View.AddSubview (loadingOverlay);
System.Threading.Thread t = new System.Threading.Thread (GetNewsInfo);
t.Start ();
}
void GetNewsInfo()
{
System.Threading.Thread.Sleep (500);
using (var pool=new NSAutoreleasePool()) {
this.InvokeOnMainThread (delegate {
RectangleF rec = this.View.Bounds;
using (var webView = new UIWebView(new RectangleF(rec.Left,rec.Top,rec.Width,rec.Height-5))) {
//load data
var urlRequest = new NSUrlRequest (new NSUrl (ArticleURL));
webView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
webView.BackgroundColor = UIColor.White;
webView.LoadRequest (urlRequest);
this.View.Add (webView);
}
//hide loading...
loadingOverlay.Hide ();
});
}
}
//...
}
}
webveiw具体的隐藏还是应该在loadfinished事件中处理比较好:
webView.LoadFinished += delegate {
if (loadingOverlay != null) {
loadingOverlay.Hide ();
loadingOverlay.Dispose ();
loadingOverlay = null;
}
};