【问题标题】:How to refresh DataContext of PivotItem after page navigation页面导航后如何刷新 PivotItem 的 DataContext
【发布时间】:2013-03-02 00:35:37
【问题描述】:

在 WP 7.1 项目中,我有一个 MainPage.xaml。在那里,我有一个来自 WP Toolkit 的 Pivot 控件,如下所示:

<c:Pivot x:Name="pivot" Title="title">
    <c:PivotItem Header="header" DataContext="{vm:ListViewModel}">
        <v:ListView />
    </c:PivotItem>
    ...
</c:Pivot>

基本上,我将第一个数据透视项的内容设置为我创建的一个名为 ListView 的 UserControl,并为它创建了一个名为 ListViewModel 的视图模型。

使用这样的简单模型类:

public class MyModel {
    public int Id { get; set; }
    public string Name { get; set; }
}

ListView 视图的 xaml 如下所示:

...
<ListBox x:Name="list" ItemsSource="{Binding ModelItems"}>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
                <Button Command="{Binding DataContext.EditCommand, ElementName=list}"
                        CommandParameter="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}">
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
...

列表中的每个项目只需一个简单的 2 行条目。名称显示在第一行,该按钮用于我们要编辑该项目时。该视图的视图模型是:

...
private ObservableCollection<MyModel> modelItems;
public ObservableCollection<MyModel> ModelItems {
    get { return modelItems; }
    set {
        if (modelItems != value) {
            modelItems = value;
            NotifyPropertyChanged(() => this.ModelItems);
        }
    }
}

public ListViewModel() {
    ModelItems = new ObservableCollection<MyModel>(repository.GetAll());
}

public ICommand EditCommand {
    get {
      return new RelayCommand(o => {
          var model = o as MyModel;
          Navigator.Uri<EditView>()
                   .WithParam("id", model.id)
                   .Navigate();
    }
}
...

到目前为止,一切正常,列表已正确填充。点击按钮正确导航到 EditView 视图,传递所选模型项的 id。

EditView 页面很简单,只有一个文本框和一个保存按钮。其视图模型类似于:

...
public EditViewModel() { // ctor
    var id = GetQueryString("id");
    Model = repository.Get(id);
}
...

编辑页面运行良好,它将更改保存回 db。但是,在保存操作之后,我执行了Navigator.GoBack() 并在那里显示旧数据。我应该如何强制我的列表更新新的更改,同时避免代码隐藏?

我能够使用 MainPage 代码隐藏中的枢轴控件 Loaded 事件来实现这一点,如下所示:

private void pivot_Loaded(object sender, RoutedEventArgs e) {
    var selectedPivotItem = (PivotItem)pivot.SelectedItem;
    ((ListViewModel)selectedPivotItem.DataContext).Rebind();
}

我在我的 ListViewModel 中添加了一个 Rebind() 方法,该方法与构造函数的作用基本相同,再次获取所有数据并将其分配给 ModelItems

有没有办法不用写代码就能做到这一点?

谢谢

【问题讨论】:

    标签: windows-phone-7 mvvm windows-phone


    【解决方案1】:

    我习惯于使用单例模式创建一个名为AppLifeService 的类。在这个对象中,我存储了用于页面间导航的各种属性。

    当您在执行操作(编辑/删除/创建)的页面中时,只需将修改后的对象放在AppLifeService 中的某个属性中,例如DeletedComment

    然后在上一页中,在OnNavigatedTo 方法中,验证它是一个导航返回并搜索您的对象。

        protected override void OnNavigatedTo(NavigationEventArgs e, NavigationContext context, NavigationService navigationService)
        {
            base.OnNavigatedTo(e, context, navigationService);
    
            // back from other app page
            if (e.NavigationMode == NavigationMode.Back)
            {
                var lifeService = ApplicationServices.Resolve<AppLifeService>();
    

    不是吗? var deletedComment = lifeService.DeletedComment; if (deletedComment != null) { // 删除评论 }

    确保不要存放重物,以免堵塞内存。并在使用时将其删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2013-02-12
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多