0

私は Silverlight と WP7 を初めて使用し、最初のアプリを作成しています。どのエイドを使用するかを考えるのにかなりの時間を費やしましたが、私の選択は Caliburn Micro または MVVM ツールキットに行き着き、MVVM ツールキットのビデオを見て、それを選びました。しかし、Laurent の MIX10 ビデオで示されているように機能させるのは非常に困難です。コードの完全な例を見つけることができなかったので、ビデオをほぼフレームごとに見て、ローランが行ったことを複製する必要がありましたが、半分しかできていません。基本的なコードが配置されており、サービスにヒットしているように見えますが、WP7 電話エミュレーターには表示されません。副次的な質問ですが、実例はどこかに投稿されていますか? 誰かが私のコードを見て、どこが間違っているのか教えてくれることを望んでいました。ここにあります。プロジェクトを実行すると、エラーは発生しません。エミュレーターは正常に起動しますが、サービスから返されたテキストは表示されません。私は長い間 .Net アプリを開発してきましたが、Silverlight と非同期 WCF サービスには慣れていません。どんな助けでも大歓迎です。ところで、このアプリは非常にシンプルで、私がセットアップした WCF サービスからランダムな聖書の一節を返すだけです。http://www.rjmueller.com/DataAccessService/StoneFalcon.svcを取得し、パラメータを取らず、Bible というエンティティを返す GetRandomBibleVerseById というメソッドを介して表示します。それだけです、非常に簡単です。答えは非常に明白であることはわかっていますが、わからないことはわかりません。

これは、私の Service と通信する 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>

4

1 に答える 1

0

はい、ご指摘のとおり「Text」というプロパティにバインドしていますが、そのようなプロパティがViewModelによって公開されていることはわかりません。

これは実際にはBibleVerseオブジェクトのプロパティですか?その場合、バインディングパスは「BibleVerse.Text」である必要があります

于 2010-11-21T13:29:54.710 に答える