[VB.NET]序列化的问题

前端之家收集整理的这篇文章主要介绍了[VB.NET]序列化的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

序列化的问题
我的程序有个主窗口,一个子窗口.点击主窗口则显示子窗口.在子窗口中实现图片选择,点击确定后进行序列化.子窗口有个显示按钮,点击后显示保存过的图片.在主窗口不关闭的情况下,能正常序列化,反序列化,当主窗口关闭后,再打开,在子窗口中点击显示,发现反序列化后什么结果也没得到.(保存的是图片路径).不知道为什么会这样,为什么和主窗口有关系.
程序如下:
Imports System
Imports System.IO
Imports System.Collections
Imports System.Xml.Serialization

Public Class shzh
Inherits System.Windows.Forms.Form
Public Class xlh_shzh 需要序列化的类
Public hyldPic As String
Public hyldflag As Int16
End Class

Dim mhyldlj As String
Dim hyldflag As Int16

选择图片
Private Sub shz_hyldPicBt_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles shz_hyldPicBt.Click
Dim m_opfileDlg As New OpenFileDialog
Dim jpg,bmp,gif,pic,wmf,jpeg As String 所选文件不为这些格式则告警
jpg = *.[jJ][pP][gG] 后缀为JPG或者jpg[因文件后缀不区分大小写]
bmp = *.[bB][mM][pP]
gif = *.[Gg][Ii][Ff]
pic = *.[Pp][Ii][Cc]
wmf = *.[Ww][Mm][Ff]
jpeg = *.[jJ][pP][Ee][gG]
hyldPicYl.SizeMode = PictureBoxSizeMode.StretchImage
m_opfileDlg.AddExtension = True
m_opfileDlg.Filter() = 所有文件|*.*
m_opfileDlg.CheckFileExists = True
m_opfileDlg.CheckPathExists = True
m_opfileDlg.ShowDialog()

If m_opfileDlg.FileName() Like jpg Or m_opfileDlg.FileName() Like bmp Or m_opfileDlg.FileName() Like gif Or m_opfileDlg.FileName() Like pic Or m_opfileDlg.FileName() Like wmf Or m_opfileDlg.FileName() Like jpeg Then
If m_opfileDlg.ShowDialog().OK Then
mhyldlj = m_opfileDlg.FileName()
MsgBox(mhyldlj)
hyldPicYl.Image = System.Drawing.Bitmap.FromFile(mhyldlj)
hyldPicYl.Show()
hyldflag = 2
End If
m_opfileDlg.Dispose()
Else
MsgBox( 所选文件非法! )
End If
End Sub
显示图片
Private Sub yhldCancelBt_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles yhldCancelBt.Click
Dim m_xlh_shzh As New xlh_shzh

Dim myObject As XmlSerializer
Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(xlh_shzh))
Dim myFileStream As FileStream = _
New FileStream( KZS_ShZh.xml,FileMode.Open)
Dim reader As New System.XML.XmlTextReader(myFileStream)
m_xlh_shzh = CType(mySerializer.Deserialize(reader),xlh_shzh)
myFileStream.Close()

hyldPicYl.SizeMode = PictureBoxSizeMode.StretchImage
hyldPicYl.Image = Image.FromFile(m_xlh_shzh.hyldPic)
hyldPicYl.Show()
MsgBox(m_xlh_shzh.hyldPic)
End Sub
选择图片后点确定
Private Sub hyldSetBt_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles hyldSetBt.Click
Dim m_xlh_shzh As New xlh_shzh
m_xlh_shzh.hyldPic = mhyldlj
m_xlh_shzh.hyldflag = 2
Dim x As XmlSerializer = New XmlSerializer(GetType(xlh_shzh))
Dim writer As TextWriter = New StreamWriter( KZS_ShZh.xml )
x.Serialize(writer,m_xlh_shzh)
writer.Close()
End Sub
End Class
请各位大狭帮忙看看,我已经研究好几天了,也找不到原因
__________________________________________________________________________
( KZS_ShZh.xml ) 改为 ( d:/KZS_ShZh.xml ) 试试。
__________________________________________________________________________
帮顶...
__________________________________________________________________________
个人的一点愚昧的意见:
因为你的集合对象在 windows froms 数据绑定的时候产生事件,当你序列化的时候,处理事件的对象或者窗体也会被串行化(这个可能就你预想不到的后果),就回引发对象图:解决方法应该在事件添加属性标记,表明的yhldCancelB 不应该被序列化,可以声明 . 还有一种办法:可惜VB.net不支持 就是用C#[field:NonSerializable] 替代那个事件 __________________________________________________________________________ 谢谢sx_lxh(路漫漫),果真是这个问题.20分已发出.同时也十分感谢hhh21(呖呖),分析的很深入,只恨出的问题太低级,呵呵,没办法,我以前很少用VB.NET __________________________________________________________________________
原文链接:https://www.f2er.com/vb/263486.html

猜你在找的VB相关文章