如何在Windows Phone 7应用程序中显示后台办公室的图像?

时间:2014-01-08 11:36:05

标签: c# windows-phone-7

我正在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>

1 个答案:

答案 0 :(得分:0)

将此添加到您的模型

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循环中设置image属性

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