是否可以将控件(面板)存储为对象,序列化并将其存储为文件?

发布于 2024-08-27 17:47:34 字数 750 浏览 4 评论 0原文

主题说明了一切。

使用 Compact Framework C#,

我将从 url 下载的一些图像平铺(顺序/顺序很重要)到面板中(每个图像都是一个 PictureBox)。 这可能是一个巨大的过程,并且可能需要一些时间。

因此,我只希望用户下载图像并将其平铺一次。因此,下次用户使用 Tile 应用程序时,第一次创建的面板已存储在文件中并从该文件加载。

所以我想要的是一种将面板存储为文件的方法。

这是可能的吗,还是你认为我应该用另一种方式来做?

我尝试过这样的事情:

            BinaryWriter panelStorage = new BinaryWriter(new FileStream("imagePanel.panel", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
            Byte[] bImageObject = new Byte[20000];
            bImageObject = (byte[])(object)this.imagePanel;
            panelStorage .Write(bMapObject);
            panelStorage .Close();

但是转换不是很合法:P

“InvalidCastException”

任何人都可以帮助我解决这个问题吗?

先感谢您!

The topic says it all.

Using Compact Framework C#

I'm tiling (order/sequence is important) some images that i download from an url, into a Panel(each image is a PictureBox).
This can be a huge process, and may take some time.

Therefor i only want the user to download the images and tile them once. So the next time the user uses the Tile Application, the Panel that was created the first time is already stored in a file and is loaded from that file.

So what i want is a method to store a Panel as a file.

Is this possible, or do you think i should do it another way?

I've tried something like this:

            BinaryWriter panelStorage = new BinaryWriter(new FileStream("imagePanel.panel", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
            Byte[] bImageObject = new Byte[20000];
            bImageObject = (byte[])(object)this.imagePanel;
            panelStorage .Write(bMapObject);
            panelStorage .Close();

But the casting was not very legal :P

"InvalidCastException"

Can anyone help me with this problem?

Thank you in advance!

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

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

发布评论

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

评论(3

乖不如嘢 2024-09-03 17:47:34

如果我是你,我就不会序列化该控件。相反,我会序列化进入控件的数据。有几个原因。

首先,它更容易。您可以将要放入的任何数据(图像)放入自定义的 MyPanel 类或其他类中。也在那里存储有关如何操作它们的信息,然后您可以序列化该类,就完成了。

第二个原因是,如果您想在以后更改数据的显示或使用方式,您有更多的自由。您可以通过其他方式使用它,或者与其他应用程序和控件一起使用,而无需您付出任何实际努力。

I wouldn't serialize the control if I were you. I'd instead serialise the data that goes into the control. There are a couple of reasons.

Firstly, it's easier. You can take whatever data you are putting in (images) and put them into a custom MyPanel class or whatever. Store information about how they are manipulated in there too, and then you can serialise that class, and it's done.

The second reason is that if you want to change how the data is displayed or used at a later date, you have a lot more freedom. You can use it in other ways, or with other applications and controls without any real effort on your part.

囚我心虐我身 2024-09-03 17:47:34

是的,你的代码会抛出,你不能将 Control 对象强制转换为 byte[]。这种转换需要序列化,即由 BinaryFormatter 执行的类型。问题是:Control 类及其后代不可序列化,它们没有 [Serialized] 属性。

Control类是一个复杂的母亲,它有数百个私有字段。其中许多字段具有在运行时生成的值,通常是通过与本机 Windows 窗口互操作来生成的。虽然序列化它们在某种程度上是可能的,但反序列化则不然。重建 Control 对象也需要重建本机窗口。它本身有很多状态,这种状态是通过向它发送一堆消息来设置的。重新生成这些消息是不切实际的。

这不会给你留下很多有吸引力的选择。序列化 UI 是可能的,Windows 窗体设计器和 WPF XAML 可以做到这一点。但这些都是非常大的代码块,不容易嵌入到您的程序中。

目标放低一些,不要尝试序列化整个控件。只需它的一些属性,例如“大小”和“位置”,您就可以尝试使其工作。

Yes, your code will throw, you cannot cast a Control object to byte[]. That kind of conversion requires serialization, the kind performed by BinaryFormatter. Trouble is: the Control class and its decedents are not serializable, they don't have the [Serializable] attribute.

The Control class is a complicated mother, it has hundreds of private fields. A lot of those fields have values that are generated at runtime, often by interoperating with the native Windows window. While serializing them is somewhat possible, deserializing is not. Reconstructing the Control object requires the native window to be reconstructed as well. And it has a lot of state itself, the kind that's set by sending it a bunch of message. Regenerating those messages isn't practical.

This doesn't leave you with a lot of attractive options. Serializing a UI is possible, the Windows Forms designer and WPF XAML does it. But these are very large chunks of code, not easily embedded in your program.

Aim lower, don't try to serialize the entire control. Just some of its properties, like Size and Location, and you'll have a shot at making this work.

や莫失莫忘 2024-09-03 17:47:34

ResX 文件序列化控件的多个属性。也许你可以使用那些?

ResX files serialize several properties of the control. Perhaps you can use those?

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