Xamarin iOS - 多个动态大小的UIWebViews

时间:2014-02-19 22:40:01

标签: ios uiwebview xamarin

我遇到一个问题异步将几个UIWebViews加载到一个Section中。我正在使用MonoTouch.Dialog来生成UI。我正在从博客加载数据并显示10个项目,其中包括图像和一些HTML文本。发生的事情是这些帖子并非全部显示,而且显示的帖子都不正常。这就是我正在做的事情:

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);
    }


}

我猜测A)LoadFinished事件没有按照它们排队的顺序发生,并且B)Root.Reload在它们全部触发之前被调用。我尝试在Root.Reload之前使用Thread.Sleep进行旋转,但随后LoadFinished事件甚至不会被触发。

我还尝试将所有UIView元素放在一个字典中,在它们全部填充后添加,但似乎只要InvokeOnMainThread结束,LoadFinished事件处理程序就不会再被调用。

1 个答案:

答案 0 :(得分:0)

你必须使用MT.Dialog吗?我个人会尝试使用UITableView。加载Web视图内容,确定其大小(可能使用sizeThatFits?)并将其推送到正确位置的数据源。然后返回UITableViewDelegate的heightForRowAtIndexPath调用的大小。 UITableView应该处理其余的事情。