1

あるページから次のページにいくつかの値を渡します。値の1つはサブ配列にあります。配列内に複数の値が存在する可能性があるためArtist、カウントが1より大きい場合は「VariousArtists」を渡します。それ以外の場合は、単一のアーティストの値を渡します。ここに私がこれまでに持っているものがあります。

これが私のクラスです

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));
        }
    }

アップデート

_raforループ内で「割り当てられていないローカル変数の使用」を取得しています

4

1 に答える 1

1

これは、「if/else」ステートメント内で_rAを宣言しているためです。_rAは、他の宣言とともにif/elseの外で宣言する必要があります。

いくつかの質問...

すべきでは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 に答える