简体   繁体   English

Silverlight WP7应用程序不显示XML内容

[英]Silverlight WP7 App Not Displaying XML Contents

I am trying to access an online XML file and display it's contents within a Windows Phone 7 Silverlight app. 我正在尝试访问在线XML文件并在Windows Phone 7 Silverlight应用程序中显示其内容。 I get no errors, but when emulated no content is displayed from the XML file. 我没有收到任何错误,但是当进行仿真时,XML文件中不会显示任何内容。 From what I've gathered online, I'm simply calling things out of order. 从网上收集的信息来看,我只是在打乱电话。 I'm just not sure what. 我只是不确定。

MainPage.xaml.cs: 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: 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>

And finally the contents of the XML file: 最后是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>

Any advice or suggestions are, of course, greatly appreciated. 当然,任何建议都将不胜感激。 Thanks everyone for your help! 谢谢大家的帮助!

I think your problem is that the callback from DownloadStringCompleted is performed on a thread other than the UI thread. 我认为您的问题是,DownloadStringCompleted的回调是在UI线程以外的线程上执行的。 Listbox either plain ignores you or throws an exception which is swallowed by the calling thread. 列表框要么无视您,要么抛出被调用线程吞没的异常。

You need to switch to the UI thread before assigning the itemssource property. 在分配itemssource属性之前,您需要切换到UI线程。

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

The same goes for assigning to any property that is databound to a UI element 分配给与UI元素进行数据绑定的任何属性也是如此

If you figure out how this works in a different order I would be very interested to see it. 如果您弄清楚它是如何工作的,我将非常感兴趣。 I had to add a lot of overhead to get it going. 我必须增加很多开销才能使其正常运行。 The way I got it to work was my data class (your rosterviewmodel) inherited from INotifyPropertyChanged and all that implies. 我使用它的方式是从INotifyPropertyChanged继承的数据类(您的rosterviewmodel)及其所包含的所有内容。 When initializing my Data Object, I set a handler up on the propertychanged event of my data object. 初始化数据对象时,我为数据对象的propertychanged事件设置了处理程序。 Then in the handler what you would do is set the DataContext for your stackpanel to the object that was just changed. 然后,在处理程序中,您要做的是将堆栈面板的DataContext设置为刚刚更改的对象。

Have you verified the list is being populated? 您是否已确认正在填充列表? Set a breakpoint before binding to rosterList and inspect list.Count. 在绑定到rosterList并检查list.Count之前设置一个断点。

You could just load the xml like 您可以像这样加载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;

(code edited by hand to use your var names - not tested). (使用var名称手动编辑的代码-未测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM