2010-11-10 54 views
0

我试图访问在线XML文件并在Windows Phone 7 Silverlight应用程序中显示它的内容。我没有收到任何错误,但是当模拟时,XML文件中没有显示任何内容。从我在网上收集到的信息来看,我只是简单地把事情搞乱了。我只是不确定是什么。Silverlight WP7应用程序不显示XML内容

MainPage.xaml.cs中:

namespace TwitterMix 
{ 
public partial class MainPage : PhoneApplicationPage 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 


    private void GetRoster() 
    { 
     WebClient rstr = new WebClient(); 

     rstr.DownloadStringCompleted += new DownloadStringCompletedEventHandler(roster_DownloadStringCompleted); 
     rstr.DownloadStringAsync(new Uri("http://www.danfess.com/data.xml")); 
    } 

    void roster_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error != null) 
      return; 

     XElement xmlPersons = XElement.Parse(e.Result); 

     var list = new List<RosterViewModel>(); 

     foreach (XElement person in xmlPersons.Elements("person")) 
     { 
      var name = person.Element("name").Value; 
      var age = person.Element("age").Value; 


      list.Add(new RosterViewModel 
      { 
       Name = name, 
       Age = age, 
      }); 
     } 

     rosterList.ItemsSource = list; 
    } 


    public class RosterViewModel 
    { 
     public string Name { get; set; } 
     public string Age { get; set; } 
    } 


} 
} 

MainPage.xaml中:

<Grid x:Name="ContentPanel" Grid.Row="1"> 
     <ListBox HorizontalAlignment="Left" Name="rosterList" VerticalAlignment="Top" Width="468" Height="600"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Height="132"> 
         <StackPanel Width="370"> 
          <TextBlock Text="{Binding Name}" Foreground="White" FontSize="28" /> 
          <TextBlock Text="{Binding Age}" TextWrapping="Wrap" FontSize="24" Foreground="White" /> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

最后的XML文件的内容:

<?xml version="1.0" encoding="utf-8" ?> 

<roster> 

<person> 
<name>Blake</name> 
<age>25</age> 
</person> 

<person> 
<name>Jane</name> 
<age>29</age> 
</person> 

<person> 
<name>Bryce</name> 
<age>29</age> 
</person> 

<person> 
<name>Colin</name> 
<age>29</age> 
</person> 

</roster> 

任何意见或建议,当然,非常感谢。谢谢大家的帮助!

+0

X-Ref:http://stackoverflow.com/questions/4138694/connect-to-xml-file-at-web-address-for-wp7-app – 2010-11-11 09:25:59

回答

1

我认为你的问题是从DownloadStringCompleted的回调是在UI线程以外的线程上执行的。 Listbox或者plain会忽略你或者抛出一个被调用线程吞下的异常。

您需要在分配itemssource属性之前切换到UI线程。

Dispatcher.Current.BeginInvoke((Action)(()=>rosterList.ItemsSource = list)); 

同样为分配给一个数据绑定到UI元素

+0

这解决了一切!我无法告诉你,当它真正起作用时我有多兴奋,哈哈。无可否认,我比我咀嚼蝙蝠更糟糕,犯了一个愚蠢的错误。再次感谢你的帮助! – Dan 2010-11-11 02:31:35

0

如果你弄清楚这是如何以不同的顺序工作的话,我会非常感兴趣的看到它。我不得不增加大量的开销来实现它。我得到它的工作方式是我的数据类(你的rosterviewmodel)从INotifyPropertyChanged继承和所有暗示。初始化我的数据对象时,我在数据对象的属性更改事件上设置了一个处理程序。然后在处理程序中,您要做的就是将您的堆栈面板的DataContext设置为刚刚更改的对象。

0

任何财产您是否已经验证列表被填充?在绑定到rosterList之前设置一个断点并检查list.Count。

你可以只加载XML像

XDocument xmlPersons = XDocument.Load(e.Result); 

    var list = from query in xmlPersons.Descendants("person") 
        select new RosterViewModel 
        { 
         Name = (string)query.Element("name"), 
         Age = (int)query.Element("age") 
        }; 


    rosterList.ItemsSource = list; 

(手动编辑使用您的VAR名称代码 - 未测试)。

相关问题