我对 Silverlight 和 WP7 非常陌生,正在编写我的第一个应用程序。我花了很多时间试图弄清楚要使用哪些辅助工具,我的选择归结为 Caliburn Micro 或 MVVM 工具包,在看到 MVVM 工具包上的视频后,我选择了它。但是我很难让它像 Laurent 的 MIX10 视频中显示的那样工作。我找不到任何完整的代码示例,所以我不得不几乎逐帧观看视频以复制 Laurent 所做的事情,而我只完成了一半。我有基本的代码,它似乎在打我的服务,但没有显示在我的 WP7 手机模拟器上。一个附带问题,工作示例是否发布在任何地方?我希望有人可以查看我的代码并告诉我哪里出错了。这里是。当我运行项目时,没有错误,模拟器运行良好,但文本未显示从服务返回的内容。我一直在开发 .Net 应用程序,但我对 Silverlight 和异步 WCF 服务很陌生。任何帮助,将不胜感激。顺便说一句,该应用程序非常简单,它所做的只是从我设置的 WCF 服务返回随机圣经经文http://www.rjmueller.com/DataAccessService/StoneFalcon.svc并通过一个名为 GetRandomBibleVerseById 的方法显示它,该方法不接受任何参数并返回一个名为 Bible 的实体。就是这样,非常简单。我知道答案会很明显,但我不知道,我不知道。
这是与我的服务通信的 ServiceHelper:
public class ServiceHelper
{
public void GetRandomBibleVerseById(Action<Bible, Exception> callback)
{
var client = new StoneFalconClient();
client.GetRandomBibleVerseByIdCompleted += (s, e) =>
{
var userCallback = e.UserState as Action<Bible, Exception>;
if (userCallback == null)
{
return;
}
if (e.Error != null)
{
userCallback(null, e.Error);
return;
}
};
client.GetRandomBibleVerseByIdAsync(callback);
}
这是我的 MainViewModel:
public class MainViewModel : INotifyPropertyChanged
{
/// <summary>
/// The <see cref="BibleVerse" /> property's name.
/// </summary>
public const string BibleVersePropertyName = "BibleVerse";
private Bible _bibleVerse;
public Bible BibleVerse
{
get
{
return _bibleVerse;
}
set
{
if (_bibleVerse == value)
{
return;
}
_bibleVerse = value;
RaisePropertyChanged(BibleVersePropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string ApplicationTitle
{
get
{
return "RJ's Bible Searcher";
}
}
public string PageName
{
get
{
return "Verse of the Day";
}
}
public MainViewModel()
{
ServiceHelper helper = new ServiceHelper();
helper.GetRandomBibleVerseById((bibleVerse, error) =>
{
if (error != null)
{
//show error
}
else
{
BibleVerse = new Bible();
}
});
}
}
这是我的 Xaml 页面:(我现在绑定的字段称为 Text,是的,我知道,这不是最好的名称,我要更改它,但现在就是这样)
<phone:PhoneApplicationPage x:Class="BibleSearcher.wp7.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:BibleSearcher.wp7.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"
Orientation="Portrait"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="768"
shell:SystemTray.IsVisible="True"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<UserControl.Resources>
<!--not the best way to do this,
does not allow the constructor to take paramaters, uses default constructor
when the xaml reaches this point, the viewmodel is created-->
<vm:MainViewModel x:Key="MainViewModel" />
</UserControl.Resources>
<!--LayoutRoot contains the root grid where all other 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="24,24,0,12">
<TextBlock x:Name="ApplicationTitle"
Text="RJ's Bible Searcher"
Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock x:Name="PageTitle"
Text="Verse of the Day"
Margin="-3,-8,0,0"
Style="{StaticResource PhoneTextTitle1Style}" FontSize="48" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentGrid"
Grid.Row="1"
DataContext="{Binding Source={StaticResource MainViewModel}}" >
<TextBlock Text="{Binding Path=Text}"
Style="{StaticResource PhoneTextNormalStyle}"
FontSize="28" Margin="17,8,18,8" d:LayoutOverrides="Width" TextWrapping="Wrap" VerticalAlignment="Top" />
</Grid>
</Grid>