我有一些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]);
}
答案 0 :(得分:1)
只是一个友好的建议,在编写wpf中的单行代码之前学习MVVM。
首先确保项目中包含图片且BuildAction
为"Resource"
然后在你的VM或代码后面你可以做
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;
}
并在View
<ListBox ItemsSource="{Binding ImageModels}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Height="128" Width="128" Source="{Binding Source}"></Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 1 :(得分:0)
你可以试试这个:
Source = new BitmapImage(new Uri(@"pack://application:,,,/ProjectDemo;component/images/highway.png"));