我有一个功能
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 :(得分: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;
}