Weird blocks when adding Elements to MonoTouch Dialog RootElement after it is shown

五迷三道 提交于 2019-12-12 15:17:45

问题


In an app I am building I use the following pattern: If the user clicks on a hierarchical navigation element I open the next UIViewController immediately, and it takes care about loading its data itself and shows a loading spinner if it is going over the network.

Now most list views are created using MonoTouch. On one DialogViewController I have an issue with adding many element to the views RootElement after the screen has already been show.

At first the StringElements appear fine on screen, but if you scroll quickly up and down the text on each line becomes a block:

Code to reproduce the issue:

        var root = new RootElement("Root");
        var dvc = new DialogViewController(root) { AutoHideSearch = true, EnableSearch = true };

        nav.PushViewController(dvc, true);

        Task.Factory.StartNew(() => {
                Thread.Sleep(TimeSpan.FromSeconds(1));
            UIApplication.SharedApplication.InvokeOnMainThread(() => {
                var sec = new Section();

                for (int i = 0; i < 100; i++)
                {
                    var el = new StyledStringElement("Test", "Test", UITableViewCellStyle.Value1);
                    sec.Add(el);
                }

                root.Add(sec);
            });
        });

Interestingly only the string on the left looks like a block, while the one on the right is fine. On a side note, in their MWC (demo) app, Xamarin created a new RootElement to repopulate the twitter list as well.


回答1:


There does not seem to be a reload of the data source called on the main thread to correctly render the new values:

var dvc = new DialogViewController(new RootElement(""));
_root = new UINavigationController();
_root.PushViewController(dvc, true);
_window.RootViewController = _root;

Task.Factory.StartNew(() => {
    Thread.Sleep(TimeSpan.FromSeconds(1));
    var section = new Section("");
        foreach (var i in Enumerable.Range(1, 100)) {
            section.Add(new StyledStringElement(i.ToString(), "cell", UITableViewCellStyle.Value1));
        }

    dvc.Root.Add(section);
    BeginInvokeOnMainThread(() => {
        dvc.ReloadData();
    });
});



回答2:


I exactly have the same issue when I'm rendering dynamically the element in an BeginInvokeOnMainThread statement If I do load my elements in the constructor, the UI is blocked but the elements will render well.

and thanks for the tip dvc.ReloadData will work as well for me



来源:https://stackoverflow.com/questions/9456650/weird-blocks-when-adding-elements-to-monotouch-dialog-rootelement-after-it-is-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!