我無法建立一個模型,該模型還顯示一峯,映像創建模型
我的模型類:
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;
我得到一個nullreference異常,我不知道我做錯了什麼,是否有另一種方式添加圖像到數據綁定的源?
我已經試過這樣:
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)' – Leri