1

我将一些值从一页传递到下一页。其中一个值位于子数组中。因为Artist数组中可能有多个值,我想说如果计数大于一,则传递“各种艺术家”,否则传递单个艺术家值。这是我到目前为止所拥有的。

这是我的课

public class RootObject
{
   //public MetadataRelease metadata { get; set; }
    public Results results = new Results();

    public IEnumerator<Results> GetEnumerator()
    {
        return this.results.GetEnumerator();
    }
}
public class Results
{
    public Release release { get; set; }
    public List<Track> tracks { get; set; }
    //public List<UsersAlsoBought2> usersAlsoBought { get; set; }
    //public List<LatestFromLabel2> latestFromLabel { get; set; }

    internal IEnumerator<Results> GetEnumerator()
    {
        throw new NotImplementedException();
    }
}
public class Track
{
    public int id { get; set; }
    public bool selected { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public bool active { get; set; }
    public string mixName { get; set; }
    public string title { get; set; }
    public string slug { get; set; }
    public string isrc { get; set; }
    public string releaseDate { get; set; }
    public string publishDate { get; set; }
    public string sampleUrl { get; set; }
    public string rtmpStreamUrl { get; set; }
    public bool exclusive { get; set; }
    public Price2 price { get; set; }
    public AudioFormatFee2 audioFormatFee { get; set; }
    public string currentStatus { get; set; }
    public string length { get; set; }
    public int bpm { get; set; }
    public Key key { get; set; }
    public string saleType { get; set; }
    public List<Artist2> artists { get; set; } 
    public List<Genre2> genres { get; set; }
    public List<object> subGenres { get; set; }
    public List<object> charts { get; set; }
    public Release2 release { get; set; }
    public Label2 label { get; set; }
    public Images2 images { get; set; }
    public DynamicImages2 dynamicImages { get; set; }
}
public class Artist2
{
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public string type { get; set; }
}

从选定项目中提取数据的代码。

    public void musicSampleSelectedHandler(object sender, RoutedEventArgs e)
    {
        Track selectedTrack = (sender as Image).DataContext as Track;
        ListBoxItem pressedItem = this.listReleaseMain.ItemContainerGenerator.ContainerFromItem(selectedTrack) as ListBoxItem;

        if (pressedItem != null)
        {
            string _rN = selectedTrack.release.name;
            string _rT = selectedTrack.title;
            string _rS = selectedTrack.sampleUrl;
            string _rI = selectedTrack.images.large.url;
            string _rA;

            if (selectedTrack.artists.Count > 1)
            {
                _rA = "Various Artists";
            }
            else
            {
                for (int i = 0; i <= selectedTrack.artists.Count - 1; i++)
                    _rA += "," + selectedTrack.artists[i].name;
            }

            this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseUrl=" + _rS + "&releaseTrack=" + _rT + "&releaseImg=" + _rI + "&releaseName=" + _rN + "&releaseArtist=" + _rA, UriKind.Relative));
        }
    }

更新

我现在_ra在 for 循环中得到“使用未分配的局部变量”

4

1 回答 1

1

这是因为您在“if/else”语句中声明了 _rA。您应该在 if/else 之外声明 _rA 以及其他声明。

几个问题...

string _rA = selectedTrack.artists.name应该string _rA = selectedTrack.artists[i].name吗?

if (selectedTrack.artists.Count > 1) 
            { 
                string _rA = "Various Artists"; 
            } 
            else 
            { 
                for (int i = 0; i <= selectedTrack.artists.Count; i++) 
                { 
                    string _rA = selectedTrack.artists.name; //Unable to access name vaule here 
                } 
            } 

以上不应该是:

string _rA;

if (selectedTrack.artists.Count > 1) 

{ 


    for (int i = 0; i <= selectedTrack.artists.Count; i++)
        _rA += "," + selectedTrack.artists[i].name; 
}
else
{
_rA = "Various Artists"; 


}
于 2012-04-30T02:39:31.023 回答