WPF 中带有 FolderBrowserDialog 的 DialogResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14813506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:27:41  来源:igfitidea点击:

DialogResult with FolderBrowserDialog in WPF

c#wpffolderbrowserdialog

提问by Ortund

First time I'm implementing a FolderBrowserDialog in WPF and I'm not loving it one bit...

我第一次在 WPF 中实现 FolderBrowserDialog 并且我一点也不喜欢它......

Aside from the issues I had figuring out that Windows.Forms wasn't referenced in my project, now I'm having trouble trying to see what the DialogResult return value is...

除了我发现我的项目中没有引用 Windows.Forms 的问题之外,现在我在尝试查看 DialogResult 返回值时遇到了麻烦......

With an OpenFileDialog, in the past I've done it thusly:

使用 OpenFileDialog,过去我是这样做的:

OpenFileDialog ofd = new OpenFileDialog();
Nullable<bool> result = ofd.ShowDialog();

if (result == true)
{
    // all went well, carry on and do your thing here
}

Unfortunately, I'm now getting errors with this saying something about conversions from type DialogResult to bool and whatever have you.

不幸的是,我现在遇到了关于从 DialogResult 类型到 bool 类型的转换以及您拥有的任何内容的错误。

Can't seem to find anything on how to complete this step in using the dialog in WPF, can anyone shed some light?

似乎无法找到有关如何在 WPF 中使用对话框完成此步骤的任何信息,任何人都可以解释一下吗?

Thanks in advance!

提前致谢!

EDIT

编辑

Here's my code as amended without the type conversion error. I'm not sure what value to check resultagainst. Typically I'd use DialogResult.OKexcept that doesn't appear as a valid value here.

这是我修改后的代码,没有类型转换错误。我不确定要检查什么值result。通常我会使用,DialogResult.OK除了这里没有显示为有效值。

    private void btnBrowse_Click(object sender, RoutedEventArgs e)
    {
        if (cmbTemplate.SelectedItem == "Blockbusters")
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            DialogResult result = fbd.ShowDialog();

            //
            // ERROR: 'System.Nullable<bool>' does not contain a definition for 'OK'
            // and no extention method 'OK' accepting a first argument of type
            // 'System.Nullable<bool>' could be found.
            //
            if (result == DialogResult.OK)
            {
                txtSource.Text = fbd.SelectedPath;
            }
        }
    }

回答by Ortund

Okay so it turns out all answers other answers here were right.

好的,结果证明这里的所有其他答案都是正确的。

They just missed out one thing and I think that was my fault...

他们只是错过了一件事,我认为那是我的错......

Every time I saw DialogResultin Intellisense when trying to use it in my if statement (as I've been told to use, I saw this:

每次我DialogResult尝试在我的 if 语句中使用 Intellisense 时看到它(正如我被告知要使用的那样,我看到了这个:

bool? Window.Dialog.Result
Gets or sets the dialog result value, which is the value that is returned from the
System.Windows.Window.ShowDialog() method.

Exceptions:
System.InvalidOperationException

布尔?Window.Dialog.Result
获取或设置对话框结果值,该值是从
System.Windows.Window.ShowDialog() 方法返回的值。

异常:
System.InvalidOperationException

This particular DialogResult object isn't the one I was looking for.

这个特殊的 DialogResult 对象不是我要找的那个。

What finally worked was the following:

最终起作用的是以下内容:

DialogResult result = fbd.ShowDialog();

if (result == System.Windows.Forms.DialogResult.OK)
{
    // do work here
}

It's worth noting that I do have System.Windows.Formsreferenced in my usings which is why I never thought to reference the class from System as in the above snippet. I thought it was using this anyway.

值得注意的是,我确实System.Windows.Forms在我的使用中引用过,这就是为什么我从未想过在上面的代码片段中引用 System 中的类。我认为无论如何它都在使用它。

回答by Zhen Hao

Late answer here but why not just . .

在这里回答晚了,但为什么不只是 . .

private void SelectFolder()
{
    var dialog = new FolderBrowserDialog();
    var status = dialog.ShowDialog(); // ShowDialog() returns bool? (Nullable bool)
    if (status.Equals(true))
    {
        SelectedFolderPath = dialog.SelectedPath;
    }
}

You can see the result in debugging session. It returns false when the Cancel button is clicked.

您可以在调试会话中看到结果。单击取消按钮时返回 false。

回答by Jehof

DialogResultis an enumeration and defines values to indicate the return values of dialogs.

DialogResult是一个枚举并定义值以指示对话框的返回值。

In your code you should check for DialogResult.OKto init your variable with the path selected in the dialog. DialogResult.OKis returned when the "OK"-Button is pressed in the dialog, otherwise is DialogResult.Cancelreturned.

在您的代码中,您应该检查DialogResult.OK以使用对话框中选择的路径来初始化您的变量。DialogResult.OK在对话框中按下“确定”按钮时DialogResult.Cancel返回,否则返回。

if (result == DialogResult.OK){
  txtSource.Text = fbd.SelectedPath;
}

回答by fhnaseer

DialogResult.(OK, Cancel whatever you want to check),

DialogResult.(OK, 取消任何你想检查的),

if (result == DialogResult.OK) // DialogResult.(Your desired result, select from the list it generates)
{
    txtSource.Text = fbd.SelectedPath;
}