0

我在让我的应用程序显示来自网络的图像时遇到了一些问题。可以看出,它在我的示例数据中运行良好:

美好的

但是一个应用程序在模拟器上运行我得到这个,你可以在文本框中看到链接:

坏的

这是我的代码,我做错了什么?

<phone:LongListSelector.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="105" Width="432">
            <!--Replace rectangle with image-->
            <Image>
                <Image.Source>
                    <BitmapImage UriSource="{Binding Image}" CreateOptions="BackgroundCreation"/>
                </Image.Source>
            </Image>
            <StackPanel Width="311" Margin="8,-7,0,0">
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Margin="10,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}" />
                <TextBox Text="{Binding Image}" FontSize="10" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</phone:LongListSelector.ItemTemplate>

http://i.imgur.com/GcwxIpl.png

4

1 回答 1

0

您可以看到图像的唯一原因是您的代码无法下载它们。我刚刚为 windows phone 创建了一个简单的应用程序来手动下载图像(只是为了看看到底发生了什么?)。

代码非常简单:

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  <StackPanel>
     <Image>
       <Image.Source>
         <BitmapImage UriSource="{Binding Image}" CreateOptions="BackgroundCreation"/>
       </Image.Source>
     </Image>
      <Button Content="go" Click="ClickMe"/>
   </StackPanel>
</Grid>

和代码隐藏:

    private void ClickMe(object sender, RoutedEventArgs e)
    {
        //var url = "http://img7.anidb.net/pics/anime/136529.jpg";
        var url = "http://img7.anidb.net/pics/anime/54893.jpg";
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        //req.ContentType = "image/jpeg";
        //req.Accept = "image/jpeg";
        req.Method = "GET";
        req.BeginGetResponse(Callback, req);
    }

    private void Callback(IAsyncResult result)
    {
        try
        {
            HttpWebRequest httpReq = (HttpWebRequest)result.AsyncState;
            HttpWebResponse response = (HttpWebResponse)httpReq.EndGetResponse(result);

            Stream myStream = response.GetResponseStream();
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(myStream);
                var character = new Character();
                character.Image = bmp;
                ContentPanel.DataContext = character;
                //image1.Source = bmp;
            });
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

当我尝试在 WP 上下载图像时,我收到一个带有 Stats 代码的 webException:System.Net.HttpStatusCode.Forbidden 顺便说一句 - 当我尝试使用 Web 浏览器获取图像时,有时会出现此错误。不那么频繁,但它会发生。很可能该网站不允许获取图像。

于 2013-06-09T08:44:20.567 回答