2

Web サービス アプリケーションからデータを非同期に取得する Windows Phone 7 (wp8 SDK および VS Ultimate 2012 を使用する 7.1) アプリケーションで MVVM Light を使用しています。各ページで RelayCommands を使用して非同期メソッドを実行し、データを取得してから次のページに移動します。たとえば、ViewModel の 1 つで、次の ICommand を宣言します。

public ICommand ShowTreatmentDetailsCommand { get; set; }

次に、VM のコンストラクターで、次のように割り当てます。

ShowTreatmentDetailsCommand = new RelayCommand(ShowTreatmentDetails);

そして、そのコマンドによって呼び出されるメソッドは次のとおりです。

private async void ShowTreatmentDetails()
{
    try
    {
        Treatment refreshedTreatment = await treatmentService.LoadSingle(SelectedTreatment.id, LoggedUser.logon, LoggedUser.pwHash);

        if (refreshedTreatment != null)
        {
            DrugGroup anaestGroup = null;
            DrugGroup surgGroup = null;

            IEnumerable<DrugGroup> groups = await drugGroupService.Load(
                refreshedTreatment.id,
                LoggedUser.logon,
                LoggedUser.pwHash);

            anaestGroup = groups
                .Where(g => g.type == DrugType.Anaesthetic)
                .SingleOrDefault<DrugGroup>();

            surgGroup = groups
                .Where(g => g.type == DrugType.Surgical)
                .SingleOrDefault<DrugGroup>();

            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add(Keys.AnaestDrugGroup, anaestGroup);
            parameters.Add(Keys.SurgDrugGroup, surgGroup);
            parameters.Add(Keys.SelectedTreatment, refreshedTreatment);

            Messenger.Default.Send(parameters);
        }
        else
        {
            // Display error message
        }

        RefreshData();
    }
    catch (NullReferenceException) { }
}

このコマンドは、EventTrigger と EventToCommand クラスを使用して、ListBox からアイテムが選択されたときにビューの xaml コードから呼び出されます (ただし、問題はボタン関連のコマンドでも同じです。念のため、ここに私の ListBox 要素を示します。

<ListBox x:Name="lbxTreatmentList"
         ItemsSource="{Binding Treatments}"
         SelectedItem="{Binding SelectedTreatment, Mode=TwoWay}">
    <int:Interaction.Triggers>
        <int:EventTrigger EventName="SelectionChanged">
            <com:EventToCommand Command="{Binding ShowTreatmentDetailsCommand}"
                                PassEventArgsToCommand="True" />
        </int:EventTrigger>
    </int:Interaction.Triggers>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <custom:TreatmentListItem PatientName="{Binding patient}"
                                      OpeDescription="{Binding description}"
                                      StartedAt="{Binding startedAt}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ボタンを使用した別の例:

<Button Grid.Row="2"
    HorizontalAlignment="Center"
    Foreground="{StaticResource BeldicoBlue}"
    BorderBrush="{StaticResource BeldicoBlue}"
    Margin="0,12,0,0"
    Padding="24,12"
    Command="{Binding ValidateCommand}">
    <TextBlock Text="Validate this treatment" FontWeight="ExtraBold" />
</Button>

問題は、コマンドが起動されるたびに、関連するメソッドが実行される回数が増えることです。つまり、最初の呼び出しで 1 回、次に 2 回、次に 3 回、4 回、5 回...。メソッドに非同期サービス呼び出しがあるため、これはすぐに帯域幅と時間がかかります。

この動作の背後にある理由は絶対にわかりません。誰か助けてもらえますか?

4

1 に答える 1