在与WPF的FolderBrowserDialog的DialogResult [英] DialogResult with FolderBrowserDialog in WPF

查看:174
本文介绍了在与WPF的FolderBrowserDialog的DialogResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次在WPF实现的FolderBrowserDialog,我没有从我已经搞清楚了Windows中的问题,爱它一个位...



除了者,恕不不是在我的项目中引用,我现在遇到了麻烦想看看有什么DialogResult的返回值是...



通过一个OpenFileDialog,在过去的我正是如此做的:

 打开文件对话框OFD =新的OpenFileDialog(); 
可空<布尔>结果= ofd.ShowDialog();

如果(结果==真)
{
//一切顺利,继续在这里做你的事
}

不幸的是,现在我得到错误与这句话有关的东西从转换类型的DialogResult为bool和任何有你。



似乎无法找到如何完成在使用WPF对话框这一步任何事情,任何人都可以提供一些线索?



提前感谢!



修改



下面的修订没有我的代码类型转换错误。我不知道要检查结果反对什么价值。通常,我会使用 DialogResult.OK 除了不会出现在这里作为一个有效的值。

 私人无效btnBrowse_Click(对象发件人,RoutedEventArgs E)
{
如果(cmbTemplate.SelectedItem ==大片)
{
的FolderBrowserDialog FBD =新的FolderBrowserDialog();

DialogResult的结果= fbd.ShowDialog();

//
//错误:System.Nullable<布尔>'不包含'OK'
//没有延伸的方法定义'OK'接受类型
//的第一个参数System.Nullable<布尔>'可以找到。
//
如果(结果== DialogResult.OK)
{
txtSource.Text = fbd.SelectedPath;
}
}
}


解决方案

好了,原来所有的答案其他的答案在这里是正确的。



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



每当我看到试图利用它在我的if语句(因为我已经被告知,当的DialogResult 在IntelliSense使用时,我看到了这一点:




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



例外:结果
System.InvalidOperationException




这特别的DialogResult对象不是我所期待的。



终于什么工作是以下内容:

  DialogResult的结果= fbd.ShowDialog(); 

如果(结果== System.Windows.Forms.DialogResult.OK)
{
//在这里做的工作,
}

这是值得注意的,我确实有 System.Windows.Forms的在我usings其中引用的就是为什么我从来没有想过要引用从系统类在上面的代码片段。我认为这就是用这个反正。


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

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...

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

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.

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

Thanks in advance!

EDIT

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

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

解决方案

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 DialogResult in Intellisense when trying to use it in my if statement (as I've been told to use, I saw this:

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

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

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.Forms referenced 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.

这篇关于在与WPF的FolderBrowserDialog的DialogResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆