gpt4 book ai didi

xaml - 在 Metro 风格应用程序中以编程方式更改按钮的背景图像

转载 作者:行者123 更新时间:2023-12-01 01:02:49 26 4
gpt4 key购买 nike

如何使用 VS 2012 在 Metro Style 应用程序中更改按钮的背景图像?在 Windows 窗体中,按钮具有可以设置的 BackgroundImage 属性。此功能是否适用于 Metro Style 应用程序?

在 Windows 窗体中,我可以在 C# 中执行以下操作:

btnImage.BackgroundImage = System.Drawing.Image.FromFile("...\Pictures\flower.png");

如何以编程方式更改 Metro Style 应用程序中按钮的背景图像?

最佳答案

实际上,非常简单,只需修改 Button 的 XAML 以包含一个结束标记,然后在中间放置一个图像控件,如下所示:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button HorizontalAlignment="Left" Margin="532,285,0,0" VerticalAlignment="Top" Height="135" Width="283">
<Image Source="Assets/Logo.png" />
</Button>
</Grid>

在上面的代码片段中,我将图像源指向 Logo.png 文件,该文件是 C#/XAML 应用内置模板的一部分。

另一种方法是在 Blend for Visual Studio 中打开项目,将图像从“ Assets ”选项卡拖到设计图面上(确保在“对象和时间轴” Pane 中选择了所需的容器),然后右- 单击图像并选择 Make into Control...,然后选择 Button 控件。

此技术的唯一缺点是您无法获得内置 Button 控件所具有的默认 VisualStates。相反,Blend 使用空的 VisualStates 为您定义了一种样式,您可以根据需要设置样式。

希望对您有所帮助。

关于xaml - 在 Metro 风格应用程序中以编程方式更改按钮的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com