MVVM Toolkit的新手,需要一些帮助才能显示简单的返回值

时间:2010-11-21 13:01:25

标签: silverlight mvvm mvvm-light mvvm-toolkit

我是Silverlight和WP7的新手,正在编写我的第一个应用程序。我花了很多时间试图弄清楚使用什么辅助工具,我的选择归结为Caliburn Micro或MVVM工具包,在看了MVVM工具包上的视频后,我选择了它。但是我很难让它像Laurent的MIX10视频中所展示的那样工作。我找不到任何代码的完整示例,所以我不得不逐帧地观看视频以复制Laurent所做的事情,而且我只是完成了halpf。我有基本的代码,它似乎是我的服务,但没有在我的WP7手机模拟器上显示。一个附带问题,是在任何地方发布的工作示例吗?我希望有人可以查看我的代码并告诉我哪里出错了。这里是。当我运行项目时,没有错误,模拟器出现正常,但文本没有显示从服务返回。我一直在开发.Net应用程序很长一段时间,但它是Silverlight和异步WCF服务的菜鸟。任何帮助,将不胜感激。顺便说一下,应用程序非常简单,它只是从我在http://www.rjmueller.com/DataAccessService/StoneFalcon.svc设置的WCF服务返回一个随机圣经经文,并通过一个名为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>

1 个答案:

答案 0 :(得分:0)

是的,正如您所指出的那样,您绑定了一个名为“Text”的属性,但我没有看到ViewModel公开这样的属性!

这实际上是BibleVerse对象的属性吗?如果是这样,您的绑定路径应为“BibleVerse.Text”