开发者

Adding controls causes silverlight to hang

I have a very strange issue that I'm finding it extremely hard to debug, thus I once more turn to the SO community. :-)

First a bit on my setup (note! I'm new to silverlight, just learning by doing, so my entire premise might be wrong!):

I have some root nodes (forms) under which are some leaves (questions). Now, I simply want to show / hide the questions depending on which form is currently selected - fairly easy.

I do the following:

  • A number of controls are added to a LayoutControl (I'm using the DeveloperExpress components)
  • When you select another form, the created controls are saved in a list on the previously selected form
  • When you select a form, the list of controls is iterated through, and each element is added to my layoutcontrol again

When I save the elements in the list on a Form I make sure to first .Remove() them from their parent to make sure there are no issues with that.

And, this works.

If I have 1 or controls.

If I have MORE than that, everything is added as usual, no exception is thrown - but Silverlight apparently does an infinite loop somehow? No matter if I run it in IE or Chrome, it just crashes the browser! (Or, I guess, the browser plugin).

I've tried pausing the debugger during this to see where the problem is, but it just stops in "external code". I don't think a code sample will do much good, but here's the two methods that save and load the elements: (Note: AddControl is a method I've defined - it wraps the element in another control to provide a bit of functionality. Likewise .MyChildren removes the control from the wrapper using .Remove() and returns it)

    private void LoadElementsFromCurrentForm()
    {
        foreach (var child in _currentForm.Elements)
        {
            layoutControl1.AddControl(child);
        }
    }

    private void SetElementsOnCurrentForm()
    {
        _currentFo开发者_Python百科rm.Elements.Clear();
        foreach (var child in layoutControl1.MyChildren)
        {
            _currentForm.Elements.Add(child);
        }
    }

How do I even go about debugging this!? I don't know if it matters, but all these controls have a unique name as well.

Regards

Søren


To take full advantage of Silverlight you should try to use XAML to do the markup of design. If you want to make a list of questions use e Listbox or ItemsControl. Then you can style each item in the list to "look right". If you bind the listbox to ObservableCollection you just add or remove in the list and the gui will be updated according to whats in your list.

Take a look at MVVM which works really good with Silverlight.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜