[英]C#: How do I view an image when a user clicks on a button?
I've seen many different versions of the same answer, none of which have solved my problem. 我见过相同答案的许多不同版本,但都没有解决我的问题。 I'm using a WPF and I want an image to view when the user clicks on a button "hint".
我正在使用WPF,并且我希望用户单击按钮“提示”时查看图像。 Here's the code so far:
到目前为止的代码如下:
void Hint_Click(object sender, RoutedEventArgs e)
{
Label.Content = "Yes";
BitmapImage RealTrump = new BitmapImage(new Uri("RealTrump", UriKind.Relative));
}
It's a jpeg image and is saved within the project file so the directory is just "RealTrump". 这是一个jpeg图片,并保存在项目文件中,因此目录仅为“ RealTrump”。 When I run through compiler and click the button it just views the label and not the image.
当我运行编译器并单击按钮时,它仅查看标签而不是图像。
Here's the XAML code, I really don't know what to do with it as I'm still pretty new to c# and programming in general: 这是XAML代码,我真的不知道该怎么做,因为我对C#和一般编程还是很陌生:
<Image x:Name="RealTrump" HorizontalAlignment="Left" Height="100" Margin="381,44,0,0" VerticalAlignment="Top" Width="97"/>
I'm on Visual Studio 2013 if that helps. 如果有帮助,我正在使用Visual Studio 2013。
While you have given your label the name RealTrump
you've created a local variable of the same name in the click event handler. 将标签命名为
RealTrump
您在click事件处理程序中创建了一个具有相同名称的局部变量。
If you simply remove the type declaration and set the image source like this: 如果仅删除类型声明并像这样设置图像源:
void Hint_Click(object sender, RoutedEventArgs e)
{
Label.Content = "Yes";
RealTrump.Source = new BitmapImage(new Uri("pack://application:,,,/Images/RealTrump.jpg", UriKind.Relative));
}
The image should be displayed. 该图像应显示。 (You may have to adjust the path slightly depending on exactly where it is relative to your source.
(您可能必须根据与源相对的确切位置来稍微调整路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.