繁体   English   中英

将自定义ImageButton控件添加到Windows Phone项目

[英]Adding custom ImageButton control to Windows Phone project

我正在尝试将图像作为按钮背景,我对此有很多疑问,更好的方法似乎是对按钮进行子类化以创建自定义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;

看看这个线程。

希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM