开发者

XamlWriter/Reader -> multiple constructor calls resulting in duplicate elements

i'm kind of stuck here. i'm using a dragndrop-library whic开发者_开发知识库h serializes the dragged UIElements via XamlWriter.Save().

now i'm trying to dragndrop some instances of a class extending Grid. in the constructor i push some elements into the visual tree. the constructor gets called when the object is deserialized and the elements get added again (and again and again depending on how often i dragndrop). sadly Children.Count tells me there are no child elements if i retrieve the value from within the constructor. if i retrieve it from outside (by myGrid.Children.Count) it gives me the higher (unwanted) amount.

is there any other function i should use to initialize the visuals? or any other way to prevent those duplicates?

thanks and cheers


took a while, but i seem to have found a solution.
now i'm able to create a base class already containing the visual elements all the subclasses need. and they are parsable via XamlWriter/Reader without duplicates. whew.

1) extend UserControl (dont Add->UserControl but Add->Class and inherit manually)
2) put the standard InitializeControl(); into the constructor
3) implement InitializeControl() and put the layouting, child adding and whatever in there
4) xamlwrite/xamlread/inherit like crazy

hope it will be helpful to someone and that i havent overseen the unforeseen..

-- edit: of course. there will be duplicates but we cant see them. after the XamlReader is through there are the UIElements created by my InitializeComponent() AND the ones getting xaml-parsed after that. any references in the code regard the code-created controls which are NOT show :/

sigh.

one far from perfect way around this is to put a switch into an Initialized event handler, like so: if(HasContent)
Initialize();
else
Reinitialize();
Multiinitialize();

where the Initialize() would add the UIElements to the visual tree, the Reinitialize() just finds the right references (by control = FindName("controlName")) and the Multiinitialze() recreates the event handlers and what else gets lost on the way throug the XamlReader.

well, now you can instantiate the custom inherited UserControl in xaml but there is no easy way to set attributes. that is because setting the attributes happens before the Initialized-event and there are NullPointerExceptions awaiting. you could work around that with bindings i guess.

but there HAS to be an easier way. please let me know!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜