2012-04-16 18 views
0

我想顯示電影的日期比今天要屏幕。我一整天都在閱讀不同的主題,購買我無法讓webRequest工作。HttpWebRequest的XML解析不顯示任何東西

基本上我有webClient的工作代碼,但我希望UI是響應,所以我決定使用httpWebRequest保持xml解析關閉UI線程。

public partial class MainPage : PhoneApplicationPage { 



public MainPage() { 
    InitializeComponent(); 
} 

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { 
    DoHttpWebRequest(); 
} 


private void DoHttpWebRequest() { 
    string url = "http://www.cinamon.ee/rss/schedule/1001.xml"; 
    var request = HttpWebRequest.Create(url); 
    var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request); 
} 

private void ResponseCallback(IAsyncResult result) { 
    var request = (HttpWebRequest)result.AsyncState; 
    var response = request.EndGetResponse(result); 

    using (var stream = response.GetResponseStream()) { 

    XDocument scheduleXml = XDocument.Load(stream); 
    var todayMovies = from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
         where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
         DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0 
         select new Movie() { 
         MoviePicture = new BitmapImage(new Uri((string)query.Element("images").Element("imageType2").Value, UriKind.RelativeOrAbsolute)), 
         MovieName = (string)query.Element("title"), 
         MovieId = (string)query.Element("movieId"), 
         MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
         }; 

    // Removing duplicate movies from list. 
    List<Movie> todayList = todayMovies.ToList(); 
    IEnumerable<Movie> noDuplicates3 = todayList.Distinct(new MovieComparer()); 

    // Adding to the UI 
    Dispatcher.BeginInvoke(() => { 
     todayBox.ItemsSource = noDuplicates.ToList(); 
    }); 
    } 

} 
} 

有沒有人有看到這段代碼有什麼錯誤的想法?

謝謝你提前 編輯。這是我基於我的解決方案的鏈接 - http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/594e1422-3b69-4cd2-a09b-fb500d5eb1d8

EDIT2。我的Mainpage.xaml

<StackPanel x:Name="TodayPanel" Grid.Row="1" Margin="10,5,10,10" Orientation="Horizontal" Height="580" Background="#90000000" > 
     <ListBox x:Name="todayBox"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
      <HyperlinkButton x:Name="hyperLinkButton" Style="{StaticResource HyperlinkButtonStyle1}" CommandParameter="{Binding MovieId}" Tap="hyperLinkButton_Tap"> 
       <HyperlinkButton.Content> 
       <StackPanel Margin="10" Grid.Row="1" Orientation="Horizontal"> 
        <Image Source="{Binding MoviePicture}" /> 
        <StackPanel Margin="10" Grid.Row="1" Orientation="Vertical"> 
        <TextBlock TextWrapping="Wrap" Margin="10, 5, 10, 5" Width="200" FontFamily="Trebuchet MS" Foreground="Orange" VerticalAlignment="Top"> 
             <Run Text="{Binding MovieName}"/> 
             <LineBreak></LineBreak> 
        </TextBlock> 
        <TextBlock TextWrapping="Wrap" Width="200" FontFamily="Trebuchet MS" Foreground="White" VerticalAlignment="Bottom"> 
             <Run Text="Järgmine seanss: "/> 
             <LineBreak></LineBreak> 
             <Run Text="{Binding MovieSoonest}"/> 
        </TextBlock> 
        </StackPanel> 
       </StackPanel> 
       </HyperlinkButton.Content> 
      </HyperlinkButton> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 

和我編輯後的代碼。

private void DoHttpWebRequest() { 
    string url = "http://www.cinamon.ee/rss/schedule/1001.xml"; 
    var request = HttpWebRequest.Create(url); 
    var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request); 
} 

private void ResponseCallback(IAsyncResult result) { 
    var request = (HttpWebRequest)result.AsyncState; 
    var response = request.EndGetResponse(result); 

    // Adding to the UI 
    Dispatcher.BeginInvoke(() => { 
    IEnumerable<Movie> todayMovies; 
    using (var stream = response.GetResponseStream()) { 

     XDocument scheduleXml = XDocument.Load(stream); 
     todayMovies = from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
        where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
        DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0 
        select new Movie() { 
         MoviePicture = new BitmapImage(new Uri((string)query.Element("images").Element("imageType2").Value, UriKind.RelativeOrAbsolute)), 
         MovieName = (string)query.Element("title"), 
         MovieId = (string)query.Element("movieId"), 
         MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
        }; 
    } 

     var todayList = todayMovies.ToList(); 
     //IEnumerable<Movie> noDuplicates = movieList.Distinct(new MovieComparer()); 

     todayBox.ItemsSource = todayList.ToList(); 



    }); 

回答

1

我試過你的代碼,並得到UnauthorizedAccessException。通過改變Dispactcher.Begininvoke委託的範圍內它的工作原理如下:

private void ResponseCallback(IAsyncResult result){ 
var request = (HttpWebRequest) result.AsyncState; 
var response = request.EndGetResponse(result); 
// Adding to the UI 
Dispatcher.BeginInvoke(() => 
{ 
IEnumerable<Movie> todayMovies; 
using (var stream = response.GetResponseStream()) 
{ 

    XDocument scheduleXml = XDocument.Load(stream); 
    todayMovies = 
     from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
     where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
       DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 
       0 
     select new Movie() 
        { 
         MoviePicture = 
          new BitmapImage(
          new Uri((string) query.Element("images").Element("imageType2").Value, 
            UriKind.RelativeOrAbsolute)), 
         MovieName = (string) query.Element("title"), 
         MovieId = (string) query.Element("movieId"), 
         MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
        }; 

} 
// Removing duplicate movies from list. 
var todayList = todayMovies.ToList(); 
    //IEnumerable<Movie> noDuplicates3 = todayList.Distinct(new MovieComparer()); 


            todayBox.ItemsSource = todayList.ToList(); 
           }); 

}

但是你可以使用RestSharp庫(你會發現它的NuGet),以使其更容易。請檢查下面的代碼:

public void RestSample(){ 
var client = new RestClient 
{ 
    BaseUrl = "http://www.cinamon.ee/" 
}; 

var request = new RestRequest 
{ 
    Resource = "rss/schedule/1001.xml" 
}; 

client.ExecuteAsync(request, (a) => 
{ 
    if (a.StatusCode == HttpStatusCode.OK) 
    { 
     var scheduleXml = XDocument.Parse(a.Content); 

     var todayMovies = from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show") 
          where DateTime.Parse(query.Element("showDateTime").Value).Date.Equals(DateTime.Now.Date) && 
          DateTime.Parse(query.Element("showDateTime").Value).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0 
          select new Movie() 
          { 
           MoviePicture = new BitmapImage(new Uri((string)query.Element("images").Element("imageType2").Value, UriKind.RelativeOrAbsolute)), 
           MovieName = (string)query.Element("title"), 
           MovieId = (string)query.Element("movieId"), 
           MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm") 
          }; 

     // Removing duplicate movies from list. 
     List<Movie> todayList = todayMovies.ToList(); 
     //IEnumerable<Movie> noDuplicates = todayList.Distinct(new MovieComparer()); 

     // Adding to the UI 
     Dispatcher.BeginInvoke(() => 
     { 
      todayBox.ItemsSource = todayList.ToList(); 
     }); 
    } 
    else 
    { 
     //error 
    } 
}); 

}

試試吧,讓我們知道...

編輯:xaml.cs的DataTemplate:

 <StackPanel x:Name="TodayPanel" Grid.Row="1" Margin="10,5,0,10" Orientation="Horizontal" Height="580" Background="#90000000" > 
     <ListBox x:Name="todayBox" Width="468"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Margin="10" Orientation="Horizontal"> 
           <Image Source="{Binding MoviePicture, FallbackValue=http://www.cinamon.ee/visinternetticketing/images/movies/NowShowingComingSoon/HungerGames.jpg}" /> 
           <StackPanel Margin="10" Grid.Row="1" Orientation="Vertical"> 
            <TextBlock TextWrapping="Wrap" Margin="10, 5, 10, 5" FontFamily="Trebuchet MS" Foreground="Orange" VerticalAlignment="Top" Text="{Binding MovieName}"/> 
            <TextBlock TextWrapping="Wrap" FontFamily="Trebuchet MS" Foreground="White" VerticalAlignment="Bottom" Text="{Binding MovieSoonest}"/> 
           </StackPanel> 
           <HyperlinkButton x:Name="hyperLinkButton" CommandParameter="{Binding MovieId}" /> 
          </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 

RECALL 變化MovePicture從物業BitmapImage字符串

That's my result using DataTemplate above

+0

我想你第一次發佈的解決方案,但它仍然不會工作。沒有錯誤,沒有任何東西。該頁面只是空的。 還沒有嘗試過RestSharp,因爲恐怕我無法實現「加載欄」,因爲該庫可用的教程數量較少。我以後會嘗試,也許這會起作用 – JoonasL 2012-04-16 14:46:40

+0

當你什麼都不說,今天什麼都沒有出現在?你是否創建了它的DataTemplate?我試了一下,它適用於我。請讓我知道.. – 2012-04-16 15:21:06

+0

我編輯我的原始帖子與我的mainpage.xaml和.cs。 dataTemplate必須正確,因爲當我再次使用webClient – JoonasL 2012-04-16 15:48:30