【问题标题】:Modifying Collection In ICommand Delegate Action Not Updating UI在 ICommand 委托操作中修改集合未更新 UI
【发布时间】:2014-12-09 18:42:56
【问题描述】:

我的应用程序当前发出 REST 请求,反序列化一些 JSON 并将项目添加到 ObservableCollection 异步(减去在 UI 线程上添加项目)。将 ICommand 委托绑定到按钮时,尝试修改操作中的集合不会反映在 UI 上,但执行“ad hoc”委托 Execute() 会。

我的 ProjectViewModel (DataContext)

public class ProjectViewModel : INotifyPropertyChanged
{
    public DelegateCommand AddProject { get; set; }
    public DelegateCommand OnLoad { get; set; }

    private ObservableCollection<Project> m_Projects;
    public ObservableCollection<Project> Projects
    {
        get { return m_Projects; }
        set
        {
            m_Projects = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Projects"));
        }
    }

    private CoreDispatcher MainDispatcher = null;
    public event PropertyChangedEventHandler PropertyChanged;

    public ProjectViewModel()
    {
        MainDispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher;
        Projects = new ObservableCollection<Project>();

        OnLoad = new DelegateCommand(
            async (p) => 
            { 
                await GetProjectsAsync();

                //Test = Works!
                Projects.Insert(0, new Project() { Name = "Steve" });
            },
            () => { return true; }
        );

        OnLoad.Execute(null);


        AddProject = new DelegateCommand(
            (p) =>
            {
                Projects.Insert(0 , new Project() { Name = "Test", Key = "Test", Avatar = null });
            },
            () => { return true; }
        );

        //Test = Works!
        AddProject.Execute(null);
    }

Xaml 按钮: &lt;Button Margin="232,0,0,583" Content="Search" Background="White" Foreground="Black" Width="158" Command="{Binding AddProject}"/&gt;

网格视图:

        <GridView x:Name="ProjectListView" Margin="0,107,0,0" VerticalAlignment="Top" Width="400" ItemsSource="{Binding Projects, Mode=TwoWay}" ItemTemplate="{StaticResource ProjectTemplate}" Height="533">
        <GridView.DataContext>
            <vm:ProjectViewModel/>
        </GridView.DataContext>
    </GridView>

委托按预期执行,我可以看到使用 Debug.WriteLine 添加到集合中的项目,但是,当从 UI 执行委托时,它们只是不显示在 GridView 上。另外值得注意的是,确实会调用 CollectionChanged 事件。

谁能给我指出正确的方向,谢谢!!

【问题讨论】:

  • 1) 当您改用Add 时,它是否有效? 2)GetProjectsAsync 是做什么的? 3) 如果你在 UI 线程中 ceate Projects observable 会发生什么? 4) 如果从 AddProject 中删除参数会发生什么? 5) 另外,您正在设置 GridView DataCotext,这表明 Button 必须位于 GridView ItemPanelTemplate 内?否则它将如何访问它?你在复制视图模型吗?
  • 1)。不,任何方法(添加、删除、插入等)都不起作用。 2)。 GetProjectsAsync 是一个异步任务,它反序列化每个 JSON 对象,将其添加到“项目”集合中。每个项目都使用 MainDispatcher.RunAsync(priority, action) 添加,因此项目被添加到 UI/前台线程。 3)。完全相同的结果,仍然无法添加/删除等。 4)。默认情况下它为 null 并且是 CommandParameters 参数。我会尝试删除它。 5)。 DataContext 在页面上设置,因此每个孩子都“将”继承它。我通过 Button.DataContext 添加了一个数据上下文谢谢你的评论@ChrisEelmaa

标签: c# mvvm


【解决方案1】:

看来这个问题是因为 DataContext 是在 GridView.DataContext 和 Page.Resources 中的 Page 设置的。肯定有冲突。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    相关资源
    最近更新 更多