0

Set a button's background is not as simple as android's dev.it's more complex;

in .xaml file:

       <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>

in this .cs file:

                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"

how can I get this Image in Button.If can't what's the way to set a button background with a url in code;

Albert.Qing
  • 4,220
  • 4
  • 37
  • 49

1 Answers1

1

try this

Create image source at first and then giving that source to the image control you are using.

string imagepath = "url";

ImageSource imgsrc1 = new BitmapImage(new Uri(imagepath, UriKind.RelativeOrAbsolute)); //image can be Image/ImageBrush image.Source = imgsrc1;

TutuGeorge
  • 1,972
  • 2
  • 22
  • 42
  • Thank you,in this way I can set a ImageSource with a url.But the important for me.In a word,how can I get a Image control that in a Button; – Albert.Qing Mar 05 '12 at 10:52
  • Finally I find [it](http://stackoverflow.com/questions/9444091/how-to-reference-image-in-controltemplate "Thanks this guy"),and it works well. – Albert.Qing Mar 05 '12 at 11:13