gpt4 book ai didi

c# - 如何在 wpf 中设置来自 xaml 的相对 uri?

转载 作者:行者123 更新时间:2023-11-30 19:41:17 27 4
gpt4 key购买 nike

所以我想知道应该相对于哪里设置相对 uri。我以为这是主要项目。

我将图像存储在文件夹 images 下。图像文件夹直接位于解决方案下的项目 DaedalusGraphViewer 下,该项目也具有相同的名称(DaedalusGraphViewer)。

我曾假设相对 uri 是相对于项目的,但我似乎没有任何运气。也欢迎其他想法,例如从代码背后进行。我之前已经从代码后面完成了它,所以我可能会弄清楚,因为我可以在那里更好地调试它,但我更愿意只在 xaml 中进行。

我正在使用

<ResourceDictionary Source="DaedalusGraphViewer/ResourceDictionaries/GraphViewerBrushes.xaml" />

在别处做同样的事情,所以我看不出我做错了什么。

  <Button 
Height="50"
VerticalAlignment="Top"
>
<Image Width="50" Height="50" Source="Images/zoominbutton.bmp" Stretch="Fill"/>
</Button>

编辑:仍然没有运气,不确定我做错了什么。

我修改了代码以使用更小的测试用例。

理想情况下,我希望能够从 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);
}
}
}

我现在的目标只是在主 Canvas 上显示图像。什么时候需要使用绝对 uri,什么时候可以使用相对 uri。我已经阅读了很多使用相对 uris 的 xaml 示例,所以我认为这是可能的。

最佳答案

替换 YOURAPPNAME

<Image Width="50" Height="50" Source="/YOURAPPNAME;component/Images/zoominbutton.bmp" Stretch="Fill"/>

关于c# - 如何在 wpf 中设置来自 xaml 的相对 uri?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341066/

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