添加自定义ImageButton控件到Windows Phone项目
本文关键字:Windows Phone 项目 控件 自定义 ImageButton 添加 | 更新日期: 2023-09-27 18:10:08
我试图把图像作为按钮背景,我看到很多关于这个问题,更好的方法似乎是子类化按钮来创建自定义ImageButton。
无论如何,我尝试使用ImageBrush将图像设置为按钮背景,但它不起作用,像这样:
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类,我下载并添加到项目。
但是我不能编译它
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
谁能告诉我怎么解决这个问题?或者如果我做错了什么,把背景图像或其他方式,只需要一个按钮的图像填充它,没有填充,空白或边框。
如果你只是想要一个图片作为背景的按钮,试试这个
<Grid>
<Button BorderThickness="0">
<Button.Background>
<ImageBrush Stretch="None" ImageSource="your_image.jpg"/>
</Button.Background>
</Button>
<Grid>
您可以简单地使用ImageBrush
.
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("ms-appx:/Assets/SmallLogo.png"));
Button1.Background = brush;
看看这篇文章。
希望有帮助!