从 XAML 加载元素

发布于 2025-01-01 21:02:50 字数 1061 浏览 0 评论 0原文

我有一个像这样的自定义 XAML 用户控件:

<UserControl x:Class="CheckPoint.Modules.Beach.Beach_Shape"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Polygon Name="Shape"></Polygon>
    </Grid>
</UserControl>

我希望使用 XamlWrite.Save 对其进行 xaml 序列化,然后使用 XamlReader.Load 重新加载它。

XmlReader reader = XmlReader.Create(new StringReader(xml));
UserControl uc=(UserControl)XamlReader.Load(reader);
myGrid.Children.Add(uc);

“uc”在 myGrid 上正确可视化,但“uc”对象在逻辑上不正确,因为 Shape 元素未正确加载,例如它没有设置背景、描边或点,即使它位于 xaml 中。

我尝试重新加载它,

Shape=myGrid.Findname("Shape");

但它也不起作用。

那么,我的错误在哪里呢?

I have a custom XAML User Control like this:

<UserControl x:Class="CheckPoint.Modules.Beach.Beach_Shape"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Polygon Name="Shape"></Polygon>
    </Grid>
</UserControl>

I want xaml serialize it with XamlWrite.Save and then reload it with XamlReader.Load.

XmlReader reader = XmlReader.Create(new StringReader(xml));
UserControl uc=(UserControl)XamlReader.Load(reader);
myGrid.Children.Add(uc);

"uc" is correctly visualized on myGrid, but "uc" Object is not logical correct, becouse the Shape element is not correctly loaded, for example it has not Background, Stroke or Points setted even though it are in xaml.

I try to reload it with

Shape=myGrid.Findname("Shape");

but it doesn't work too.

So, where is my mistake?

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

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

发布评论

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

评论(1

无声无音无过去 2025-01-08 21:02:51

这可能是您问题的答案(来自 MSDN 文章 XamlWriter.Save 的序列化限制< /a>):

通过调用 Save 序列化的基本原理是
结果将是正在序列化的对象的表示,位于
运行时。原始 XAML 文件的许多设计时属性可能会
在 XAML 加载时已经被优化或丢失
内存中的对象,并且在调用 Save to 时不会保留
连载。序列化结果是
构造了应用程序的逻辑树,但不一定是
生成它的原始 XAML。这些问题都让人非常
很难将 Save 序列化用作扩展 XAML 的一部分
设计表面。

This might be an answer for your question (from MSDN article Serialization Limitations of XamlWriter.Save):

The basic philosophy of what is serialized by a call to Save is that
the result will be a representation of the object being serialized, at
run-time. Many design-time properties of the original XAML file may
already be optimized or lost by the time that the XAML is loaded as
in-memory objects, and are not preserved when you call Save to
serialize. The serialized result is an effective representation of the
constructed logical tree of the application, but not necessarily of
the original XAML that produced it. These issues make it extremely
difficult to use the Save serialization as part of an extensive XAML
design surface.

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