我将一个页面中的一些值传递给下一页。其中一个值位于子数组中。因为数组中可能有多个Artist
值,我想说如果计数大于1,则传递“Various Artists”否则传递单个艺术家值。这就是我到目前为止所拥有的。
这是我的课程
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));
}
}
更新
我现在在for循环中得到“使用未分配的局部变量_ra
”
答案 0 :(得分: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";
}