我正在学习制作一个关于Pokemon信息的基本应用程序,以此来学习Windows Phone开发。为此,我创建了一个可移植类库(PokeLib.dll),使其与通用应用兼容。我通过同一解决方案中的一个项目(“Test”)进行了测试,一切正常。你可以在我的Github上查看这些代码,但据我所知,它们都很好。这两个项目在同一个解决方案中。对于Windows Phone应用程序的解决方案,我将PokeLib添加为“现有项目”,添加了引用,并编写了几行代码以确保可以成功调用它:
MainPage.xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="GetDataButton" Content="GetData" Click="GetDataButton_Click" Grid.Row="0" HorizontalAlignment="Center"/>
<TextBlock Name="DataText" Text="Click to get data" Grid.Row="1" Padding="10"/>
</Grid>
MainPage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
p = new Pokemon(1); // gets data for Pokemon #1 (Bulbasaur)
}
Pokemon p;
int counter = 0;
private async void GetDataButton_Click(object sender, RoutedEventArgs e)
{
DataText.Text = "Fetching... Count: " + ++counter;
if (counter == 1) // first time button's clicked
{
await p.Create(); // populates the data container
DataText.Text = String.Format("Pokemon #{0}: {1}", p.Id, p.Name);
}
}
当我尝试在手机模拟器上运行此项目时,出现以下消息:。我以 "debug" 模式构建项目,并取消了 "启用我的代码" 选项。我不确定在 Symbols 面板下该怎么做,但如果有用的话,我也可以添加屏幕截图。
无论如何,应用程序会打开,在我按下 GetData 按钮时会冻结。我预计由于该调用是同步完成的,它会冻结一会儿,但这是永久性的。然而,没有错误/异常被抛出。当我尝试步入 p.Create() 调用时,调试器也没有响应(可能源于屏幕截图中的消息)。
有人知道我做错了什么吗?谢谢!
p.Create
而不是调用Wait
方法。在 UI 线程上等待本来就是不好的做法。 - Kevin Gosse