C# 无法将ViewModel绑定到视图
我想在Windows Phone 8上的长列表选择器中显示图片列表 这就是我的模型:C# 无法将ViewModel绑定到视图,c#,xaml,windows-phone-8,mvvm,C#,Xaml,Windows Phone 8,Mvvm,我想在Windows Phone 8上的长列表选择器中显示图片列表 这就是我的模型: public class LocationMediaModel : INotifyPropertyChanged { private string _id; public string ID { get { return _id; } set { if (val
public class LocationMediaModel : INotifyPropertyChanged
{
private string _id;
public string ID
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("ID");
}
}
}
private string _mediatype;
public string MediaType
{
get
{
return _mediatype;
}
set
{
if (value != _mediatype)
{
_mediatype = value;
NotifyPropertyChanged("MediaType");
}
}
}
private string _url;
public string URL
{
get
{
return _url;
}
set
{
if (value != _url)
{
_url = value;
NotifyPropertyChanged("URL");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
以下是我的ViewModel:
public class LocationMediaViewModel : INotifyPropertyChanged
{
public LocationMediaViewModel()
{
this.MediaItems = new ObservableCollection<LocationMediaModel>();
}
public ObservableCollection<LocationMediaModel> MediaItems { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
// Properties vom Model <-> ViewModel:
private string _id;
public string ID
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("ID");
}
}
}
private string _mediatype;
public string MediaType
{
get
{
return _mediatype;
}
set
{
if (value != _mediatype)
{
_mediatype = value;
NotifyPropertyChanged("MediaType");
}
}
}
private string _url;
public string URL
{
get
{
return _url;
}
set
{
if (value != _url)
{
_url = value;
NotifyPropertyChanged("URL");
}
}
}
public bool IsDataLoaded
{
get;
private set;
}
public void LoadData()
{
// Sample data; replace with real data
this.MediaItems.Add(new LocationMediaModel() { ID = "3e3b1e48-8463-424b-8256-15c569358dfb", MediaType = "jpg", URL = "http://media.contoso.com/thumbs/f3be161f-cffc-485e-8f1d-52cfcebc1c79.jpg" });
this.MediaItems.Add(new LocationMediaModel() { ID = "e5e7cb87-04bb-498a-ab57-83bd5c3425c4", MediaType = "jpg", URL = "http://media.contoso.com/thumbs/99FBBBB0-9C16-4E0A-B6E7-A1A709638D06.jpg" });
this.MediaItems.Add(new LocationMediaModel() { ID = "b45931bf-0778-45cc-9efa-fc1aa397ccd9", MediaType = "jpg", URL = "http://media.contoso.com/thumbs/dff96a4f-f22f-4c6d-af24-504a1c80b7c4.jpg" });
this.MediaItems.Add(new LocationMediaModel() { ID = "a23e31b1-9232-086c23fe2279ab1bb22dd0", MediaType = "jpg", URL = "http://media.contoso.com/thumbs/1d1315c7-3292-4e67-8189-5db5393ec801.jpg" });
this.MediaItems.Add(new LocationMediaModel() { ID = "22991670-32b2-45f6-be69-3892b9587865", MediaType = "jpg", URL = "http://media.contoso.com/thumbs/433EA3D7-B9BF-4F3E-96C8-EEC1B9B04896.jpg" });
this.IsDataLoaded = true;
}
}
公共类LocationMediaViewModel:INotifyPropertyChanged
{
公共位置MediaViewModel()
{
this.mediateItems=新的ObservableCollection();
}
公共ObservableCollection MediaItems{get;private set;}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
//属性vom模型视图模型:
私有字符串_id;
公共字符串ID
{
得到
{
返回_id;
}
设置
{
如果(值!=\u id)
{
_id=值;
NotifyPropertyChanged(“ID”);
}
}
}
私有字符串_mediatype;
公共字符串媒体类型
{
得到
{
返回mediatype;
}
设置
{
如果(值!=\u mediatype)
{
_mediatype=值;
NotifyPropertyChanged(“MediaType”);
}
}
}
私有字符串url;
公共字符串URL
{
得到
{
返回url;
}
设置
{
如果(值!=\u url)
{
_url=值;
NotifyPropertyChanged(“URL”);
}
}
}
公共布尔值已加载
{
得到;
私人设置;
}
公共void LoadData()
{
//示例数据;替换为真实数据
this.mediateItems.Add(新位置MediaModel(){ID=“3e3b1e48-8463-424b-8256-15c569358dfb”,MediaType=“jpg”,URL=”http://media.contoso.com/thumbs/f3be161f-cffc-485e-8f1d-52cfcebc1c79.jpg" });
this.mediateItems.Add(new LocationMediaModel(){ID=“e5e7cb87-04bb-498a-ab57-83bd5c3425c4”,MediaType=“jpg”,URL=”http://media.contoso.com/thumbs/99FBBBB0-9C16-4E0A-B6E7-A1A709638D06.jpg" });
this.mediateItems.Add(新位置MediaModel(){ID=“b45931bf-0778-45cc-9efa-fc1aa397ccd9”,MediaType=“jpg”,URL=”http://media.contoso.com/thumbs/dff96a4f-f22f-4c6d-af24-504a1c80b7c4.jpg" });
添加(新位置MediaModel(){ID=“a23e31b1-9232-086c23fe2279ab1bb22dd0”,MediaType=“jpg”,URL=”http://media.contoso.com/thumbs/1d1315c7-3292-4e67-8189-5db5393ec801.jpg" });
this.mediateItems.Add(new LocationMediaModel(){ID=“22991670-32b2-45f6-be69-3892b9587865”,MediaType=“jpg”,URL=”http://media.contoso.com/thumbs/433EA3D7-B9BF-4F3E-96C8-EEC1B9B04896.jpg" });
this.IsDataLoaded=true;
}
}
最后是我的XAML视图:
<phone:PhoneApplicationPage
x:Class="WhaGoO.LocationPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignData DesignData/LocationDetailsSampleData.xaml}"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<phone:Pivot x:Name="pvtMainPivot" SelectionChanged="evt_PivotControl_SelectionChanged">
<!-- ++++++++++++++++++ Header -->
<phone:Pivot.Title>
<StackPanel HorizontalAlignment="Center">
<Image Stretch="None" HorizontalAlignment="Left" Margin="-5,0,0,0" MinWidth="50" MaxHeight="50" Source="/mAppData/logo.png"/>
</StackPanel>
</phone:Pivot.Title>
<!-- ++++++++++++++++++ EREIGNISSE -->
<phone:PivotItem x:Name="pivotitemFavoriten" Header="Ereignisse">
<StackPanel Margin="0,0,0,15" >
<Grid VerticalAlignment="Top" Margin="0,0,5,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" TextTrimming="WordEllipsis" Text="LocationName" TextWrapping="NoWrap" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Top" Margin="0,0,0,22" />
<Image Grid.Column="0" Width="138" Height="25" Source="/mAppData/stars-3.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0"/>
<TextBlock Grid.Column="1" Text="distance" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Right" Margin="0,0,-3,40" VerticalAlignment="Bottom"/>
<TextBlock Grid.Column="1" Text="lastupload" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Right" Margin="0,0,-3,20" VerticalAlignment="Bottom"/>
<TextBlock Grid.Column="1" Text="ratingscnt" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Right" Margin="0" VerticalAlignment="Bottom"/>
</Grid>
<TextBlock Grid.Column="1" Text="Marlene-Dietrich-Platz 1" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Bottom"/>
<TextBlock Grid.Column="1" Text="Berlin" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Bottom"/>
<phone:LongListSelector Name="lls_PhotoHub" Margin="0" IsGroupingEnabled="False" LayoutMode="Grid" DataContext="LocationMediaViewModel" ItemsSource="{Binding MediaItems}" GridCellSize="108,108" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Image Width="100" Height="100" Source="{Binding URL}" />
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</StackPanel>
</phone:PivotItem>
<!-- ++++++++++++++++++ 2.PivotItem-->
<phone:PivotItem x:Name="pivotitemUmgebung" Header="Karte">
</phone:PivotItem>
</phone:Pivot>
</Grid>
</phone:PhoneApplicationPage>
但是绑定似乎不起作用->没有显示图像
有人能帮忙吗?正如Florent Gz指出的,我也没有看到任何DataContext初始化 在PhoneApplicationPage code behind的构造函数中,添加以下内容:
this.DataContext = new LocationMediaViewModel();
似乎您的datacontext设置得不正确。我在App.xaml.cs中没有看到来自VS的示例代码。。。现在它工作得很好!谢谢!