2010-11-21 27 views
0

Je suis très nouveau à Silverlight et WP7 et j'écris ma première application. J'ai passé beaucoup de temps à essayer de trouver les aides à utiliser et mon choix est tombé sur Caliburn Micro ou MVVM toolkit et après avoir vu la vidéo sur MVVM toolkit, je l'ai choisi. Mais j'ai du mal à le faire fonctionner comme le montre la vidéo MIX10 de Laurent. Je n'ai pas pu trouver d'exemple complet du code, donc j'ai dû regarder la vidéo presque image par image pour reproduire ce que Laurent a fait et je ne suis que très satisfait. J'ai le code de base en place et il semble frapper mon service, mais ne s'affiche pas sur mon émulateur de téléphone WP7. Une question secondaire, est l'exemple de travail posté n'importe où? J'espérais que quelqu'un pourrait regarder mon code et me dire où je me trompe. C'est ici. Quand j'exécute le projet, il n'y a pas d'erreur, l'émulateur se présente bien mais le texte ne montre pas qu'il est retourné par le service. Je développe des applications .Net depuis longtemps, mais je suis un noob aux services Silverlight et Asynchronous WCF. Toute aide serait appréciée. BTW, l'application est très simple, tout ce qu'il fait est de retourner un verset bible aléatoire d'un service WCF que j'ai mis en place à http://www.rjmueller.com/DataAccessService/StoneFalcon.svc et l'affiche par une méthode appelée GetRandomBibleVerseById qui ne prend aucun paramètre et renvoie une entité appelée Bible. C'est tout, très simple. Je sais que la réponse va être très évidente mais ce que je ne sais pas, je ne sais pas.Nouveau sur MVVM Toolkit et besoin d'aide pour obtenir une simple valeur de retour à afficher

Ceci est mon ServiceHelper qui communique avec mon 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); 
    } 

Voici mon 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(); 
       } 
      }); 
    } 
} 

Voici ma page XAML: (le terrain, je suis RELIURE à l'heure actuelle est appelé Texte, oui, je sais, pas le meilleur nom, je vais changer cela, mais pour l'instant c'est ce que c'est)

<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> 

Répondre

0

Oui, vous liez à une propriété appelée « Texte » comme vous le dites, mais je ne vois pas une telle propriété exposée par votre ViewModel!

Est-ce réellement une propriété de l'objet BibleVerse? Si oui, votre chemin de liaison doit être "BibleVerse.Text"