WP7用代码更改代码中的按钮图像

时间:2012-03-04 23:49:18

标签: windows-phone-7 url button

设置按钮的背景并不像android的dev.it那么简单;

在.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中获取此图像。如果无法在代码中设置带有网址的按钮背景,

1 个答案:

答案 0 :(得分:1)

试试这个

首先创建图像源,然后将该源提供给您正在使用的图像控件。

string imagepath =“url”;

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