Silverlight MVVM工具包新手,需要一些帮助才能获得一个简单的返回值来显示
我对Silverlight和WP7非常陌生,正在编写我的第一个应用程序。我花了很多时间试图弄清楚要使用什么辅助工具,我的选择是Caliburn Micro或MVVM toolkit,在看过MVVM toolkit上的视频后,我选择了它。但我真的很难让它像Laurent的MIX10视频中那样工作。我找不到任何完整的代码示例,所以我不得不几乎一帧一帧地观看视频,以复制Laurent所做的,而我只是以halpf的方式完成的。我已经准备好了基本代码,它似乎正在影响我的服务,但没有显示在我的WP7手机模拟器上。一个附带的问题是,工作示例是否张贴在任何地方?我希望有人能看看我的代码,告诉我哪里出了问题。给你。当我运行项目时,没有错误,模拟器运行正常,但是文本没有显示服务返回的内容。我已经开发.Net应用程序很长时间了,但我是Silverlight和异步WCF服务的noob。任何帮助都将不胜感激。顺便说一句,该应用程序非常简单,它所做的只是从我设置的WCF服务返回一个随机圣经经文,并通过一个名为GetRandomBibleVerseByd的方法显示它,该方法不带任何参数,并返回一个名为bible的实体。就这样,很简单。我知道答案很明显,但我不知道,我不知道 这是我的ServiceHelper,它与我的服务进行通信: 这是我的主视图模型: 这是我的Xaml页面:我现在绑定到的字段名为Text,是的,我知道,这不是最好的名称,我将改变它,但现在它是什么Silverlight MVVM工具包新手,需要一些帮助才能获得一个简单的返回值来显示,silverlight,mvvm,mvvm-light,mvvm-toolkit,Silverlight,Mvvm,Mvvm Light,Mvvm Toolkit,我对Silverlight和WP7非常陌生,正在编写我的第一个应用程序。我花了很多时间试图弄清楚要使用什么辅助工具,我的选择是Caliburn Micro或MVVM toolkit,在看过MVVM toolkit上的视频后,我选择了它。但我真的很难让它像Laurent的MIX10视频中那样工作。我找不到任何完整的代码示例,所以我不得不几乎一帧一帧地观看视频,以复制Laurent所做的,而我只是以halpf的方式完成的。我已经准备好了基本代码,它似乎正在影响我的服务,但没有显示在我的WP7手机模拟
是的,正如您所指出的,您正在绑定到一个名为Text的属性,但我没有看到ViewModel公开这样的属性 这实际上是BibleVerse对象的属性吗?如果是这样,那么绑定路径应该是BibleVerse.Text
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);
}
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();
}
});
}
}
<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>