Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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 - Fatal编程技术网

C# 如何在执行过程中请求用户输入?

C# 如何在执行过程中请求用户输入?,c#,wpf,C#,Wpf,我有一个WPF应用程序,它基本上通过while循环读取XML文档。但是,用户每次都必须手动输入文件路径 我需要中断执行,提供一个文本框或键入文件路径的内容,然后在输入后继续。我读了好几个答案,但还没有找到一个解决这个问题的答案 这是代码的样子: private void BTN_Click(object sender, RoutedEventArgs e) { int s = 1; while (s < 10) { if (condition == true)

我有一个WPF应用程序,它基本上通过while循环读取XML文档。但是,用户每次都必须手动输入文件路径

我需要中断执行,提供一个文本框或键入文件路径的内容,然后在输入后继续。我读了好几个答案,但还没有找到一个解决这个问题的答案

这是代码的样子:

private void BTN_Click(object sender, RoutedEventArgs e)
{
    int s = 1;
    while (s < 10)
    { if (condition == true)
         {
           XDocument mydocument = XDocument.Load("C:\\Users\\me\\mydocument.xml");
           string XML2Str = mydocument.ToString();
           if (XML2Str.Contains("bingo"))
              { //do stuff//}
         }
         s++;
    }
 }
private void BTN\u单击(对象发送者,路由目标)
{
int s=1;
而(s<10)
{if(条件==true)
{
XDocument mydocument=XDocument.Load(“C:\\Users\\me\\mydocument.xml”);
字符串XML2Str=mydocument.ToString();
if(XML2Str.Contains(“宾果”))
{///做事//}
}
s++;
}
}

目前我只能使用一个文件路径。我希望用户输入文件路径,而不是代码中的文件路径。

下面的代码如何

using System.Windows;
using Microsoft.Win32;

namespace WpfApp1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = "All Files | *.*"
            };

            while (true)
            {
                var b = dialog.ShowDialog();
                if (b == null || !b.Value)
                    break;

                MessageBox.Show(dialog.FileName);
            }
        }
    }
}
现在根据您的需要调整它。

可能会有帮助吗
private void BTN\u单击(对象发送者,路由目标)
{
尝试
{
加载(textbox1.Text);
}catch(Exception ex){MessageBox.Show(ex.Message);}
}
无效加载(字符串_路径)
{
int s=1;
而(s<10)
{if(条件==true)
{
XDocument mydocument=XDocument.Load(_路径);
字符串XML2Str=mydocument.ToString();
if(XML2Str.Contains(“宾果”))
{///做事//}
}
s++;
}
}
private void BTN_Click(object sender, RoutedEventArgs e)
{
  try
  {
    load(textbox1.Text);
  }catch(Exception ex){ MessageBox.Show(ex.Message);}
}

void load(string _path)
{
  int s = 1;
   while (s < 10)
   { if (condition == true)
      {
        XDocument mydocument = XDocument.Load(_path);
        string XML2Str = mydocument.ToString();
        if (XML2Str.Contains("bingo"))
          { //do stuff//}
      }
     s++;
  }
}