如何在C#代码中添加图像url

如何在C#代码中添加图像url,c#,wpf,xaml,c#-4.0,c#-3.0,C#,Wpf,Xaml,C# 4.0,C# 3.0,我有一些c#代码,我想通过url在c#代码中添加一些图像,但没有添加,所以请告诉我这段代码中有什么错误。 我的项目名称是ProjectDemo,我有一个文件夹图像,它可以维护所有图像 private void Window_Loaded(object sender, RoutedEventArgs e) { collection = new ObservableCollection<Image> { new Image{ Heig

我有一些c#代码,我想通过url在c#代码中添加一些图像,但没有添加,所以请告诉我这段代码中有什么错误。 我的项目名称是ProjectDemo,我有一个文件夹图像,它可以维护所有图像

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        collection = new ObservableCollection<Image> { 
            new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/highway.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/highway-1.png", UriKind.Relative))}, 
            new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo/images/part-5.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/part-6.png", UriKind.Relative))} ,
             new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/part-7.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/part-8.png", UriKind.Relative))} ,
             new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/speed-1.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/speedroad.png", UriKind.Relative))} 
         };
         FirstListBox.Items.Add(collection[0]);
         FirstListBox.Items.Add(collection[1]);
         FirstListBox.Items.Add(collection[2]);
         FirstListBox.Items.Add(collection[3]);
         FirstListBox.Items.Add(collection[4]);
         FirstListBox.Items.Add(collection[5]);
         FirstListBox.Items.Add(collection[6]);
         FirstListBox.Items.Add(collection[7]);

    }
private void Window\u已加载(对象发送方,路由目标)
{
集合=新的可观察集合{
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/highway.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/highway-1.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo/images/part-5.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/part-6.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/part-7.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/part-8.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/speed-1.png”,UriKind.Relative))},
新图像{Height=128,Width=128,Source=new-BitmapImage(新Uri(“/ProjectDemo;component/images/speedroad.png”,UriKind.Relative))}
};
FirstListBox.Items.Add(集合[0]);
FirstListBox.Items.Add(集合[1]);
FirstListBox.Items.Add(集合[2]);
FirstListBox.Items.Add(集合[3]);
FirstListBox.Items.Add(集合[4]);
FirstListBox.Items.Add(集合[5]);
FirstListBox.Items.Add(集合[6]);
FirstListBox.Items.Add(集合[7]);
}

这只是一个友好的建议,在用wpf编写单行代码之前先学习MVVM

首先确保项目中包含图像,并且它们的
BuildAction
“资源”

然后在你的虚拟机或代码背后你可以做什么

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            InitializeImages();
            DataContext = this;

        }

        private void InitializeImages()
        {
            ImageModels.Add(new ImageModel
            {
                Source = new BitmapImage(new Uri("/ProjectDemo;component/images/highway.png", UriKind.Relative))
            });
            ImageModels.Add(new ImageModel
            {
                Source = new BitmapImage(new Uri("/ProjectDemo;component/images/highway-1.png", UriKind.Relative))
            });
            ImageModels.Add(new ImageModel
            {
                Source = new BitmapImage(new Uri("/ProjectDemo;component/images/part-5.png", UriKind.Relative))
            });
        }


        ObservableCollection<ImageModel> _imageModels = new ObservableCollection<ImageModel>();

        ObservableCollection<ImageModel> ImageModels
        {
            get { return _imageModels; }
        }
    }

    public class ImageModel : INotifyPropertyChanged
    {
        private ImageSource imageSource;

        public ImageSource Source
        {
            get { return imageSource; }
            set
            {
                imageSource = value;
                RaisePropertyChanged("Source");
            }
        }

        private void RaisePropertyChanged(string propName)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(propName));
            }
        }


    public event PropertyChangedEventHandler PropertyChanged;
    }
你能试试这个吗

Source = new BitmapImage(new Uri(@"pack://application:,,,/ProjectDemo;component/images/highway.png"));

我建议写绝对值,例如
newuri(“pack://application:,,,,/ProjectDemo;component/images/highway.png“
而不是
新的Uri(“/ProjectDemo;component/images/highway.png”,UriKind.Relative)
@Clemens,而且在视图模型中不需要严格的位图。您也可以使用URI字符串,因为WPF提供了从
string
(和
URI
)到
ImageSource
@Clemens的内置自动转换,我认为只有当我们直接在xaml中设置字符串时,这种转换才成立。如果我们将其绑定到string属性,它不会将其转换为ImageSource,这是不正确的。WPF会自动将字符串或URI转换为ImageSource,而不管您是直接在XAML中编写,还是绑定到属性。试试看。
Source = new BitmapImage(new Uri(@"pack://application:,,,/ProjectDemo;component/images/highway.png"));