【发布时间】:2013-07-16 11:16:46
【问题描述】:
我无法创建同时显示图片的模型,
我的模型类:
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;
}
}
app.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>
mainpage.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;
mainpage.xaml:
<phone:LongListSelector x:Name="citiesList"
Background="Transparent"
ItemTemplate="{Binding citiesItemTemplate}"
/>
但是现在图像应该显示在哪里,它只显示phoneapp7.Model.City,不知道我做错了什么?
【问题讨论】:
-
使用前需要先实例化
Country(即new_city.Country.Source = bi;)。例如:new_city.Country = new Image(someArgsIfAny)
标签: c# xaml binding windows-phone-8