アップデート:
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 ファイルを作成し、個人の Web サイトにアップロードしました。残念ながら、コード サンプル ボタンをリモートでもうまく機能させることができません。なので、見栄えが悪くてすみません。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>
'
でも!エミュレーターでアプリを実行すると、エラーは発生しませんが、コンテンツはまったく表示されません。解決策はおそらく非常に簡単であることを知っているので、あなたの助けが私にとってどれほど意味があるかを繰り返したいと思います. アドバイスをいただければ幸いです。