2017-11-06 22 views
0

を更新していない私のビューモデルは、コレクション上で動作するようObservableCollection<AppToolbarButton>プロパティといくつかの方法を維持AppToolbarServiceを目的としている:のObservableCollectionプロパティが表示

public ObservableCollection<AppToolbarButton> Buttons { get; } 

    public void AddButton(AppToolbarButton button, int index = -1) 
    { 
     if (Buttons.Any(buttonVm => buttonVm.Id == button.Id)) 
     { 
      return; 
     } 

     if (index > -1) 
     { 
      Buttons.Insert(index, button); 
     } 
     else 
     { 
      Buttons.Add(button); 
     } 
    } 

私の見解モデルが表現ボディのプロパティとしてサービスのコレクションを公開し、独自のObservableCollection<AppToolbarButton>あります

01:ここで

protected IAppbarService AppbarService { get; } 

public ObservableCollection<AppbarToolbarButton> Buttons => AppbarService.Buttons; 

public MapViewModel(IAppbarService appbarservice){ 
    AppbarService = appbarService; 

    NavigateToEngineer = new Command<MapView>(OnNavigateToEngineerAsync); 

    AppbarService.AddButton(new AppToolbarButton(){ 
     Command = NavigateToEngineer 
    }); 
} 

public Command<MapView> NavigateToEngineer { get; } 

private Task OnNavigateToEngineerAsync(MapView mapView) { 
    AppbarService.RemoveAll(); 
    AppbarService.AddButton(new AppbarToolbarButton(/* the new button I want to display */)); 
    ... 
} 

は私の見解であります

<StackLayout Spacing="0" x:Name="MainAppContent"> 
     <views:ItemsStack ItemsSource="{Binding Buttons}" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Orientation="Horizontal"> 
      <views:ItemsStack.ItemTemplate> 
       <DataTemplate> 
        <Button Text="{Binding Title}" Command="{Binding Command}" CommandParameter="{x:Reference MainMapView}"/> 
       </DataTemplate> 
      </views:ItemsStack.ItemTemplate> 
     </views:ItemsStack> 
     <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> 
      <esriUI:MapView x:Name="MainMapView" Map="{Binding Map}" > 

       <esriUI:MapView.InteractionOptions> 
        <ui:MapViewInteractionOptions IsRotateEnabled="False" /> 
       </esriUI:MapView.InteractionOptions> 

       <esriUI:MapView.Behaviors> 
        <local:MapTappedBehavior Command="{Binding MapTapped}" /> 
       </esriUI:MapView.Behaviors> 
      </esriUI:MapView> 
     </Grid> 
    </StackLayout> 

OnNavigateToEngineerAsyncの後、私のボタンのスタックは更新されません。

答えて

0

UIを更新するには、PropertyChangedイベントを発生させる必要があります。 AppToolbarService.ButtonsAdd()を実行したときにPropertyChangedイベントを発行していますが、UIはAppToolbarServiceにバインドされておらず、ViewModelにバインドされています。

PropertyChangedイベントがViewModelのButtonsコレクションにバブルアップするとは思わないのですが、

、この作業を行うために、あなたは直接のViewModelのButtonsコレクションを変更する必要があるか、あなたはあなたのサービスに直接接続しないはずなので、おそらくいくつかの奇妙な/貧しいコードを必要とするあなたのViewModelのButtonsコレクションにPropertyChangedイベントをトリガする必要がありますそのようなあなたのViewModelに。

関連する問題