2010-11-21 8 views
0

我對Silverlight和WP7非常陌生,正在編寫我的第一個應用程序。我花了大量的時間試圖找出使用哪些輔助工具,並將我的選擇歸結爲Caliburn Micro或MVVM工具包,並且在MVVM工具包上看到視頻後,我選擇了它。但是我很難讓它像Laurent的MIX10視頻中顯示的那樣工作。我找不到任何完整的代碼示例,因此我不得不逐幀觀看視頻,以複製Laurent所做的事情,而我只是採取了一些辦法。我有基本的代碼,它似乎打我的服務,但沒有顯示在我的WP7手機模擬器。一個側面問題,是在任何地方發佈的工作示例?我希望有人可以看看我的代碼,並告訴我哪裏出錯了。這裏是。當我運行該項目時,沒有錯誤,模擬器顯示正常,但文本未顯示正在從服務返回。我一直在開發.Net應用程序很長一段時間,但對Silverlight和異步WCF服務不太熟悉。任何幫助,將不勝感激。順便說一句,該應用程序非常簡單,它所做的只是從我在http://www.rjmueller.com/DataAccessService/StoneFalcon.svc設置的WCF服務中返回一個隨機聖經詩句,並通過名爲GetRandomBibleVerseById的方法顯示它,該方法不接受參數並返回一個名爲「聖經」的實體。就是這樣,非常簡單。我知道答案會非常明顯,但我不知道,我不知道。MVVM Toolkit的新手,需要一些幫助才能獲得簡單的返回值來顯示

這是我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頁面:(我綁定領域到現在爲止稱爲文本,是的,我知道,不是最好的名字,我會改變,但現在這就是它)

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

回答

0

是的,你是綁定到一個名爲「文本」正如你所指出的財產,但我沒有看到你的視圖模型暴露這樣的屬性!

這實際上是BibleVerse對象的屬性嗎?如果是這樣,你的綁定路徑應該是「BibleVerse.Text」