繁体   English   中英

通过视图模型表达式强健的成员公开的对象的ObservableCollection属性不会更新视图

[英]ObservableCollection property of an object exposed through view model expression-bodied member does not update view

我的视图模型具有一个AppToolbarService对象,该对象维护一个ObservableCollection<AppToolbarButton>属性以及用于对该集合进行操作的几种方法:

    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> ,将服务的集合公开为表达式ObservableCollection<AppToolbarButton>的属性:

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之后,我的按钮堆栈没有更新。

为了更新UI,必须触发PropertyChanged事件。 当您执行Add()时,您的AppToolbarService.Buttons可能会发出PropertyChanged事件,但您的UI并未绑定到AppToolbarService ,而是已绑定到ViewModel。

我认为AppToolbarService.ButtonsPropertyChanged事件不会冒泡到ViewModel的Buttons集合。

为了使这项工作,您可能需要修改视图模型的Buttons直接收集,或者你需要触发PropertyChanged你的视图模型的事件Buttons集合这可能需要一些怪异/可怜的代码,因为你的服务不应该直接连接到您的视图模型像那。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM