使用MVVM Windows Phone绑定列表框

时间:2013-10-04 19:41:25

标签: 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;

    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));
        }
    }


  }
  }

查看

            <ListBox x:Name="list" ItemsSource="{Binding musicSource}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding artista}"/>
                        <ProgressBar Value="{Binding Dvalue}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

代码背后

          protected override void OnNavigatedTo(NavigationEventArgs e)
          {

            DownloadFileViewModel download = new DownloadFileViewModel();
            Model.Music newMusic = new Model.Music() { href = new   Uri("http://media1.li.ru/b/4/mp3/2/95366/953662_14Friday_Im_In_Love.mp3", UriKind.Absolute), artista = "the cure" };
            download.request(newMusic);
            this.DataContext = download;
            base.OnNavigatedTo(e);

         }

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

2 个答案:

答案 0 :(得分:1)

您的商家名称中有下划线

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

.NET倡导的标准惯例是:

// Private member variables
private int _someInteger;

// Public ones
public int SomeInteger { get { ... } set { ... } }

答案 1 :(得分:1)

问题很简单。您在

开头初始化您的musicSource属性
private ObservableCollection<Model.Music>_musicSource= new ObservableCollection<Model.Music>();

然后在请求完成后添加内容。 RaiseProperyChanged("Property")只会在您添加新的可观察集合时触发,但在您向其添加项目时则不会触发。

再次将此行添加到请求的末尾(当您填充musicSource时)

RaisePropertyChanged("musicSource");

这将触发视图中的另一个更新

编辑:

另一种方法是增加一个像

这样的字段
private ObservableCollection<Model.Music>_anotherMusicSource= new ObservableCollection<Model.Music>();

并做完所有事情,之后就说:

musicSource = _anotherMusicSource;

这将触发通知,一切都应该有效