简体   繁体   English

在UWP中添加图像?

[英]Adding an image in UWP?

I have been looking online for hours trying to find a way to insert a simple image into my Universal Windows Application. 我一直在网上寻找几个小时试图找到一种方法将一个简单的图像插入我的通用Windows应用程序。 All the tutorials I have found so far have been too confusing or just don't work. 到目前为止,我发现的所有教程都太混乱或者根本不起作用。

Is there a way to insert an image into my XAML page that can be explained for someone new to developing Windows Apps? 有没有办法将图像插入我的XAML页面,可以为刚开发Windows应用程序的人解释? Do I need to initialize it in XAML and C#, or just through XAML? 我需要在XAML和C#中初始化它,还是仅通过XAML?

Since I cannot comment yet, I'll answer this. 既然我还不能发表评论,我会回答这个问题。

In order to add an image to your XAML you simply have to add a tag. 要将图像添加到XAML,您只需添加标记即可。 So the code would look like this: 所以代码看起来像这样:

<Image Source="Assets/example.jpg"/>

I would suggest you follow this beginner series for UWP. 我建议你按照UWP的这个初学者系列进行操作。 I found it great for showing me the basics of XAML and teaching me a bit of the new design language. 我发现很高兴向我展示XAML的基础知识并教我一些新的设计语言。 Just know that it's almost a year old and there are tiny tweaks, especially with design. 只知道它已经差不多一年了,并且有微小的调整,尤其是设计。 Nothing to worry about though, just keep it in mind. 没什么可担心的,只要记住它。

If you get confused about anything you can just download a sample and follow how it works (I get bored easily so I do that myself, and set up couple of break points to understand the code flow), 如果你对任何事情感到困惑,你可以下载一个样本并按照它的工作原理(我很容易感到厌倦,所以我自己这样做,并设置几个断点来理解代码流),

Try this: https://code.msdn.microsoft.com/windowsapps/0f5d56ae-5e57-48e1-9cd9-993115b027b9 试试这个: https//code.msdn.microsoft.com/windowsapps/0f5d56ae-5e57-48e1-9cd9-993115b027b9

Remember that when you want to insert a new image into Assets, you must go into the solution explorer, Assets->Add->New Item. 请记住,当您要在Assets中插入新图像时,必须进入解决方案资源管理器,Assets-> Add-> New Item。 If you insert the image manually, then you need to Add->existing item. 如果手动插入图像,则需要添加 - >现有项目。 Then select the image, rigth click, properties and in Build Action, make sure "content" is the default. 然后选择图像,rigth click,属性,在Build Action中,确保“content”是默认值。

Andrew you are 100% correct, the online tutorial writers often forget people who are new to the xaml\\c# world might be reading their tutorials, often I find the official MS documentation much more easier to understand than those tutorials, anyways I've made a simple graphic explanation on how to add Images and display them in your project using Blend but the same principle applies to VS. 安德鲁你100%正确,在线教程编写者经常忘记那些刚接触xaml \\ c#世界的人可能正在阅读他们的教程,我经常发现官方的MS文档比那些教程更容易理解,无论如何我做了关于如何添加图像并使用Blend在项目中显示它们的简单图形说明,但同样的原则适用于VS.

Check these images: http://postimg.org/gallery/2jzw7yqgg/ 查看这些图片: http//postimg.org/gallery/2jzw7yqgg/

I had the same problem, where I only needed to just add simple picture get it over with, I'm assuming that you are using VS2017 if no, then I don't know if this solution will work, but if you are using VS2017 then do the following 我有同样的问题,我只需要添加简单的图片就可以了,我假设你使用的是VS2017,如果没有,那么我不知道这个解决方案是否有用,但是如果你使用VS2017然后执行以下操作

fist add an image element for example <Image x:Name="img1" Width="100" Height="100" /> 拳头添加图像元素,例如<Image x:Name="img1" Width="100" Height="100" />

in the Universal Apps, there is a folder called Assets you will find it in the solution folder, also in the solution explorer, 在Universal Apps中,有一个名为Assets的文件夹,您可以在解决方案文件夹中找到它,也可以在解决方案资源Assets找到它, 在此输入图像描述 right click on it in the solution explorer -> add existing item -> the photo you want 在解决方案资源管理器中右键单击它 - >添加现有项目 - >您想要的照片

after you have added the photo, go to the img1 prosperities and in the source you will find the photo that was added inside the drop down list, keep in mind that there is already images in the assets 添加照片后,转到img1繁荣,在源代码中,您将找到下拉列表中添加的照片,请记住资产中已有图像

在此输入图像描述 you will find that the path for the image is added automatically in the Source attribute to the control img1 for example <Image x:Name="img1" Width="100" Height="100" Source="Assets/1.jpg" /> 你会发现图像的路径在Source属性中自动添加到控件img1 ,例如<Image x:Name="img1" Width="100" Height="100" Source="Assets/1.jpg" />

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

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