2014-01-24 96 views
1

我有一個UISeekbar和位於內部的MvxViewUISlider沒有更新到綁定視圖模型屬性

 _currentPositionText = new UILabel { TextColor = UIColor.White }; 
     _seekBar = new UISlider 
      { 
       MinValue = 0, 
       Continuous = true, 
       AutoresizingMask = UIViewAutoresizing.FlexibleWidth 
      }; 
     _seekBar.TouchDown += OnTouchDown; 
     _seekBar.TouchUpInside += OnTouchUpInside; 
     _seekBar.TouchDragInside += OnDragInside; 

我設置的綁定如下

 set.Bind(_seekBar).For(sb => sb.Value).To(vm => _viewModel.CurrentPositionMsec); 
     set.Bind(_seekBar).For(sb => sb.MaxValue).To(vm => _viewModel.DurationMsec); 
     set.Bind(_currentPositionText).To(vm => vm.CurrentPositionText); 

如果我加入一些跟蹤一個UILabel CurrentPositionMsecget內的代碼,我可以看到屬性每秒更新一次(如預期)。

注意:同樣的方法也更新CurrentPositionText只需將毫秒格式化爲TimeSpan。

_currentPositionText的綁定正在按預期更新,但_seekBar不更新滑塊位置。

預期的結果是滑塊每秒更新一次,基於CurrentPositionMsec

我在Android中使用以下綁定工作,並且Android和iOS都共享相同的viewModel。

<SeekBar 
    android:Foo="" 
    android:Bar="" 
    local:MvxBind="Max DurationMsec; Progress CurrentPositionMsec" /> 

我沒有設置正確的東西嗎?這個「應該」工作AFAIK。

回答

2

重新排序綁定似乎不產生任何更好的效果

// binding MaxValue first doesn't fix (lambda) 
set.Bind(_seekBar).For(sb => sb.MaxValue).To(vm => _viewModel.DurationMsec); 
set.Bind(_seekBar).For(sb => sb.Value).To(vm => _viewModel.CurrentPositionMsec); 

更改綁定使用一個字符串,而不是一個拉姆達DID證明解決問題。

// using a string did enable the bindings to work as expected. 
set.Bind(_seekBar).For("MaxValue").To(vm => _viewModel.DurationMsec); 
set.Bind(_seekBar).For("Value").To(vm => _viewModel.CurrentPositionMsec); 

我們現在選擇在我們的代碼庫中使用字符串作爲標準。由於我們沒有得到編譯時錯誤,所以它確實留下了錯誤的空間,但至少它確保了綁定的工作。

0

我添加了第二個搜索欄的SeekView樣品中https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/ApiExamples

此代碼看來效果不錯:

public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     var label = new UILabel(new RectangleF(10, 100, 100, 30)); 
     label.Text = "Slide me:"; 
     Add(label); 
     var seek = new UISlider(new RectangleF(110, 100, 200, 30)); 
     seek.MinValue = 0; 
     seek.MaxValue = 100; 
     Add(seek); 
     var seek2 = new UISlider(new RectangleF(110, 160, 200, 30)); 
     seek2.MinValue = 0; 
     seek2.MaxValue = 100; 
     Add(seek2); 
     var mirrorLabel = new UILabel(new RectangleF(110, 130, 200, 30)); 
     mirrorLabel.TextColor = UIColor.Blue; 
     Add(mirrorLabel); 

     var set = this.CreateBindingSet<SeekView, SeekViewModel>(); 
     set.Bind(seek).To(vm => vm.SeekProperty); 
     set.Bind(seek2).To(vm => vm.SeekProperty); 
     set.Bind(mirrorLabel).To(vm => vm.SeekProperty); 
     set.Apply(); 
    } 

simulator image

顯然,這只是練習的當前值和的最大 - 但希望得到這個工作將有所幫助。


我唯一的建議可能是嘗試重新排序你的綁定 - 以便MaxValue在Value之前設置。或者嘗試使用固定的MaxValue,然後使用值轉換器(或其他機制)來縮放當前值。

相關問題