如何在另一个函数中引用从私有函数获得的变量?

时间:2013-04-02 14:34:56

标签: c# wpf openfiledialog

我希望有人可以帮助我。我比较新,想了解如何将一个类中的变量值传递给另一个类。在这种情况下,我想在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 个答案:

答案 0 :(得分:4)

在MainWindow类中使用property

private string[] FilePaths{get;set;}

用FilePath替换变量filePath。

答案 1 :(得分: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];
        }
    }
}

答案 2 :(得分:0)

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

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

    //...

}

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

答案 3 :(得分: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];
}

}