首页 > 代码库 > WP8_给图片、按钮设置自定义图片

WP8_给图片、按钮设置自定义图片

工程目录下新建文件夹Images,将图片文件复制到Images文件夹里,本文以image1为例

1).在xaml里加背景图片

    image图片: <Stretch="Fill" Source="Images/image1.png"  Name="image"/>

    Button背景图片:<Button Name="button1" >
            <Button.Background>
                <ImageBrush ImageSource="/Images/image1.png" />
            </Button.Background>
        </Button>

2).在程序代码里添加

  自定义两个方法:

public void changeBack(Image image, String url)
        {
            image.Source = new BitmapImage(new Uri(url, UriKind.Relative));
        }
public void changeBack(Button btn,String url)
        {
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = new BitmapImage(new Uri(url, UriKind.Relative));
            btn.Background = imageBrush;
        }

image加背景图片:changeBack(image,"Images/image1.png")

button加背景图片:changeBack(button,,"Images/image1.png");