关闭窗口时序列化

时间:2015-12-14 13:11:32

标签: c# wpf serialization event-handling code-behind

关闭窗口时尝试序列化。我收到此运行时错误:

  

发生了System.NotSupportedException类型的未处理异常   在PresentationFramework.dll

中      

附加信息:不支持给定路径的格式。

这发生在两个不同的实例中:

  1. 使用退出按钮(右上角)
  2. 关闭窗口
  3. Esc 键关闭窗口。
  4. 在每个相应的实例上。没有可用的源代码,在引发错误时突出显示了两个this.Close()(请参阅KeysDown中的Window.xaml.cs方法)。

    代码(缩减以适合此查询):

    Window.xaml.cs

    public partial class MainWindow : Window
    {
        public SessionManager SM { get; private set; }
    
        public MainWindow()
        {
            InitializeComponent();
            //code
        }
    
        /// <summary>
        /// Registered to MainWindow.KeysDown Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="kea"></param>
        private void KeysDown(object sender, KeyEventArgs kea)
        {
            switch (kea.Key)
            {
                case Key.Escape:
                    this.Close();
                    break;
            }
        }
    
        private void SaveSession(object sender, CancelEventArgs e)
        {
            SM.SaveSession();
        }
    }
    

    SessionManager.cs (我的类变量有问题,所以我没有省略它们):

    [Serializable]
        public class SessionManager : DependencyObject
        {
            [NonSerialized]
            public static readonly DependencyProperty currentSessionProperty =
                DependencyProperty.Register("currentSession", typeof(Session),
                typeof(MainWindow), new FrameworkPropertyMetadata(null));
            [NonSerialized]
            public static readonly DependencyProperty startTimeProperty =
                DependencyProperty.Register("strStartTime", typeof(string),
                typeof(MainWindow), new FrameworkPropertyMetadata(DateTime.Now.ToString()));
    
            private static string SM_FILE = "SessionManager.bin";
    
            /// <summary>
            /// Holds a list of all comments made within the session
            /// </summary>
            public List<string> Sessions { get; private set; }
            [NonSerialized]
            private DateTime _dtStartTime;
    
            public SessionManager()
            {
                //code
            }
    
    
            #region Methods
    
            public void SaveSession()
            {
                if (currentSession.timeEntries.Count != 0)
                {
                    currentSession.Save();
                }
                else
                {
                    Sessions.Remove(currentSession.Name);
                }
    
                //Serialize the SessionManager
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(SM_FILE, FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream,this);
                stream.Close();
            }
    
            public void OpenSession(int index)
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(Sessions[index], FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                currentSession=(Session)formatter.Deserialize(stream);
                stream.Close();
            }
            #endregion
        }
    

    Session.cs (我的类变量有问题,所以我没有省略它们):

    [Serializable]
    public class Session : DependencyObject
    {
        [NonSerialized]
        public static readonly DependencyProperty nameProperty =
            DependencyProperty.Register("name", typeof(string),
            typeof(Session), new FrameworkPropertyMetadata(string.Empty));
    
        [NonSerialized]
        public static readonly DependencyProperty totalTimeProperty =
            DependencyProperty.Register("totalTime", typeof(TimeSpan), typeof(Session),
            new PropertyMetadata(TimeSpan.Zero));
    
        public Session()
        {
            //code
        }
    
        public void Save()
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream= new FileStream(Name,FileMode.Create,FileAccess.Write,FileShare.None);
            formatter.Serialize(stream, this);
            stream.Close();
        }
    }
    

0 个答案:

没有答案