序列化Button类 [英] Serializing Button class

查看:127
本文介绍了序列化Button类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化在运行时创建的按钮。

但它引发了一个关于serializable属性的异常。

还有其他方式,我怎么样可以保存Button的状态吗?

I''m trying to serialize a button created at runtime.
But it throws an exception regarding the serializable attribute.
Is there any other way, how I can save the state of the Button?

推荐答案

嗯,我可以发布更多细节吗?我只是非常熟悉XML序列化,所以如果你不这样做,我不知道我能帮到多少,但我会尝试。


需要更多信息...代码片段和异常文本会很好。
Hmm, I can you post more details? I''m only really familiar with XML serialization, so if you''re not doing that I''m not sure how much I can help, but I''ll try.

Need more information though... a code snippet and the exception text would do nicely.


实际上我正在使用二进制序列化。

忘掉了序列化的类型。我面临的真正问题是使Button类可序列化。


场景是,我在运行时创建一个按钮。为了保存它的状态,我必须序列化它。代码是这样的....

Actually i''m using Binary serialization.
Forget about the type of serialization. The real problem i''m facing is to make the Button class serializable.

The scenario is that, i''m creating a button at runtime. Inorder to save its state I have to serialize it. The code goes like this....

展开 | 选择 | Wrap | 行号


嗯,错误消息未标记为序列化"似乎是关键。当您为XML编写可序列化的东西时,您需要[Serializable]标签(或类似的东西),所以也许就是它。


我刚做了一些测试,看来你需要[Serializable]标签以这种方式序列化事物。按钮必须没有。我确实尝试将标签添加到一个继承自按钮的新类,但这也不起作用。


我不知道你怎么能这样做...也许你可以创建一个包装类,它暴露了一个字符串属性,它是一个按钮的字符串表示(比如,name =" blah" ;; size =" blah"),当设置时,它会实例化正确的按钮。尽管如此,还是有更好的方法!我会看看我是否可以解决问题,或者其他人可能会有一些见解。谷歌没有太大的帮助,但我可能没有正确搜索。
Hmm, the error message "not marked as serializable" seems to be the key. When you make something serializable for XML, you need the [Serializable] tag (or something similar) so perhaps that''s it.

I just did some tests and it seems you need the [Serializable] tag to serialize things in this fashion. Button must not have that. I did try adding the tag to a new class that inherits from button, but that doesn''t work either.

I''m not sure how you can do this... maybe you could make a wrapper class that exposed a string property that was a string representation of a button (like, name="blah";size="blah") and when set, it instantiated the correct button. There''s gotta be a better way to do it though! I''ll see if I can figure something out, or maybe someone else will have some insight. Google wasn''t terribly helpful, but I might not have searched correctly.


这篇关于序列化Button类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆