在WPF C#应用程序中按字符串显示图像
我在资源文件夹中将图像作为资源 我想在ImageController中按字符串值显示图像 下面是我将要使用的代码在WPF C#应用程序中按字符串显示图像,c#,wpf,xaml,C#,Wpf,Xaml,我在资源文件夹中将图像作为资源 我想在ImageController中按字符串值显示图像 下面是我将要使用的代码 <UserControl x:Class="WinIPTVApp.Pages.Content.Views.MenuItemUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=&quo
<UserControl x:Class="WinIPTVApp.Pages.Content.Views.MenuItemUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WinIPTVApp.Pages.Content.Views"
mc:Ignorable="d">
<Grid>
<Image Source="" x:Name="Icon"/>
<ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15"
Foreground="White" />
</Grid>
</UserControl>
请帮我解决这个问题。
提前谢谢。我刚找到答案,效果很好
Icon.Source = new BitmapImage(new Uri($"pack://application:,,,/Resources/{user_icon}"));
“不工作”不是一个有用的问题陈述。请让它包括一个适当的,以及详细的解释代码现在做什么,这是如何不同于你想要的,以及你具体需要什么帮助。确保包含生成的任何错误消息的逐字文本。别忘了检查调试器输出中的错误消息,例如异常和绑定错误。有几件事情显然是错误的。不能重新指定图标字段。不应使用WinForms图像或位图类。必须使用icon变量的值,而不是字符串文字“icon”。假设该图标包含一个有效的图像URL或图像文件路径,这应该可以工作:
image.Source=new-BitmapImage(新Uri(图标))代码>请参见此:。假设名为Resources的项目文件夹中有一个文件user.png,则该文件的生成操作应设置为Resource
。它将由资源文件包URI加载,如Image.Source=new-BitmapImage(新URI(“pack://application:,,,/Resources/user.png”)
或使用user_icon变量:Image.Source=new BitmapImage(新Uri($)pack://application:,,,/Resources/{user_icon}”)谢谢你的好意和帮助,@Clemens
Icon.Source = new BitmapImage(new Uri($"pack://application:,,,/Resources/{user_icon}"));