开发者

New to MVVM Toolkit and need some help getting a simple return value to display

I am very new to Silverlight and WP7 and am writing my first app. I spent a great deal of time trying to figure out what aids to use and my choice came down to Caliburn Micro or MVVM toolkit and after seeing the video on MVVM toolkit, I chose it. But I am having a really difficult time getting it to work like was shown in the Laurent's MIX10 video. I could not find any full example of the code so I had to watch the video almost frame by frame to duplicate what Laurent did and I am only halpf way done. I have the basic code in place and it seems to be hitting my service but is not showing on my WP7 phone emulator. A side question, is the working example posted anywhere? I was hoping someone could look at my code and tell me where I am going wrong. Here it is. When I run the project, there are no errors, the emulator comes up fine but the text does not show that is being returned from the service. I have been developing .Net apps for a long time but am a noob to Silverlight and Asynchronous WCF services. Any help would be appreciated. BTW, the app is very simple, all it does is return a random bible verse from a WCF service I set up at http://www.rjmueller.com/DataAccessService/StoneFalcon.svc and displays it through a method called GetRandomBibleVerseById that takes no parameters and returns an entity called Bible. That's it, very simple. I know the answer is going to be very obvious but what I don't know, I don't know.

This is my ServiceHelper that communicates with my Service:

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);
    }

Here is my 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();
                }
            });
    }
}

Here is my Xaml page: (the field I am binding to right now is called Text, yes, I know, not the best name, I am going to change that but for now that's what it is)

<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}"
                开发者_JAVA技巧   FontSize="28" Margin="17,8,18,8" d:LayoutOverrides="Width" TextWrapping="Wrap" VerticalAlignment="Top" />

    </Grid>
</Grid>


Yes, you are binding to a property called "Text" as you point out, but I do not see such a property being exposed by your ViewModel!

Is this actually a property of the BibleVerse object? If so, your binding path should be "BibleVerse.Text"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜