我遇到绑定问题。反序列化是成功的,在反序列化后我创建了一个绑定集合,这里我是我的问题(来自输出):
(first property)System.Windows.Data Error: BindingExpression path error: 'Artist' property not found on ''Nirvana'' 'System.String' (HashCode=-816891269). BindingExpression: Path='Artist' DataItem=''Nirvana'' (HashCode=-816891269); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
(second property)System.Windows.Data Error: BindingExpression path error: 'SongName' property not found on ''Nirvana'' 'System.String' (HashCode=-816891269). BindingExpression: Path='SongName' DataItem=''Nirvana'' (HashCode=-816891269); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
这是我的班级:
internal class VkResponse
{
[JsonProperty("response")]
public List<Song> Musics { get; set; }
}
public class Song
{
public Song(string artist, string song, string uri)
{
this.Artist = artist;
this.SongName = song;
this.SongUri = uri;
}
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "artist")]
public string Artist { get; set; }
[JsonProperty(PropertyName = "title")]
public string SongName { get; set; }
[JsonProperty(PropertyName = "url")]
public string SongUri { get; set; }
[JsonProperty(PropertyName = "dutation")]
public int Duration { get; set; }
[JsonProperty(PropertyName = "owner_id")]
public int OwnerId { get; set; }
[JsonProperty(PropertyName = "lyrics_id")]
public int LyricsId { get; set; }
[JsonProperty(PropertyName = "genre_id")]
public int GenreId { get; set; }
}
这是我的反序列化:
public partial class MainPage : PhoneApplicationPage
{
public ObservableCollection<string> SongsColl { get; set; }
// Конструктор
public MainPage()
{
InitializeComponent();
Loaded +=MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
try
{
SongsColl = new ObservableCollection<string>();
this.SpinningAnimation.Begin();
JObject jobject = JObject.Parse(json);
JArray array = (JArray)jobject["response"];
var response = JsonConvert.DeserializeObject<VkResponse>(json);
for (int count = 0; count < response.Musics.Count; count++)
{
//this.sListBox.ItemsSource = response.Musics[count].ToString();
SongsColl.Add(response.Musics[count].Artist.ToString());
SongsColl.Add(response.Musics[count].SongName.ToString());
SongsColl.Add(response.Musics[count].SongUri.ToString());
sListBox.ItemsSource = SongsColl;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
this.SpinningAnimation.Stop();
this.CsEllipse.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
小小的更新,抱歉,XAML:
<ListBox x:Name="sListBox" ItemsSource="{Binding SongColl}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Artist}"/>
<TextBlock Text="{Binding SongName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:0)
我想你想要点击列表框中的元素时显示歌曲标题列表并显示歌曲详细信息。
为此,您必须拥有一组歌曲和特殊属性,其中包含选定的歌曲。
public ObservableCollection<Song> Songs {get;set;}
public Song SelectedSong {get;set;}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
...
Songs = response.Musics;
}
在XAML中:
<TextBox Text={Binding SelectedSong.Title}/>
<TextBox Text={Binding SelectedSong.Artist}/>
<ListBox ItemsSource={Binding Songs} SelectedItem={Binding SelectedSong, Mode=TwoWay}>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text={Binding Title} />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
注意列表框SelectedItem
绑定 - Mode=TwoWay
已指定。这意味着当所选项目发生更改时,属性SelectedSong
将从视图中更新。
列表框项目模板是必需的,因为列表框不知道如何显示Song
对象。您也可以在ToString
课程中覆盖Song
方法,但不建议这样做。
修改强>
最后,您的视图模型应在为Songs
集合分配新值时通知视图。