C# 绑定赢得';t工作(windows phone)
我的装订有问题。反序列化成功,反序列化后我为绑定创建了一个集合,这里是我的问题(来自输出): 这是我的班级:C# 绑定赢得';t工作(windows phone),c#,xaml,binding,windows-phone,C#,Xaml,Binding,Windows Phone,我的装订有问题。反序列化成功,反序列化后我为绑定创建了一个集合,这里是我的问题(来自输出): 这是我的班级: internal class VkResponse { [JsonProperty("response")] public List<Song> Musics { get; set; } } public class Song { public Song(string artist, string song, string uri)
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; }
}
内部类VkResponse
{
[JsonProperty(“响应”)]
公共列表音乐{get;set;}
}
公开课歌曲
{
公共歌曲(弦乐艺术家、弦乐歌曲、弦乐uri)
{
这个。艺术家=艺术家;
this.SongName=song;
this.SongUri=uri;
}
[JsonProperty(PropertyName=“id”)]
公共int Id{get;set;}
[JsonProperty(PropertyName=“artist”)]
公共字符串艺术家{get;set;}
[JsonProperty(PropertyName=“title”)]
公共字符串SongName{get;set;}
[JsonProperty(PropertyName=“url”)]
公共字符串SongUri{get;set;}
[JsonProperty(PropertyName=“dutation”)]
公共整数持续时间{get;set;}
[JsonProperty(PropertyName=“owner\u id”)]
public int OwnerId{get;set;}
[JsonProperty(PropertyName=“歌词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;
}
}
}
public部分类主页:PhoneApplicationPage
{
公共ObservableCollection歌曲调用{get;set;}
// Конструктор
公共主页()
{
初始化组件();
已加载+=主页面_已加载;
}
已加载私有void主页(对象发送方、路由目标)
{
尝试
{
SongsColl=新的ObservableCollection();
这个.SpinningAnimation.Begin();
JObject=JObject.Parse(json);
JArray数组=(JArray)jobject[“响应”];
var response=JsonConvert.DeserializeObject(json);
对于(int count=0;count
小更新,抱歉,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;
}
publicobservableCollection歌曲{get;set;}
公共歌曲已选择歌曲{get;set;}
已加载私有void主页(对象发送方、路由目标)
{
...
歌曲=回应。音乐;
}
在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
binding-Mode=TwoWay
已指定。这意味着当所选项目更改时,将从视图中更新属性SelectedSong
Listbox项模板是必需的,因为Listbox不知道如何显示歌曲
对象。您还可以在Song
类中重写ToString
方法,但不建议这样做
编辑
最后,当您为歌曲集指定新值时,视图模型应通知视图。您也可以共享XAML吗?对不起,这是我的xaml:“代码”你好,Nikita为了完整起见,你能更新你的问题并添加xaml吗?我决定删除我的mvvm,只是因为那没用。如果您想查看我的xaml,我在上面添加了注释。谢谢你们的帮助和时间。第一次理解mvvm模式及其缺点并不容易。当您不使用UnitTests时,MVVM是有用的事件。它是抽象层,编程UI。(如果你需要的话,我可以用俄语帮助你)我在解释我的想法方面有问题,但我试图提高我的知识,所以让我们回到我的问题上来。您有其他绑定(ObservableCollection/List)集合的方法吗?若你们有,请告诉我。你们可以像以前在代码隐藏中一样将集合绑定到列表框。只需设置
itemsource=songs
<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>