Windows phone 7 使用MVVM Windows Phone绑定列表框

Windows phone 7 使用MVVM Windows Phone绑定列表框,windows-phone-7,mvvm,windows-phone-8,Windows Phone 7,Mvvm,Windows Phone 8,嗨,我是新使用MVVM的,我正在尝试绑定一个列表框,但它不起作用。这是我的密码 型号 public class Musicmodel : INotifyPropertyChanged { //variables privadas private String _artista; private Uri _href; private String _informacion; private Double _Dvalue;

嗨,我是新使用MVVM的,我正在尝试绑定一个列表框,但它不起作用。这是我的密码

型号

   public class Musicmodel : INotifyPropertyChanged
   {
    //variables privadas

      private String _artista;
      private Uri _href;
      private String _informacion;
      private Double _Dvalue;

    public String artista
    {
        get
         {
            return this._artista;  
          }
        set
        {
            this._artista= value;

            this.RaisePropertyChanged("artista");
        }
    }

    public Uri href { 
        get {

            return this._href;
         }

        set
        {
            this._href = value;
            this.RaisePropertyChanged("href");
        }

    }
    public String informacion {
        get 
        {
            return this._informacion;
        }

        set
        {
            this._informacion = value;
            this.RaisePropertyChanged("informacion");
        }
    }
    public Double Dvalue
    {
        get
        {
            return this._Dvalue;
        }
        set
        {
            this._Dvalue = value;
            this.RaisePropertyChanged("Dvalue");
        }

    }



    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
   }
  }
public class DownloadFileViewModel : INotifyPropertyChanged
{

    private WebClient clienteDownload;


    private ObservableCollection<Model.Music>_musicSource= new ObservableCollection<Model.Music>();

    public ObservableCollection<Model.Music> musicSource
    {
        get
        {
            return this._musicSource;
        }
        set
        {
            this._musicSource = value;
            RaisePropertyChanged("musicSource");
        }
    }


    private int index = 0;


    //request para descargar la canción
    public void request(Model.Musicmodel item)
    {
        this.clienteDownload = new WebClient();
        this.clienteDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clienteDownload_DownloadProgressChanged);

        //agregamos el item al music
        this.musicSource.Add(item);

        this.clienteDownload.OpenReadAsync(this.musicSource[index].href);

    }


    private void clienteDownload_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
       this.musicSource[index].Dvalue=(double)e.ProgressPercentage;

    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


  }
  }
视图模型

   public class Musicmodel : INotifyPropertyChanged
   {
    //variables privadas

      private String _artista;
      private Uri _href;
      private String _informacion;
      private Double _Dvalue;

    public String artista
    {
        get
         {
            return this._artista;  
          }
        set
        {
            this._artista= value;

            this.RaisePropertyChanged("artista");
        }
    }

    public Uri href { 
        get {

            return this._href;
         }

        set
        {
            this._href = value;
            this.RaisePropertyChanged("href");
        }

    }
    public String informacion {
        get 
        {
            return this._informacion;
        }

        set
        {
            this._informacion = value;
            this.RaisePropertyChanged("informacion");
        }
    }
    public Double Dvalue
    {
        get
        {
            return this._Dvalue;
        }
        set
        {
            this._Dvalue = value;
            this.RaisePropertyChanged("Dvalue");
        }

    }



    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
   }
  }
public class DownloadFileViewModel : INotifyPropertyChanged
{

    private WebClient clienteDownload;


    private ObservableCollection<Model.Music>_musicSource= new ObservableCollection<Model.Music>();

    public ObservableCollection<Model.Music> musicSource
    {
        get
        {
            return this._musicSource;
        }
        set
        {
            this._musicSource = value;
            RaisePropertyChanged("musicSource");
        }
    }


    private int index = 0;


    //request para descargar la canción
    public void request(Model.Musicmodel item)
    {
        this.clienteDownload = new WebClient();
        this.clienteDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clienteDownload_DownloadProgressChanged);

        //agregamos el item al music
        this.musicSource.Add(item);

        this.clienteDownload.OpenReadAsync(this.musicSource[index].href);

    }


    private void clienteDownload_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
       this.musicSource[index].Dvalue=(double)e.ProgressPercentage;

    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


  }
  }
我已经调试过了,下载工作正常,我的ObservableCollection正确填充,没有任何问题,但是当我尝试绑定我的列表框时失败了。 请问我做错了什么?
谢谢

您的酒店名称中有下划线

private ObservableCollection<Model.Musicmodel> musicSource= new ObservableCollection<Model.Musicmodel>();

public ObservableCollection<Model.Musicmodel> _musicSource
{
    get
    {
        return this.musicSource;
    }
    set
    {
        this.musicSource = value;
        RaisePropertyChanged("musicSource");
    }
 }

问题很简单。您可以在中的开头初始化musicSource属性

private ObservableCollection<Model.Music>_musicSource= new ObservableCollection<Model.Music>();
这将触发视图中的另一个更新

编辑:

另一种方法是增加一个字段,如

private ObservableCollection<Model.Music>_anotherMusicSource= new ObservableCollection<Model.Music>();

然后,这将触发通知,一切都应正常运行

在运行时检查输出窗口-它应显示绑定错误,例如:
System.Windows.Data错误:40:BindingExpression路径错误:'IsCurrentLocation'属性未在'object'上找到
-检查并确保绑定到所需的对象,然后不要忘记,您的RaisePropertyChanged处理程序需要引发为
musicSource
更改的属性-不要更改该属性。如果您可以发布有用的修订代码,您是否在视图中设置了
DataContext
DataContext
是绑定的目标对象-我假设您希望将视图根处的DataContext指向ViewModel是我的视图我有这个下载文件ViewModel download=new DownloadFileViewModel();this.DataContext=下载;啊,触摸!我通常在加载viewmodel后设置属性,这样就不需要INPC调用(因为它是属性设置程序的一部分)。好地方!我通常也是这么做的。在我拥有所有需要进入属性的内容之前,我将所有内容都保存在一个私有字段中,并在最后填充它,然后正确地通知视图。谢谢我已经更改了它,但是当我向ObservableCollection添加一个项时,会抛出NullReferenceException,因为它不会初始化。我该怎么办?这是抛出这个.musicSource.Add(项目)的方法;只需确保创建一个本地范围的列表,并在填充后将其分配给
musicSource
。或者,也可以像之前那样实例化列表,但请确保在填充列表后调用
RaisePropertyChanged(“musicSource”)
(事实上,您需要确保在视图上设置
DataContext
后调用它,否则视图将看不到属性更新),您使用的是MVVM框架吗?
RaisePropertyChanged("musicSource");
private ObservableCollection<Model.Music>_anotherMusicSource= new ObservableCollection<Model.Music>();
musicSource = _anotherMusicSource;