我无法创建一个也显示图片的模型,
我的模型课:
public class City
{
public string Name
{
get;
set;
}
public Image Country
{
get;
set;
}
}
然后在我的 mainpage.xaml.cs 中:
List<City> source = new List<City>();
BitmapImage bi = new BitmapImage(new Uri("/PhoneApp7;component/Images/test.png", UriKind.Relative));
City new_city = new City();
new_city.Name = "Africa";
new_city.Country.Source = bi;
new_city.Language = "xhosa";
source.Add(new_city);
citiesList.ItemsSource = source;
我得到一个空引用异常,我不确定我做错了什么,是否有另一种方法可以将图像添加到数据绑定源?
我试过这个:
public class City
{
public City(Uri countryUri)
{
Country = new BitmapImage(countryUri);
}
public string Name
{
get;
set;
}
public BitmapImage Country
{
get;
set;
}
public string Language
{
get;
set;
}
}
应用程序.xaml:
<DataTemplate x:Key="citiesItemTemplate">
<StackPanel Grid.Column="1" VerticalAlignment="Top">
<TextBlock Text="{Binding Name}" FontSize="26" Margin="12,-12,12,6"/>
<Image Source="{Binding Path=Country}" />
<TextBlock Text="{Binding Language}" Foreground="Orange" />
</StackPanel>
</DataTemplate>
主页.xaml
List<City> source = new List<City>();
Uri bi = new Uri("/Images/test.png", UriKind.Relative);
City new_city = new City(bi) { Name = "Africa", Language = "xhosa", };
new_city.Name = "Africa";
new_city.Language = "Xhosa";
source.Add(new_city);
citiesList.ItemsSource = source;
主页.xaml:
<phone:LongListSelector x:Name="citiesList"
Background="Transparent"
ItemTemplate="{Binding citiesItemTemplate}"
/>
但是现在应该显示图像的位置,它只显示phoneapp7.Model.City,不确定我做错了什么?