我有一个WPF应用程序,它挂钩到我也在编写的DLL中。 DLL扫描第三方应用程序的数据文件夹并处理其中的所有数据解释。 WPF将提供一个很好的GUI;我将它们分开了,因为将来可能还需要为它编写命令行界面。
扫描数据文件夹需要一些时间,因此我想保存对象的状态(存储库),并在数据文件夹被扫描并保持相同的“上次修改”状态时打开它。我已将Repository对象标记为[Serializable]但是当我尝试保存状态(来自WPF或DLL)时,我得到一个异常,即WPF MainWindow不是[Serializable]。
如果我读取了创建的.dat文件,那么它确实有一些(不确定的话)全部信息。
我不明白为什么会尝试保存有关WPF窗口的任何信息。我试图将窗口标记为[Serializable]只是为了尝试但该类不允许它。在网上搜索让我看着AppDomains,因为我正在加载DLL和应用程序,但这有点过头了。以下是我目前正在尝试实施它的方式。编辑:对序列化的调用位于WPF代码的底部。
我应该提一下,我在WPF命名空间中创建了一个内联类,并且能够成功地将其序列化为文件。
感谢任何帮助。
这是DLL:
namespace HPOO_XML_Parser
{
[Serializable]
public class HPOORepository
{
string _version;
string _path;
string _library;
string _uuid;
[NonSerialized]
BackgroundWorker bWorker = new BackgroundWorker();
[NonSerialized]
XElement _xmlRepo;
int _nodeCount;
List<Node> nodes;
...
Rest of properties and methods
}
WPF称之为
namespace HPOO_Repository_Scanner
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
HPOORepository repo;
public MainWindow()
{
InitializeComponent();
StatusVisibility(); // Simply hides progress bar/cancel button
}
private void OpenRepository(object sender, RoutedEventArgs e)
{
string ooHome;
using (FolderBrowserDialog browser = new FolderBrowserDialog())
{
ooHome = Environment.GetEnvironmentVariable("ICONCLUDE_HOME");
if (ooHome != null)
{
browser.SelectedPath = ooHome;
}
browser.ShowNewFolderButton = false;
browser.Description = "Select a repository to open...";
if (browser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
repo = new HPOORepository(browser.SelectedPath);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message, "Invalid Repository Selected", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
repo.ProgressChanged += new ProgressChangedEventHandler(repo_ProgressChanged);
prgStatus.Maximum = 100;
repo.ReadRepository();
}
}
private void mnuSave_Click(object sender, RoutedEventArgs e)
{
SaveRepo(repo);
}
public void SaveRepo(object repository)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = new FileStream("test" + ".dat",
FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, repository);
}
}
编辑:最后是例外:
System.Runtime.Serialization.SerializationException was unhandled
Message=Type 'HPOO_Repository_Scanner.MainWindow' in Assembly 'HPOO Repository Scanner, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
Source=mscorlib
StackTrace:
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
at HPOO_Repository_Scanner.MainWindow.SaveRepo(Object repository) in \\tsclient\D\Dropbox\_Work\Visual Studio Projects\HPOO Repository Scanner\HPOO Repository Scanner\HPOO Repository Scanner\MainWindow.xaml.cs:line 179
at HPOO_Repository_Scanner.MainWindow.mnuSave_Click(Object sender, RoutedEventArgs e) in \\tsclient\D\Dropbox\_Work\Visual Studio Projects\HPOO Repository Scanner\HPOO Repository Scanner\HPOO Repository Scanner\MainWindow.xaml.cs:line 168
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at HPOO_Repository_Scanner.App.Main() in \\tsclient\D\Dropbox\_Work\Visual Studio Projects\HPOO Repository Scanner\HPOO Repository Scanner\HPOO Repository Scanner\obj\x86\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
答案 0 :(得分:0)
我假设您正在使用XML序列化程序。您应该显示序列化代码。此外,在获取异常时,显示异常和堆栈跟踪。使用此信息确定上下文要容易得多。
如果是这样,您必须使用[XmlIgnore]而不是[NonSerialized]属性。
答案 1 :(得分:0)
我不得不从我的repo对象中分离ProgressChanged事件处理程序。
它是在创建对象时附加的
repo.ProgressChanged += new ProgressChangedEventHandler(repo_ProgressChanged);
现在在保存对象状态之前删除了
private void mnuSave_Click(object sender, RoutedEventArgs e)
{
repo.ProgressChanged -= repo_ProgressChanged;
SaveRepo(repo);
}
希望能帮助遇到同样问题的其他人。