如何在c#中将值从一个函数传递给另一个函数

时间:2013-01-17 18:34:51

标签: c#

我有一个功能

      private void button_Click(object sender, RoutedEventArgs e) 
      {
         random();

      }

      private void random()
      {
         //other code
         ImageBrush background = new ImageBrush();
         background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new                         Uri(actorUri,    UriKind.Relative));
         //other code
       }

我想在按钮点击功能中访问该背景变量 请告诉我如何访问...

5 个答案:

答案 0 :(得分:1)

为什么不让background成为周围类的实例变量?

答案 1 :(得分:0)

你的问题根本不清楚......你是说这个?

private ImageBrush m_Background = new ImageBrush();

private void button_Click(object sender, RoutedEventArgs e) 
{
    // If actorUri is stored in a TextBox, for example...
    random(textbox1.Text);
}

private void random(String actorUri)
{
    // ...
    m_Background = new ImageBrush();
    m_Background.ImageSource = new BitmapImage(new Uri(actorUri, UriKind.Relative));
    // ...
}

答案 2 :(得分:0)

我不确定,但听起来你问这个。

    private void button_Click(object sender, RoutedEventArgs e) 
    {
         random();
         background.DoSomething();
    }

    ImageBrush background ;
    private void random()
    {
         //other code
         background = new ImageBrush();
         background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new                         Uri(actorUri,    UriKind.Relative));
         //other code
    }

答案 3 :(得分:0)

  private void button_Click(object sender, RoutedEventArgs e) 
  {
     random();
     background.SomeMethod();

  }

  ImageBrush background = new ImageBrush();

  private void random()
  {
     //other code

     background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new                         Uri(actorUri,    UriKind.Relative));
     //other code
   }

修改

OR

  private void button_Click(object sender, RoutedEventArgs e) 
  {
     ImageBrush bg = random();
     bg.SomeMethod()


  }



  private ImageBrush random()
  {
     //other code
     ImageBrush background = new ImageBrush();
     background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new                         Uri(actorUri,    UriKind.Relative));
     //other code
     return background;
   }

答案 4 :(得分:0)

private void button_Click(object sender, RoutedEventArgs e) 
{
    ImageBrush background = random();
}

private ImageBrush random()
{
    //other code
    ImageBrush background = new ImageBrush();
    background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new                            Uri(actorUri,    UriKind.Relative));
    return background;
}