0

我希望有人能帮助我。我比较新,想了解如何将一个类中的变量值传递给另一个类。在这种情况下,我想在 Button_Click_2 中使用 Button_Click_1 中的 numPage 和 filePath 数组。

先感谢您!

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog();

        openFileDialog1.Filter = "Images (.jpg)|*.jpg";
        openFileDialog1.FilterIndex = 1;

        openFileDialog1.Multiselect = true;

        bool? userClickedOK = openFileDialog1.ShowDialog();

        if (userClickedOK == true)
        {
            string[] filePaths = openFileDialog1.FileNames;
            int imageNum = 0;
            lblFilePath.Content = filePaths[imageNum];
        }

    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        imageNum++;
        lblFilePath.Content = filePath[imageNum];
    }
}
4

4 回答 4

4

使用 MainWindow 类中的属性

private string[] FilePaths{get;set;}

用 FilePath 替换变量 filePath。

于 2013-04-02T14:39:06.993 回答
3

您只需要将它们存储为类实例变量而不是本地方法变量。

public partial class MainWindow : Window
{
    // These can now be accessed from any method in this class.
    private string[] filepaths = null;
    private int imageNum = 0;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog();

        openFileDialog1.Filter = "Images (.jpg)|*.jpg";
        openFileDialog1.FilterIndex = 1;

        openFileDialog1.Multiselect = true;

        bool? userClickedOK = openFileDialog1.ShowDialog();

        if (userClickedOK == true)
        {
            // You can use the keyword "this" to access instance variables, but it is optional.
            this.filePaths = openFileDialog1.FileNames;
            this.imageNum = 0;
            lblFilePath.Content = this.filePaths[this.imageNum];
        }

    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        // You may want to put some validation in here to prevent errors situations.

        // Validate that filePaths has been initialized.
        if (this.filePaths == null)
        {
           System.Windows.Forms.MessageBox.Show("No files paths to display.");
        }
        // Validate that imageNum can be incremented without IndexOutOfRangeException.
        else if (this.imageNum < this.filePaths.Length - 1)
        {
            this.imageNum++;
            lblFilePath.Content = this.filePaths[this.imageNum];
        }
        // Otherwise, loop back to the first file path.
        else
        {
            this.imageNum = 0;
            lblFilePath.Content = this.filePaths[this.imageNum];
        }
    }
}
于 2013-04-02T14:43:05.490 回答
0

您需要在此处创建实例变量,而不是将变量范围限定为方法的本地变量:

public partial class MainWindow : Window
{
    private string[] filePaths;
    private int imageNum = 0;

    //...

}

然后,您可以在这两个方法中使用这些实例变量(确保不要重新定义具有相同名称的新局部变量)。

于 2013-04-02T14:40:22.703 回答
0

将两者都作为形式 1 的属性

public partial class MainWindow : Window
{

   private string[] FilePaths {get;set;}
   int imageNum = 0;

   public MainWindow()
   {
    InitializeComponent();
   }

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog();

    openFileDialog1.Filter = "Images (.jpg)|*.jpg";
    openFileDialog1.FilterIndex = 1;

    openFileDialog1.Multiselect = true;

    bool? userClickedOK = openFileDialog1.ShowDialog();

    if (userClickedOK == true)
    {
        FilePaths = openFileDialog1.FileNames;

        lblFilePath.Content = filePaths[imageNum];
    }

}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    imageNum++;
     if(imageNum<FilePaths.Length)
         lblFilePath.Content = FilePaths[imageNum];
}

}

于 2013-04-02T14:42:02.687 回答