0

我正在尝试显示比今天要放映的电影的电影日期。我一整天都在阅读不同的主题,购买我无法让 webRequest 工作。

基本上我有使用 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

编辑2。我的主页.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();



    });
4

1 回答 1

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 数据模板:

        <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 更改为 string

这是我使用上面的 DataTemplate 的结果

于 2012-04-16T13:29:02.567 回答