【问题标题】:How do I display images from back office in windows phone 7 application?如何在 Windows Phone 7 应用程序中显示来自后台的图像?
【发布时间】:2014-01-08 19:58:47
【问题描述】:

我正在 Windows Phone 7 中构建我的第一个应用程序。我需要显示来自 Web 服务的一些数据以及图像。我能够显示数据但不知道如何显示图像。可以输入需要更新的新数据。图像将来自后台,路径将来自 Web 服务。我的网络服务是:

 <string><NewDataSet>
  <UserDetails>
    <id>5</id>
    <News_Title>Audit of Electricity Companies</News_Title>
    <News_Description> Rejecting the contention of private power distributors, the Delhi government today ordered an audit of their finances by the government's national auditor or Comptroller and Auditor General (CAG), fulfilling yet another election promise of the Aam Aadmi Party.

</News_Description>
    <Date_Start>2014-01-03</Date_Start>
    <image_path>news.png</image_path>
  </UserDetails>

将有超过 1 个数据。我可以显示 news_Title、news_description、Date_start。我的cs代码是

 public class Newss
    {
        public string News_Title { get; set; }
        public string News_Description { get; set; }
        public string Date_Start { get; set; }
    }




    public News()
    {
        InitializeComponent();

        KejriwalService.aapSoapClient client = new KejriwalService.aapSoapClient();
        client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted);
        client.getarvindNewsAsync();
    }

    void client_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
    {
        string result = e.Result.ToString();
        List<Newss> listData = new List<Newss>();
        XDocument doc = XDocument.Parse(result);
        // Just as an example of using the namespace...
        //var b = doc.Element("NewDataSet").Value;
        foreach (var location in doc.Descendants("UserDetails"))
        {
            Newss data = new Newss();
            data.News_Title = location.Element("News_Title").Value;

           // data.News_Description = location.Element("News_Description").Value;
            data.Date_Start = location.Element("Date_Start").Value;
            listData.Add(data);
        }
        listBox1.ItemsSource = listData;

    }

我的 xaml 文件是

 <ScrollViewer Margin="12,17,-12,144" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" AllowDrop="False" ManipulationMode="Control">
            <ListBox Name="listBox1" Margin="38,86,38,562">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">

                            <TextBlock Text="{Binding Path=News_Title}"></TextBlock>
                            <TextBlock Text="{Binding Path=News_Description}"></TextBlock>
                            <TextBlock Text="{Binding Path=Date_Start}"></TextBlock>

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

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    将此添加到您的模型中

    public class Newss
    {
        public string News_Title { get; set; }
        public string News_Description { get; set; }
        public string Date_Start { get; set; }
        public string Image_Path { get; set; }
    }
    

    在你的 foreach 循环中设置图像属性

    foreach (var location in doc.Descendants("UserDetails"))
        {
            Newss data = new Newss();
            data.News_Title = location.Element("News_Title").Value;
    
           // data.News_Description = location.Element("News_Description").Value;
            data.Date_Start = location.Element("Date_Start").Value;
            Newss.Image_Path  = location.Element("image_path").Value
            listData.Add(data);
        }
    

    在你的 Xaml 中

    <ScrollViewer Margin="12,17,-12,144" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" AllowDrop="False" ManipulationMode="Control">
            <ListBox Name="listBox1" Margin="38,86,38,562">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
    
                            <TextBlock Text="{Binding Path=News_Title}"></TextBlock>
                            <TextBlock Text="{Binding Path=News_Description}"></TextBlock>
                            <TextBlock Text="{Binding Path=Date_Start}"></TextBlock>
                            <Image Source="{Binding Path=Image_Path}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </ScrollViewer>
    

    显然,请确保您的 xml 有效负载中的 Image_Path 数据是有效的 uri,我会首先将其设置为静态图像,例如“http://static.bbci.co.uk/frameworks/barlesque/2.59.4/orb/4/img/bbc-blocks-dark.png

    【讨论】:

    • 在添加您提供的代码后运行应用程序时,控件移动到 app.xaml.cs 中的以下行 // 要在未处理的异常上执行的代码 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (System.Diagnostics.Debugger.IsAttached) { // 发生了未处理的异常;闯入调试器 System.Diagnostics.Debugger.Break(); } }
    • 展开异常对象 (e) - Message 和 Inner 异常说明了什么?
    • 你能详细说明一下吗?我不明白,我是这个领域的新手
    • 此外,我在这行 Newss.Image_Path = location.Element("image_path").Value 中有一个错误所以我将新闻更改为数据,然后只有程序运行
    • 您的 XML 中似乎不存在 image_path,您的问题 xml 数据状态为“image_path”。这应该是“Image_Path”,因为这是区分大小写的。无论如何,请尝试 Newss.Image_Path = location.Element("image_path") == null ? "NoImage.jpg" : location.Element("image_path").Value;
    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多