Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关闭窗口时序列化_C#_Wpf_Serialization_Event Handling_Code Behind - Fatal编程技术网

C# 关闭窗口时序列化

C# 关闭窗口时序列化,c#,wpf,serialization,event-handling,code-behind,C#,Wpf,Serialization,Event Handling,Code Behind,在关闭窗口时尝试序列化。我得到以下运行时错误: 发生类型为System.NotSupportedException的未处理异常 在PresentationFramework.dll中 其他信息:不支持给定路径的格式 这在两种不同的情况下发生: 使用退出按钮(右上角)关闭车窗 按Esc键关闭车窗 在每一个实例上,一个。没有可用的源代码,出现错误时突出显示了两个this.Close()(请参阅Window.xaml.cs中的KeysDown方法) 代码(缩减以适应此查询): Window.xaml.

在关闭窗口时尝试序列化。我得到以下运行时错误:

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

其他信息:不支持给定路径的格式

这在两种不同的情况下发生:

  • 使用退出按钮(右上角)关闭车窗
  • 按Esc键关闭车窗 在每一个实例上,一个。没有可用的源代码,出现错误时突出显示了两个
    this.Close()
    (请参阅
    Window.xaml.cs
    中的
    KeysDown
    方法)

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

    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();
        }
    }
    

    您的代码中是否有使用“+=”注册的事件?我认为从DependencyObject派生的类无法序列化。事件结束应该是您所需要的。“不支持给定路径的格式。”--这意味着您试图在
    Save
    方法中写入的文件路径存在某种问题:可能是无效的文件名字符或权限问题。我怀疑这就是您的问题所在
    DateTime.Now.ToString()
    可能会返回一个包含
    的字符串,该字符串对于文件名无效。
    [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
        }
    
    [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();
        }
    }