[英]Binding won't work(windows phone)
我綁定有問題。 反序列化成功,反序列化后我收集了一個要綁定的集合,這是我的問題(來自輸出):
(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>
我想您想在單擊列表框中的元素時顯示歌曲標題列表並顯示歌曲詳細信息。
為此,您必須具有“歌曲”和特殊屬性的集合,其中將包含選定的歌曲。
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
對象。 您也可以在Song
類中重寫ToString
方法,但不建議這樣做。
編輯最后,當您為Songs
集合分配新值時,您的視圖模型應通知該視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.