繁体   English   中英

绑定无效(Windows Phone)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM