2012-03-04 57 views
0

設置一個按鈕的背景並不像android的dev.it更簡單;WP7改變一個按鈕圖像的代碼與一個url

中的.xaml文件

 <Button Click="Button_Click" x:Name="img_btn"> 
      <Button.Template> 
       <ControlTemplate> 
      <!--set button background that we can hanle click event--> 
        <Image HorizontalAlignment="Left" Margin="0,-69,0,479" x:Name="image1" Stretch="Fill" Width="480" Source="/myNameSpace;component/home_003.jpg" /> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 
在此

cs文件:

   JsonObject jsonObject = (JsonObject)JsonObject.Load(e.Result); 
       ads_address = jsonObject["imageurl"];//get a url from jsonobject 
       ImageBrush imageBrush = new ImageBrush(); 
       imageBrush.ImageSource = new BitmapImage(new Uri(ads_address));//ads url likes //"http://27.98.194.93/forum/images/2009/home_004.jpg"; 

       this.img_btn.Background = imageBrush;//this code is not work,perhaps, I can't get what I want(Image) with code "this.img_btn.Background" 

我怎麼能得到這個形象Button.If不了什麼設置按鈕背景的方法與代碼中的網址;

回答

1

試試這個

創建圖像源在第一,然後給予該源正在使用的圖像控制。

string imagepath =「url」;

ImageSource imgsrc1 = new BitmapImage(new Uri(imagepath,UriKind.RelativeOrAbsolute)); // image可以是Image/ImageBrush image.Source = imgsrc1;

+0

謝謝,通過這種方式,我可以設置一個帶有url的ImageSource。但對我來說很重要。總之,我怎樣才能獲得一個Button中的Image控件; – 2012-03-05 10:52:12

+0

最後我發現[它](http://stackoverflow.com/questions/9444091/how-to-reference-image-in-controltemplate「感謝這個人」),它運作良好。 – 2012-03-05 11:13:09

相關問題