更新:
我认为你是对的,问题可能出在我打电话给 GetRoster 时。这是我称之为的上下文:
'
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"));
}
'
调用它的正确位置是什么?顺便说一句,我很抱歉代码部分没有正确显示。
大家好!谢谢您的帮助!我之前没有创建帐户,所以这可能会显示为答案,因为我在另一台计算机上。我尽我最大的努力将 Twitter 教程改成我想要做的事情。我没有收到任何错误,但它没有在模拟器中显示任何内容。我创建了一个 XML 文件并将其上传到我的个人网站。不幸的是,我无法让代码示例按钮远程工作。所以我很抱歉这看起来很糟糕。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>
这是 MainPage.xaml.cs:
private void GetRoster()
{
WebClient rstr = new WebClient();
rstr.DownloadStringCompleted += new DownloadStringCompletedEventHandler(roster_DownloadStringCompleted);
rstr.DownloadStringAsync(new Uri("http://www.MyPersonalWebsiteURL.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:
'
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox HorizontalAlignment="Left" Name="rosterList" ItemsSource="rosterList" VerticalAlignment="Top" Width="476">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<StackPanel Width="370">
<TextBlock Text="{Binding Name}" Foreground="#FFC8AB14" FontSize="28" />
<TextBlock Text="{Binding Age}" TextWrapping="Wrap" FontSize="24" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
'
然而!当我在模拟器中运行应用程序时,我没有收到任何错误,但没有显示任何内容。我知道解决方案可能非常简单,所以我想重申一下您的帮助对我的意义。非常感谢您提供的任何建议。