XamlWriter/Reader ->多次构造函数调用导致重复元素

发布于 2024-09-08 22:10:12 字数 320 浏览 1 评论 0原文

我有点被困在这里了。我正在使用一个拖放库,它通过 XamlWriter.Save() 序列化拖动的 UIElements。

现在我正在尝试拖放扩展网格的类的一些实例。在构造函数中,我将一些元素推入可视化树中。 当对象被反序列化并且元素被再次添加(并且一次又一次取决于我拖放的频率)时,构造函数被调用。遗憾的是 Children.Count 告诉我,如果我从构造函数中检索值,则没有子元素。如果我从外部检索它(通过 myGrid.Children.Count),它会给我更高的(不需要的)金额。

我还应该使用其他函数来初始化视觉效果吗?或者任何其他方法来防止这些重复?

谢谢和欢呼

i'm kind of stuck here. i'm using a dragndrop-library which 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-09-15 22:10:12

花了一段时间,但我似乎找到了解决方案。
现在我可以创建一个已经包含所有子类所需的视觉元素的基类。并且它们可以通过 XamlWriter/Reader 进行解析,而不会重复。哇。

1)扩展UserControl(不要Add->UserControl,而是Add->Class并手动继承)
2)放入标准的InitializeControl();进入构造函数
3) 实现InitializeControl() 并将布局、子元素添加等放入其中
4) xamlwrite/xamlread/inherit 疯狂地

希望它对某人有帮助,并且我没有监督不可预见的情况..

--
编辑:当然。
会有重复项,但我们看不到它们。
XamlReader 完成后,会出现由我的 InitializeComponent() 创建的 UIElement 以及之后进行 xaml 解析的 UIElement。代码中的任何引用都涉及代码创建的控件,这些控件不显示:/

叹息。

解决这个问题的一种远非完美的方法是将开关放入初始化事件处理程序中,如下所示:
if(有内容)
初始化();
否则
重新初始化();
多重初始化();

其中 Initialize() 会将 UIElement 添加到可视化树中,Reinitialize() 只是找到正确的引用(通过 control = FindName("controlName")),而 Multiinitialze() 重新创建事件处理程序以及在通过 XamlReader 的过程中还会丢失什么。

好吧,现在您可以在 xaml 中实例化自定义继承的 UserControl,但没有简单的方法来设置属性。这是因为设置属性发生在初始化事件之前,并且有 NullPointerExceptions 等待。我猜你可以用绑定来解决这个问题。

但必须有一种更简单的方法。请告诉我!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文