1

MvxBindableTableViewCell. 我在 iOS 上使用 MvvmCross 的 vNext 分支。

一切都已正确设置,并且在首次加载/显示列表时可以看到初始值。该列表是 aObservableCollection<T>并且 ViewModels 继承自MvxViewModel(因此实现了 INotifyPropertyChanged)。

主视图模型如下所示:

public abstract class BaseViewModel : MvxViewModel, IMvxServiceConsumer
{
    //... just regular implementation
}

public class UploadListViewModel: BaseViewModel
{
    private readonly IUploadItemTasks uploadItemTasks;
    private readonly IPhotoPickerService photoPickerService;

    public IObservableCollection<UploadItemViewModel> Uploads { get { return this.LoadUploadItems(); }  }

    public UploadListViewModel()
    {
        this.uploadItemTasks = this.GetService<IUploadItemTasks>();
        this.photoPickerService = this.GetService<IPhotoPickerService>();
    }

    private IObservableCollection<UploadItemViewModel> LoadUploadItems()
    {
        using (var unitOfWork = UnitOfWork.Start ())
        {
            return new SimpleObservableCollection<UploadItemViewModel>(uploadItemTasks.GetAll());
        }
    }

    public void StartUpload ()
    {
        if (this.Uploads == null || this.Uploads.Count == 0) {
            ReportError("Error", "No images to upload");
            return;
        }

        this.Uploads.ForEach (uploadItem => PostCallback (uploadItem));
    }

    private void PostCallback (UploadItemViewModel uploadAsset)
    {
        IProgressReporter progressReporter = uploadAsset;

        this.photoPickerService.GetAssetFullImage(uploadAsset.ImageUrl,
                                                  (image) => {
            UIImage fullImage = image;
            NSData jpeg = fullImage.AsJPEG();

            byte[] jpegBytes = new byte[jpeg.Length];           
            System.Runtime.InteropServices.Marshal.Copy(jpeg.Bytes, jpegBytes, 0, Convert.ToInt32(jpeg.Length));

            MemoryStream stream = new MemoryStream(jpegBytes);
            Uri destinationUrl = new Uri(uploadAsset.DestinationUrl + "&name=" + uploadAsset.Name + "&contentType=image%2FJPEG");

            //TO DO: Move this to plugin
            var uploader = new Uploader().UploadPicture (destinationUrl, stream, UploadComplete, progressReporter);
            uploader.Host = uploadAsset.Host;

            ThreadPool.QueueUserWorkItem (delegate {
                uploader.Upload ();                 
                jpeg = null;
            });
        });
    }

    private void UploadComplete (string name)
    {
        if (name == null){
            ReportError("Error","There was an error uploading the media.");
        } else 
        {
            //ReportError("Succes", name);
        }
    }


项目 ViewModel 看起来像:

public interface IProgressReporter
{
    float Progress { get; set;} 
}

public abstract class BaseAssetViewModel: BaseViewModel, IBaseAssetViewModel
{
    //... just regular properties 
}

public class UploadItemViewModel: BaseAssetViewModel, IProgressReporter
{
    public UploadItemViewModel(): base()
    {
    }

    private float progress;
    public float Progress {
        get {
            return this.progress;
        }
        set {
            this.progress = value;
            this.RaisePropertyChanged(() => Progress);
        }
    }
}


项目的视图继承自MvxBindableTableViewCell并具有以下属性:

private float progress;
public float ProgressMarker {
    get {
        return progress;
    }
    set {
        progress = value;
        // change progressbar or textfield here
    }
}

tableviewcellUploadItemViewModel通过 BindingText 绑定到:

public const string BindingText = @"ProgressMarker Progress, Converter=Float;";

Uploader片段中提到的类UploadListViewModel实现了一个私有方法,该方法试图在 IProgressReporter 上设置进度。

    float progressValue;
    void SetProgress (float newvalue)
    {
        progressValue = newvalue;

        this.dispatcher.InvokeOnMainThread (delegate {
            if (ProgressReporter != null)
                ProgressReporter.Progress = progressValue;
        });
    }

在第一次查看列表期间,我可以看到 ViewModel 和 View 中的属性都被命中,但是当我通过接口更新 ViewModel 时,tableviewcellIProgressReporter中的 View 中的新值Progress没有更新,也没有调用该属性.

我做错了什么或者我在这里错过了什么?

更新:检查这个问题的答案。

4

1 回答 1

1

我找到了绑定不起作用的原因。我一遍又一遍地替换 ObservableCollection。我按照如下所述更改了那段代码,现在它反映了对单元格视图中的 UploadItemViewModel 所做的更改。

    private IObservableCollection<UploadItemViewModel> uploads;
    private IObservableCollection<UploadItemViewModel> LoadUploadItems()
    {
        if (uploads == null)
        {
            using (var unitOfWork = UnitOfWork.Start ())
            {
                uploads = new SimpleObservableCollection<UploadItemViewModel>(uploadItemTasks.FindAll());
            }
        }
        return uploads;
    }
于 2013-03-28T00:58:16.237 回答