2014-11-03 77 views
-1

对不起,这个愚蠢的问题。我甚至不明白应该给这个问题的标题。选择更改事件是瑞星当我点击Click事件处理程序

我有3个按钮,当我点击它们时,应该显示列表框。 如果我在列表框中选择一个项目,它应该被导航并开始播放。

当我点击一个按钮,列表框显示的是,当一个项目被选中它导航到到其他页面和playing.After执行如果我点击任何按钮,我得到错误,如

第一次机会异常改变的选择类型 'System.NullReferenceException' 发生在tori.dll

型 'System.NullReferenceException' 未处理的异常发生在tori.dll

Archieves .XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,-951,0"> 
     <Button Name="btn1" Click="btn1_Click" Content="Daily" HorizontalAlignment="Left" Margin="0,88,0,0" VerticalAlignment="Top" Width="127" Height="72"/> 
     <Button Name="btn2" Click="btn2_Click" Content="Weekly" HorizontalAlignment="Left" Margin="132,88,0,0" VerticalAlignment="Top" Height="72" Width="140"/> 
     <Button Name="btn3" Click="btn3_Click" Content="CurrentMonth" HorizontalAlignment="Left" Margin="277,88,0,0" VerticalAlignment="Top" Height="72" Width="169"/> 
        <ListBox x:Name="itemsList" Margin="0,225,945,0" 
       SelectionChanged="itemsList_SelectionChanged"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Height="130"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="100"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Image delay:LowProfileImageLoader.UriSource="{Binding ThumbnailUrl}" 
           Grid.Column="0" 
           Width="500" 
           Height="125" 
           VerticalAlignment="Center" 
           HorizontalAlignment="Center" 
           Margin="6"/> 
         <StackPanel Margin="10,20,0,0" 
            Grid.Column="1" 
            Height="60" 
            Orientation="Horizontal" 
            VerticalAlignment="Center"> 
          <TextBlock Text="{Binding title}" 
             FontSize="40" /> 

         </StackPanel> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     </Grid> 

Archieves.xaml.cs:

namespace tori 
{ 
    public partial class Archieves : PhoneApplicationPage 
    { 
     public Archieves() 
     { 
      InitializeComponent(); 
     } 

    private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
     WebClient downloader = new WebClient(); 
     Uri uri = new Uri(" http://www.toucheradio.com/RSSFeed/rssDaily.php ", UriKind.Absolute); 
     downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ChannelDownloaded); 
     downloader.DownloadStringAsync(uri); 
    } 

    void ChannelDownloaded(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Result == null || e.Error != null) 
     { 
      MessageBox.Show("There was an error downloading the XML-file!"); 
     } 


     else 
     { 
      // Deserialize if download succeeds 

      XDocument document = XDocument.Parse(e.Result); 

      var queue = from item in document.Descendants("item") 
         select new Item 
         { 
          title = item.Element("title").Value 
          , 
          link = item.Element("link").Value 
          , 
          pubDate = item.Element("pubDate").Value 
          , 
          ThumbnailUrl = item.Element(item.GetNamespaceOfPrefix("media") + "thumbnail").Attribute("url").Value 
          , 

         }; 

      itemsList.ItemsSource = queue; 
     } 
    } 

private void btn2_Click(object sender, RoutedEventArgs e) 
    { 
     WebClient downloader = new WebClient(); 
     Uri uri = new Uri(" http://www.toucheradio.com/RSSFeed/rssWeekly.php ", UriKind.Absolute); 
     downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Channel1Downloaded); 
     downloader.DownloadStringAsync(uri); 
    } 

     void Channel1Downloaded(object sender, DownloadStringCompletedEventArgs e) 
     { 
      if (e.Result == null || e.Error != null) 
      { 
       MessageBox.Show("There was an error downloading the XML-file!"); 
      } 
     else 
     { 
      // Deserialize if download succeeds 

      XDocument document = XDocument.Parse(e.Result); 

      var queue = from item in document.Descendants("item") 
         select new Item 
         { 
          title = item.Element("title").Value 
          , 
          link = item.Element("link").Value 
          , 
          pubDate = item.Element("pubDate").Value 
          , 
          ThumbnailUrl = item.Element(item.GetNamespaceOfPrefix("media") + "thumbnail").Attribute("url").Value 
          , 

         }; 

      itemsList.ItemsSource = queue; 
     } 
    } 

private void btn3_Click(object sender, RoutedEventArgs e) 
    { 

     WebClient downloader = new WebClient(); 
     Uri uri = new Uri("http://www.toucheradio.com/RSSFeed/rssMonthly.php ", UriKind.Absolute); 
     downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Channel2Downloaded); 
     downloader.DownloadStringAsync(uri); 
    } 

    void Channel2Downloaded(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Result == null || e.Error != null) 
     { 
      MessageBox.Show("There was an error downloading the XML-file!"); 
     } 
     else 
     { 
      // Deserialize if download succeeds 

      XDocument document = XDocument.Parse(e.Result); 

      var queue = from item in document.Descendants("item") 
         select new Item 
         { 
          title = item.Element("title").Value 
          , 
          link = item.Element("link").Value 
          , 
          pubDate = item.Element("pubDate").Value 
          , 

          ThumbnailUrl = item.Element(item.GetNamespaceOfPrefix("media") + "thumbnail").Attribute("url").Value 
          , 

         }; 

      itemsList.ItemsSource = queue; 
     } 
    } 
    private void itemsList_SelectionChanged(Object sender, SelectionChangedEventArgs e) 
    { 
     var app = App.Current as App; 
     app.selectedItem = (Item)itemsList.SelectedItem; 
     this.NavigationService.Navigate(new Uri("/Details.xaml", UriKind.Relative)); 
    } 

Item.cs:

namespace tori 
{ 
     public class Item 
    { 
     public string title { get; set; } 
     public string ThumbnailUrl { get; set; } 
     public string link { get; set; } 
     public string pubDate { get; set; } 
    } 
} 

Details.xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid Height="617" VerticalAlignment="Top" Margin="-27,96,0,0"> 

      <Image x:Name="ThumbnailUrl" 

           Width="279" 
           Height="421" 
           VerticalAlignment="Center" 
           HorizontalAlignment="Center" 
           Margin="0,-29,204,225" /> 
      <Image x:Name="Image1" Tap="Image1_Tap" Margin="46,430,354,92" Source="/Images/pausebutton.png"/> 
     <TextBlock x:Name="title" FontSize="30" TextWrapping="Wrap" Margin="284,57,-50,436" /> 
       <TextBlock x:Name="pubDate" FontSize="25" Margin="284,186,10,363" /> 
      <MediaElement x:Name="MediaElement1" AutoPlay="True" Margin="0,397,20,0" Height="94" VerticalAlignment="Top" /> 

Details.xaml.cs:

namespace tori 
{ 
    public partial class Details : PhoneApplicationPage 
    { 
     Item item; 

    public Details() 
    { 
     InitializeComponent(); 


     var app = App.Current as App; 
     item = app.selectedItem; 
     title.Text = item.title; 
     pubDate.Text = item.pubDate; 
     ThumbnailUrl.Source = new BitmapImage(new Uri(item.ThumbnailUrl, UriKind.RelativeOrAbsolute)); 
     string s = item.link; 
     string url = s.Replace("archivesplayer", "hostArchivesURLForMobile"); 
     WebClient downloader = new WebClient(); 
     Uri uri = new Uri(url,UriKind.Absolute); 
     downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Channel3Downloaded); 
     downloader.DownloadStringAsync(uri); 
    } 

    void Channel3Downloaded(object sender, DownloadStringCompletedEventArgs e) 
    { 
     var textData = (string)e.Result; 
     Regex urlRegex = new Regex("<td height=\"25\" align=\"center\">(?<url>.*)</td>"); 
     MatchCollection mc = urlRegex.Matches(textData); 
     string url = ""; 
     if (mc.Count > 0) 
     { 
      url = mc[0].Groups["url"].Value; 
      MediaElement1.Source = new Uri(url, UriKind.Absolute); 
      MediaElement1.Play(); 
     } 
} 
} 
} 

执行选择项目,如果我点击一个按钮我在此行得到错误后

title.Text = item.title;

任何人都可以请告诉我如何克服这个空的异常。当我点击按钮选择更改事件是提高而不是点击事件。我无法知道这个原因。

请有人帮我这个。

很多预先感谢。

回答

1

当您更改ItemsSource时,会引发SelectionChanged事件。我认为每当你改变ItemsSource时,选择被重置(即 - 设置为空(没有选择))。这应该是这样,否则不在ItemsSource中的项目可能是SelectedItem,这是错误的。

现在,要解决您的问题,只需检查SelectedItem != null中的itemsList_SelectionChanged方法。

别的东西对你的代码:方法btn1_Clickbtn2_Clickbtn3_Click似乎只有细微的差别,所以你可以把大部分的代码在一个方法,只是通过它的URL。这对于ChannelDownloaded方法更重要(因为它们更长)。基本上你想尽可能地重用代码。这使得代码更易于阅读(因为它不是10页,而是一个,可以这么说),并且更易于维护(如果出现错误 - 您只需要在一个地方修复它)。

+0

我已经改变了选择更改事件点击事件,并解决了我的问题。我也试图写在一个方法的代码和重用。但我有点困惑,通过url.Can你可以告诉我点击按钮时传递该网址。感谢你。 – deepu 2014-11-04 04:50:47