简体   繁体   English

C#:当用户单击按钮时如何查看图像?

[英]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.

相关问题 当用户单击不正确的按钮时,如何使c#应用程序弹出视频? - How do I get my c# application to popup a video when user clicks incorrect button? 如果用户在C#中单击多次,如何防止按钮代码运行两次? - How do I prevent button code from running twice if the user clicks more than once in C#? C#treeView,当用户单击子节点时如何获取父节点 - C# treeView, how do I get the parent nodes when user clicks the child node 用户在c#中单击它时获取图像源 - get the source of image when user clicks it in c# 用户单击浏览器停止按钮时如何停止服务器端处理(ASP.net C#) - How to stop server side processing when user clicks browser stop button (ASP.net C#) 当用户单击asp按钮时,如何模拟键盘按钮的按下? - How do I simulate keyboard button press when the user clicks on asp button? Windows Phone 7:当用户单击图像时,我该怎么做? - Windows Phone 7: How can I do something when the user clicks on an image? 当用户使用MVC3 C#asp.net单击按钮时显示弹出对话框 - Display popup dialog box when user clicks button using MVC3 C# asp.net ASP.NET MVC3 C# - 当用户单击按钮时,将DB中的记录从1更改为2,反之亦然 - ASP.NET MVC3 C# - when user clicks button, change record in DB from 1 to 2 and viceversa c# 单击按钮时如何打开 combobox - c# How do I open the combobox when the button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM