2013-01-17 136 views
-2

我有一個函數如何從一個函數值傳遞到另一個在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 
     } 

我要訪問按鈕的點擊功能背景變量 請告訴我如何訪問...

+0

哪些變量? –

+0

隨機函數中的背景變量 –

回答

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

非常感謝 你的代碼工作.... :) –

+0

不要忘記標記爲回答。 –

+0

我沒有得到? –

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)); 
    // ... 
} 
+0

圖像刷類型的背景 我想在按鈕點擊函數中獲取它的值 –

+0

該值存儲在哪裏?在文本框? –

+0

存儲在背景變量 –

1

爲什麼不讓background成爲周圍類的實例變量?

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 
    } 
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; 
} 
相關問題