我有一個問題異步加載幾個UIWebViews到一個部分。我使用MonoTouch.Dialog來生成UI。我從博客加載數據並顯示10個項目,其中包含圖像和一些HTML文本。發生的事情是,帖子並未全部顯示出來,而顯示的帖子也無法正常顯示。下面是我在做什麼:Xamarin iOS - 多個動態大小的UIWebViews
public partial class BlogViewController : DialogViewController
{
private Section mainSection;
public BlogViewController() : base (UITableViewStyle.Grouped, null)
{
Root = new RootElement ("");
mainSection = new Section ("");
mainSection.Add(new ActivityElement());
Root.Add (mainSection);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
new Thread (new ThreadStart(PopulateBlog)).Start();
}
private void PopulateBlog()
{
var posts = service.GetPosts (currentOffset, 10);
InvokeOnMainThread (delegate {
foreach (var post in posts) {
//grab an appropriate image size
var altSize = post.photos [0].alt_sizes.Where (x => x.width < 401).OrderByDescending(x => x.width).FirstOrDefault();
if (altSize != null) {
var img = LoadImageFromUri(altSize.url);
//scale the image, not really important
var imageView = new UIImageView (new RectangleF (0, 0, screenWidth, height));
imageView.Image = img;
var content = new UIWebView();
//When the HTML finishes rendering figure out the size and add it to the section. Apparently can't figure the size ahead of time?
content.LoadFinished += (sender, e) =>
{
var contentHeight = Int32.Parse (content.EvaluateJavascript ("document.getElementById('content').offsetHeight;"));
content.Frame = new RectangleF (0, height + 10, screenWidth, contentHeight + 10);
//dynamically size this view to fit the content
var view = new UIView
(new RectangleF (0, 0,
screenWidth,
height + contentHeight));
view.AddSubview (content);
view.AddSubview (imageView);
//add the view to the Section which is later added to the Root
mainSection.Add(view);
};
var htmlString = @"some HTML here";
content.LoadHtmlString(someHtml);
content.ScrollView.ScrollEnabled = false;
content.ScrollView.Bounces = false;
}
}
});
Root.Reload(mainSection, UITableViewRowAnimation.None);
}
}
我猜一)LoadFinished事件不會以相同的順序,他們正在排隊的情況發生,而B)的Root.Reload得到之前,他們都叫火。我在Root.Reload之前嘗試使用Thread.Sleep旋轉,但LoadFinished事件甚至沒有被解僱。
我也嘗試將所有的UIView元素放入一個字典中,在它們全部填充之後添加,但似乎只要InvokeOnMainThread結束LoadFinished事件處理程序就再也不會被調用。
我絕對沒有結婚使用MT.Dialog,這正是我過去使用過的。用你的方法,我需要使用一個xib文件,或者我仍然可以在代碼中完成這一切嗎? –
你仍然可以做一切代碼。 – SKall
我會給它一個鏡頭 –