public FileOperationResult ObservedSave(NamedFileOperationDelegate saveFileAs, string fileName, string message, FileErrorPolicy policy)
        {
            Debug.Assert(IsValidFileName(fileName));
            try
            {
                saveFileAs(fileName);
                return(FileOperationResult.OK);
            }
            catch (Exception e)
            {
                switch (policy)
                {
                case FileErrorPolicy.Inform:
                    using (SaveErrorInformDialog informDialog = new SaveErrorInformDialog(fileName, message, "Error while saving", e))
                    {
                        informDialog.ShowDialog();
                    }
                    break;

                case FileErrorPolicy.ProvideAlternative:
restartlabel:
                    using (SaveErrorChooseDialog chooseDialog = new SaveErrorChooseDialog(fileName, message, "Error while saving", e, true))
                    {
                        switch (chooseDialog.ShowDialog())
                        {
                        case DialogResult.OK:
                            using (SaveFileDialog fdiag = new SaveFileDialog())
                            {
                                fdiag.OverwritePrompt = true;
                                fdiag.AddExtension    = true;
                                fdiag.CheckFileExists = false;
                                fdiag.CheckPathExists = true;
                                fdiag.Title           = "Choose alternate file name";
                                fdiag.FileName        = fileName;
                                if (fdiag.ShowDialog() == DialogResult.OK)
                                {
                                    return(ObservedSave(saveFileAs, fdiag.FileName, message, policy));
                                }
                                else
                                {
                                    goto restartlabel;
                                }
                            }

                        case DialogResult.Retry:
                            return(ObservedSave(saveFileAs, fileName, message, policy));

                        case DialogResult.Ignore:
                            return(FileOperationResult.Failed);
                        }
                    }
                    break;
                }
            }
            return(FileOperationResult.Failed);
        }
        public FileOperationResult ObservedLoad(FileOperationDelegate loadFileDelegate, string fileName, string message, FileErrorPolicy policy)
        {
            Debug.Assert(IsValidFileName(fileName));
            //Eventually do the load file action.
            try
            {
                loadFileDelegate();                //通过这个委托来真正最后调用实际的方法,所有的ObservedLoad方法
                //无论是NamedFileOperation还是FileOperation最终都会通过这个委托来调用所对应的方�?
                return(FileOperationResult.OK);
            }
            catch (Exception e)
            {
                switch (policy)
                {
                case FileErrorPolicy.Inform:
                    using (SaveErrorInformDialog informDialog = new SaveErrorInformDialog(fileName, message, "Error while loading", e))
                    {
                        informDialog.ShowDialog();
                    }
                    break;

                case FileErrorPolicy.ProvideAlternative:
                    using (SaveErrorChooseDialog chooseDialog = new SaveErrorChooseDialog(fileName, message, "Error while loading", e, false))
                    {
                        switch (chooseDialog.ShowDialog())
                        {
                        case DialogResult.OK:                                         // choose location (never happens here)
                            break;

                        case DialogResult.Retry:
                            return(ObservedLoad(loadFileDelegate, fileName, message, policy));

                        case DialogResult.Ignore:
                            return(FileOperationResult.Failed);
                        }
                    }
                    break;
                }
            }
            return(FileOperationResult.Failed);
        }
        //定义一些受保护的加载文件和保存文件的方�?具体执行的函数是通过委托实例来执行的.
        #region ObservedSaveOrLoad Methods

        public FileOperationResult ObservedSave(FileOperationDelegate saveFile, string fileName, string message, FileErrorPolicy policy)
        {
            Debug.Assert(IsValidFileName(fileName));
            try
            {
                saveFile();
                return(FileOperationResult.OK);
            }
            catch (Exception e)
            {
                switch (policy)
                {
                case FileErrorPolicy.Inform:
                    using (SaveErrorInformDialog informDialog = new SaveErrorInformDialog(fileName, message, "Error while saving", e))
                    {
                        informDialog.ShowDialog();
                    }
                    break;

                case FileErrorPolicy.ProvideAlternative:
                    using (SaveErrorChooseDialog chooseDialog = new SaveErrorChooseDialog(fileName, message, "Error while saving", e, false))
                    {
                        switch (chooseDialog.ShowDialog())
                        {
                        case DialogResult.OK:                                         // choose location (never happens here)
                            break;

                        case DialogResult.Retry:
                            return(ObservedSave(saveFile, fileName, message, policy));

                        case DialogResult.Ignore:
                            return(FileOperationResult.Failed);
                        }
                    }
                    break;
                }
            }
            return(FileOperationResult.Failed);
        }
		public FileOperationResult ObservedLoad(FileOperationDelegate loadFileDelegate, string fileName, string message, FileErrorPolicy policy)
		{
			Debug.Assert(IsValidFileName(fileName));
			//Eventually do the load file action.
			try 
			{
				loadFileDelegate();//通过这个委托来真正最后调用实际的方法,所有的ObservedLoad方法
				//无论是NamedFileOperation还是FileOperation最终都会通过这个委托来调用所对应的方�?
				return FileOperationResult.OK;
			} 
			catch (Exception e) 
			{
				switch (policy) 
				{
					case FileErrorPolicy.Inform:
						using (SaveErrorInformDialog informDialog = new SaveErrorInformDialog(fileName, message, "Error while loading", e)) 
						{
							informDialog.ShowDialog();
						}
						break;
					case FileErrorPolicy.ProvideAlternative:
						using (SaveErrorChooseDialog chooseDialog = new SaveErrorChooseDialog(fileName, message, "Error while loading", e, false)) 
						{
							switch (chooseDialog.ShowDialog()) 
							{
								case DialogResult.OK: // choose location (never happens here)
									break;
								case DialogResult.Retry:
									return ObservedLoad(loadFileDelegate, fileName, message, policy);
								case DialogResult.Ignore:
									return FileOperationResult.Failed;
							}
						}
						break;
				}
			}
			return FileOperationResult.Failed;
		}
		public FileOperationResult ObservedSave(NamedFileOperationDelegate saveFileAs, string fileName, string message, FileErrorPolicy policy)
		{
			Debug.Assert(IsValidFileName(fileName));
			try 
			{
				saveFileAs(fileName);
				return FileOperationResult.OK;
			} 
			catch (Exception e) 
			{
				switch (policy) 
				{
					case FileErrorPolicy.Inform:
						using (SaveErrorInformDialog informDialog = new SaveErrorInformDialog(fileName, message, "Error while saving", e)) 
						{
							informDialog.ShowDialog();
						}
						break;
					case FileErrorPolicy.ProvideAlternative:
						restartlabel:
							using (SaveErrorChooseDialog chooseDialog = new SaveErrorChooseDialog(fileName, message, "Error while saving", e, true)) 
							{
								switch (chooseDialog.ShowDialog()) 
								{
									case DialogResult.OK:
										using (SaveFileDialog fdiag = new SaveFileDialog()) 
										{
											fdiag.OverwritePrompt = true;
											fdiag.AddExtension    = true;
											fdiag.CheckFileExists = false;
											fdiag.CheckPathExists = true;
											fdiag.Title           = "Choose alternate file name";
											fdiag.FileName        = fileName;
											if (fdiag.ShowDialog() == DialogResult.OK) 
											{
												return ObservedSave(saveFileAs, fdiag.FileName, message, policy);
											} 
											else 
											{
												goto restartlabel;
											}
										}
									case DialogResult.Retry:
										return ObservedSave(saveFileAs, fileName, message, policy);
									case DialogResult.Ignore:
										return FileOperationResult.Failed;
								}
							}
						break;
				}
			}
			return FileOperationResult.Failed;
		}
		//定义一些受保护的加载文件和保存文件的方�?具体执行的函数是通过委托实例来执行的.
		#region ObservedSaveOrLoad Methods

		public FileOperationResult ObservedSave(FileOperationDelegate saveFile, string fileName, string message, FileErrorPolicy policy)
		{
			Debug.Assert(IsValidFileName(fileName));
			try 
			{
				saveFile();
				return FileOperationResult.OK;
			} 
			catch (Exception e) 
			{
				switch (policy) 
				{
					case FileErrorPolicy.Inform:
						using (SaveErrorInformDialog informDialog = new SaveErrorInformDialog(fileName, message, "Error while saving", e)) 
						{
							informDialog.ShowDialog();
						}
						break;
					case FileErrorPolicy.ProvideAlternative:
						using (SaveErrorChooseDialog chooseDialog = new SaveErrorChooseDialog(fileName, message, "Error while saving", e, false)) 
						{
							switch (chooseDialog.ShowDialog()) 
							{
								case DialogResult.OK: // choose location (never happens here)
									break;
								case DialogResult.Retry:
									return ObservedSave(saveFile, fileName, message, policy);
								case DialogResult.Ignore:
									return FileOperationResult.Failed;
							}
						}
						break;
				}
			}
			return FileOperationResult.Failed;
		}