【问题标题】:How can I page refresh without Navigation from my MvvM Light ViewModel如何在没有导航的情况下从我的 MvvM Light ViewModel 刷新页面
【发布时间】:2012-02-22 15:45:15
【问题描述】:

在显示初始视图后,如何在没有页面导航的情况下更新我的视图模型 (Mvvm Light) 中的嵌套列表框。目前我正在使用不断变化的查询字符串进行可重入页面导航 - 一定有更好的方法吗?

RaisePropertyChanged 没有任何效果,尽管当通过 OpenCallingPoints 触发的肥皂请求的回调已触发时,我可以看到数据填充了正确的数据。

我试图用肥皂数据填充的网格是 CallingPointsGrid

短版代码...

<ListBox x:Name="ResultsListBox" Margin="0" VerticalAlignment="Top" ItemsSource="{Binding JourneyLegs, Mode=TwoWay}" Background="{StaticResource BackgroundWhiteGradientBrush}" >
 <ListBox.ItemTemplate>               
      <DataTemplate>
          <StackPanel x:Name="StationItem" Orientation="Vertical" VerticalAlignment="Top" background="{Binding id, Converter={StaticResource myconverter}}">

               <Grid Name="CallingPointsGrid" Margin="15,10,55,10"  Visibility="{Binding JourneyCallingPoints, Converter={StaticResource CallingPointsVisibilityConverter}}" Background="{StaticResource BackgroundWhiteGradientBrush}">
                   <ListBox Grid.Row="1" Name="CallingPointsListBox" DataContext="{Binding}" VerticalAlignment="Top" ItemsSource="{Binding JourneyCallingPoints, Mode=TwoWay}">
                         <ListBox.ItemTemplate>
                               <DataTemplate>
                                    <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                           <TextBlock Margin="0" VerticalAlignment="Center" HorizontalAlignment="Left" Width="210" x:Name="Destination" Foreground="Black" Text="{Binding stationName}" />
                                           <TextBlock Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Left" Width="75" x:Name="ScheduledDepartureTime"  FontWeight="Bold" Foreground="{StaticResource BackgroundBlueLightSolidColor}" Text="{Binding timetable.scheduledTimes.arrival, StringFormat=\{0:HH:mm\}}" />
                                           <TextBlock Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Left" Width="75" x:Name="ScheduledArrivalTime"  FontWeight="Bold" Foreground="{StaticResource BackgroundBlueLightSolidColor}" Text="{Binding timetable.scheduledTimes.departure, StringFormat=\{0:HH:mm\}}" />
                                    </StackPanel>
                               </DataTemplate>
                          </ListBox.ItemTemplate>
                    </ListBox>

                   </Grid>
         </StackPanel>

        </DataTemplate>                       
 </ListBox.ItemTemplate>
 <i:Interaction.Triggers>
 <i:EventTrigger  SourceName="ResultsListBox" EventName="Tap">                            
  <i:EventTrigger.Actions>
        <local:OpenCallingPoints />                              
 </i:EventTrigger.Actions>                                                     
       </i:EventTrigger>                                          
 </i:Interaction.Triggers>

【问题讨论】:

  • 你使用ObservableCollection作为你的物品来源吗?
  • 搭载 Ku6opr:您应该为列表使用 ObservableCollection,并确保您的底层对象(在列表中)实现 INotifyPropertyChanged。
  • 嗨,据我了解,ListBox 在 WP7 上使用 ObservableCollection,但以防万一我弄错了。它没有任何影响,但在阴霾中我准备再试一次
  • 您好,JourneyCallingPoints 只是一个普通的 get,在我的模型中定义的集合,在初始视图显示后填充(以支持多个 Soap 调用)。 JourneyLegs 在我的 ViewModel 中有一个 RaisePropertyChanged(实现 INotifyPropertyChanged 的​​ Mvvm Light)。 JourneyLegs 是 List 类型,JourneyLeg 类包含 JourneyallingPoints 类型,而这又是 List。你是说我必须在基础类中的任何相关属性上定义一个 NotifyPropertyChange,尽管这些类型不是在 ViewModel 中定义而是在模型中定义???
  • 项目源上是 ObservableCollection

标签: silverlight windows-phone-7 silverlight-4.0 mvvm-light


【解决方案1】:

你可能会看到这个: http://matthamilton.net/madprops-mvvmlight-screenconductor

我打算建议你看看 Caliburn Micro 并四处寻找 Mvvm-Light 是否有类似的东西。那篇文章谈到了这一点。

这是我非常喜欢 CM 的一件事,当您从 Screen 继承 ViewModel 时,您会自动获得可以覆盖的 OnInitialize、OnActivate、OnDeactive、OnViewLoaded 和 OnViewReady 方法。所以在这种情况下,你会在 OnActivate 中坚持这个逻辑。

在 windows phone CM 也会自动解析出查询字符串参数并用它们填充你的 VM 上的属性。

看起来屏幕导体允许你用 mvvm-light 做类似的事情。

【讨论】:

  • 嗨,Dereck,我不确定我是否明白这一点。您是说当我执行 RaisePropertyChanged 时 On 方法会触发。我究竟会在那里做什么 - 使用我的 soap 调用结果更新绑定变量以保证没有导航的屏幕刷新??
  • 不,on 方法是页面生命周期事件。 OnInitialize 仅在页面第一次导航到时触发。 OnActivate 在页面也被激活或导航时触发。什么
猜你喜欢
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 2017-12-21
相关资源
最近更新 更多