WPF - SaveFileDialog

nir*_*tel 16 c# wpf

我正在使用SaveFileDialogWPF导出到用户选择的特定loaction的excel文件.但是在SaveFileDailog打开之间然后用户clilks on Cancel button对话框然后我得到另一个说出的对话框"Do you want to save changes you made to 'Sheet1'?"然后"Export completed"取消取消导出.

那么我要做些什么才能解决这个问题呢?WPF中的任何东西'DialogResult'都与winForms中的相同?

VS1*_*VS1 50

如果用户保存(ShowDialog方法返回可以为空的bool),SaveFileDialog将返回true ,如果用户按下cancel,则返回false/null.下面是一个MSDN代码示例,可帮助您入门:

// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dlg.FileName;
}
Run Code Online (Sandbox Code Playgroud)


Pan*_*yay 3

当用户单击“取消”时,您需要利用 WPF 中的 MessageBox 打开另一个窗口。将以下代码添加到取消按钮事件:-

private void canceButton()
    {
        MessageBoxResult key = MessageBox.Show(
            "Are you sure you want to quit",
            "Confirm",
            MessageBoxButton.YesNo,
            MessageBoxImage.Question,
            MessageBoxResult.No);
        if (key == MessageBoxResult.No)
        {
            return;
        }
        else
        {
            Application.Current.Shutdown();
        }
    }
Run Code Online (Sandbox Code Playgroud)