2014-06-24 76 views
1

我想將圖像作爲按鈕背景,我看到很多關於此問題的問題,而更好的方法似乎是創建自定義ImageButton的子類化按鈕。將自定義ImageButton控件添加到Windows Phone項目

無論如何,我試圖用圖像刷到圖像設置按鈕的背景,但它不工作,這樣的事情:

Button b = new Button(); 
ImageBrush ib = new ImageBrush(); 
ib.ImageSource = home; 
b.Background = ib; 

並設置圖片作爲內容:

Image i = new Image(); 
i.Source = home; 
Button b = new Button(); 
b.Content = home; 

然後我看到一個ImageButton class,我下載並添加到項目中。

,但我不能編譯

static ImageButton() { 
    DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton))); 
} 

public static readonly DependencyProperty ImageSizeProperty = 
     DependencyProperty.Register("ImageSize", typeof(double), typeof(ImageButton), 
     new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.AffectsRender)); 

基本上我得到這個錯誤:

'System.Windows.DependencyProperty' does not contain a definition for 'OverrideMetadata' and no extension method 'OverrideMetadata' accepting a first argument of type 'System.Windows.DependencyProperty' could be found (are you missing a using directive or an assembly reference?) 

The type or namespace name 'FrameworkPropertyMetadata' could not be found (are you missing a using directive or an assembly reference?) 

The name 'FrameworkPropertyMetadataOptions' does not exist in the current context 

有人能告訴我怎麼解決這個問題?或者如果我做錯了放置背景圖片或製作圖片的方式,只需要一個帶有填充圖片的按鈕,而無需填充或邊距或邊框。

回答

2

如果你只是想用一張圖片作爲背景的按鈕,試試這個

<Grid> 
    <Button BorderThickness="0"> 
     <Button.Background> 
      <ImageBrush Stretch="None" ImageSource="your_image.jpg"/> 
     </Button.Background> 
    </Button> 
<Grid> 
0

你可以簡單地使用ImageBrush

var brush = new ImageBrush(); 
brush.ImageSource = new BitmapImage(new Uri("ms-appx:/Assets/SmallLogo.png")); 
Button1.Background = brush; 

看看this線程。

希望它有幫助!

相關問題