0

我第一次在我的应用程序中使用了 MVVM 模式。我创建了一个视图类、一个 viewModel 类和一个 Command 类。当我在 Button 的视图中单击时,我会在我的 viewModel 中启动绑定命令。在此命令中,我创建了一个 HttpWebRequest,以将来自 Web 服务的新闻作为 json 对象加载。在“GetNewsResponseCallback”方法中,我将数据作为 json 流获取。序列化工作正常。当我在这个方法中调用'PropertyChanged(this, new PropertyChangedEventArgs("News"))'时,我得到一个 COMException。但我需要 PropertyChange 事件来更新我视图中的 Listview。我该如何解决这个问题?从我的命令启动服务是否正确?我是使用 MVVM 模式开发的新手。在下面我的代码中:

<Canvas>
    <Button Content="Aktualisieren"
            Canvas.Top="20"
            Canvas.Left="600"
            Command="{Binding SynchronizeNewsCommand}"/>

    <ListView x:Name="viewBox"
              ItemsSource="{Binding News}"
              ItemTemplate="{StaticResource newsTemplate}"/>

</Canvas>
public class MyCommand<T> : ICommand
    {
        readonly Action<T> callback;


        public MyCommand(Action<T> callback)
        {
            this.callback = callback;
        }


        public bool CanExecute(object parameter)
        {
            return true;
        }


        public event EventHandler CanExecuteChanged;


        public void Execute(object parameter)
        {
            if (callback != null) { callback((T)parameter); }
        }

    }



public class NewsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<NewsSerializer> News { get; set; }

        public MyCommand<object> SynchronizeNewsCommand { get; set; }


        public NewsViewModel()
        {
            SynchronizeNewsCommand = new MyCommand<object>(SynchronizeNews);
        }


        private void SynchronizeNews(object obj)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constans.uri);
            request.ContentType = "application/json; charset=utf-8";
            request.Accept = "application/json";
            request.Method = "POST";
            request.BeginGetRequestStream(new AsyncCallback(GetNewsRequestStreamCallback), request);
        }


        private void SynchronizeFinished(object obj)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("News"));
        }


        private void GetNewsRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            request.BeginGetResponse(new AsyncCallback(GetNewsResponseCallback), request);
        }


        private void GetNewsResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ObservableCollection<NewsSerializer>));
            News = (ObservableCollection<NewsSerializer>)ser.ReadObject(streamResponse);

            PropertyChanged(this, new PropertyChangedEventArgs("News"));
        }

    }
4

1 回答 1

1

通常,在使用 ObservableCollection 时 - 您不会每次都重新创建它。您应该改为调用 Clear() (或删除已删除的项目),然后将新项目一一添加到其中。您的视图将自动收到更改通知,您无需调用 PropertyChanged

    private void GetNewsResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();

        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ObservableCollection<NewsSerializer>));
        var res = (IList<NewsSerializer>)ser.ReadObject(streamResponse);

        News.Clear();
        foreach(var item in res)
        {
            News.Add(item);
        }
    }

在您的情况下,您还应该考虑通过实现 AddRange 方法来扩展 ObservableCollection 类。

于 2013-03-07T11:51:28.740 回答