我有一个问题异步加载几个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
我会给它一个镜头 –