2014-11-03 105 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