wpf 如何在wpf中从xaml设置相对uri?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20341066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set relative uri from xaml in wpf?
提问by James Joshua Street
So I am wondering where the relative uri should be set relative to. I had assumed it was the main project.
所以我想知道相对 uri 应该相对于哪里设置。我以为这是主要项目。
I have the image stored under a folder images. The images folder is directly under the project DaedalusGraphViewer under the solution which also has the same name (DaedalusGraphViewer).
我将图像存储在文件夹图像下。图像文件夹直接位于解决方案下的项目 DaedalusGraphViewer 下,该项目也具有相同的名称(DaedalusGraphViewer)。
I had assumed that the relative uri was relative to the project, but I don't seem to be having any luck. Alternative ideas such as doing it from code behind are also welcome. I have done it from code behind before so I can probably figure that out since I will be able to debug it better there, but I would prefer to just do it in xaml.
我曾假设相对 uri 与项目有关,但我似乎没有任何运气。也欢迎其他想法,例如从代码后面做。我之前是从代码后面完成的,所以我可能会弄清楚,因为我可以在那里更好地调试它,但我更愿意只在 xaml 中进行。
I am using
我在用
<ResourceDictionary Source="DaedalusGraphViewer/ResourceDictionaries/GraphViewerBrushes.xaml" />
to do the same thing elsewhere, so I don't see what I'm doing wrong.
在其他地方做同样的事情,所以我看不出我做错了什么。
<Button
Height="50"
VerticalAlignment="Top"
>
<Image Width="50" Height="50" Source="Images/zoominbutton.bmp" Stretch="Fill"/>
</Button>
edit: still no luck, not sure what I'm doing wrong.
编辑:仍然没有运气,不确定我做错了什么。
i've revised the code to use a much smaller test case.
我已经修改了代码以使用更小的测试用例。
ideally i would like to be able to set the image path from xaml, but for now i'm trying code behind just to see if I can figure out anything that will make it work.
理想情况下,我希望能够从 xaml 设置图像路径,但现在我正在尝试编写代码,看看是否可以找出任何可以使其工作的方法。
<Window
x:Class="TestApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Loaded="Window_Loaded"
>
<Canvas x:Name="MainCanvas">
<!--<Image x:Name="ImageSource2" Width="50" Height="50" Source="/DaedalusGraphViewer;component/images.jpg" Stretch="Fill"/>-->
<!--<Image x:Name="imagetest" Stretch="Fill" Width="50">
<Image.Source>
<BitmapImage
DecodePixelWidth="200"
UriSource="/Images/images.jpg"/>
</Image.Source>
</Image>-->
<!--<Image Width="50" Source="../../Images/images.jpg" Stretch="Fill"/>-->
</Canvas>
</Window>
namespace TestApp
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Image i1 = new Image();
Uri relativeuri = new Uri("/DaedalusGraphViewer;component/images.jpg", UriKind.Relative);
BitmapImage bmi = new BitmapImage(relativeuri);
i1.Source = bmi;
MainCanvas.Children.Add(i1);
}
}
}
My goal for now is just to display an image on the main canvas. When do I need to use an absolute uri and when can I use a relative Uri. I have read lots of examples of xaml using relative uris so I thought that would be possible.
我现在的目标只是在主画布上显示图像。什么时候需要使用绝对 uri,什么时候可以使用相对 uri。我已经阅读了很多使用相对 uri 的 xaml 示例,所以我认为这是可能的。
回答by ebattulga
Replace YOURAPPNAME
代替 YOURAPPNAME
<Image Width="50" Height="50" Source="/YOURAPPNAME;component/Images/zoominbutton.bmp" Stretch="Fill"/>
回答by James Joshua Street
I got it to work using "pack://application:,,,/images.jpg" as the source. However, I'm still confused as to why I can't get a relative pack uri to work like this: http://msdn.microsoft.com/en-us/library/aa970069.aspx
我使用“pack://application:,,,/images.jpg”作为源让它工作。但是,我仍然很困惑为什么我不能让一个相对的包 uri 像这样工作:http: //msdn.microsoft.com/en-us/library/aa970069.aspx
I thought I could just use a source = "/images.jpg"
我以为我可以只使用 source = "/images.jpg"
I read elsewhere that there is a default string to uri converter that converts the relative uri to an absolute uri based on the assembly being built.
我在别处读到有一个默认字符串到 uri 转换器,它根据正在构建的程序集将相对 uri 转换为绝对 uri。
anyway if anyone knows how to get the relative pack uri to work or why it doesn't work, please let me know.
无论如何,如果有人知道如何让相关的包 uri 工作或为什么它不起作用,请告诉我。
I also got ebattulga's answer to work, so I'm selecting it as the right answer. However, I would like to understand a bit better why the relative pack uri from the pack uri site doesn't work.
我也得到了 ebattulga 的工作答案,所以我选择它作为正确答案。但是,我想更好地理解为什么来自 pack uri 站点的相对 pack uri 不起作用。
ps - It doesn't appear to work with the image set to content. I got it working with the image build type set to resource
ps - 它似乎不适用于设置为内容的图像。我得到它与图像构建类型设置为资源
ps2- I haven't been able to get the same code to work with .bmp extension images. Not sure what is going on.
ps2-我无法获得相同的代码来处理 .bmp 扩展图像。不知道发生了什么。

