【问题标题】:Binding a property to a ViewModel with MvvmLight and Xamarin.iOS使用 MvvmLight 和 Xamarin.iOS 将属性绑定到 ViewModel
【发布时间】:2015-03-24 22:50:25
【问题描述】:

我已经使用 MvvmLight 很长时间了,它非常适合我的 Windows 和 Windows Phone 开发需求,但我在版本 5 中引入的新 Xamarin.iOS 绑定功能遇到了困难。

我检查了Flowers 示例,并尝试创建一个无法按预期工作的非常简单的绑定:更新操作只执行一次...

这里是视图控制器的代码:

 public partial class MainViewController : UIViewController
{
    private MainViewModel ViewModel { get; set; }

    public MainViewController()
        : base("MainViewController", null)
    {
        this.ViewModel = new MainViewModel();
    }

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

        this.SetBinding(() => this.ViewModel.IsUpdated).WhenSourceChanges(() =>
            {
                this.updateLabel.Text = this.ViewModel.IsUpdated ? "It's okay !" : "Nope ...";
            });

        this.updateButton.SetCommand("TouchUpInside", this.ViewModel.UpdateCommand);

    }
}

生成的带有两个接口元素的部分类声明:

[Register ("MainViewController")]
partial class MainViewController
{
    [Outlet]
    MonoTouch.UIKit.UIButton updateButton { get; set; }

    [Outlet]
    MonoTouch.UIKit.UILabel updateLabel { get; set; }

    void ReleaseDesignerOutlets ()
    {
        if (updateLabel != null) {
            updateLabel.Dispose ();
            updateLabel = null;
        }

        if (updateButton != null) {
            updateButton.Dispose ();
            updateButton = null;
        }
    }
}

以及相关的 ViewModel :

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        this.UpdateCommand = new RelayCommand(() =>
            {
                this.IsUpdated = !this.IsUpdated;
            });
    }

    private bool isUpdated;

    public bool IsUpdated
    {
        get { return this.isUpdated; }
        set
        {
            this.Set<bool>(ref this.isUpdated, value);
        }
    }

    public RelayCommand UpdateCommand { get; private set; }
}

谁有一个可行的例子,以及一些解释?

【问题讨论】:

    标签: c# ios mvvm xamarin mvvm-light


    【解决方案1】:

    您必须将在 SetBinding 中创建的 Binding 存储在您的 ViewController 中,否则一旦您离开 ViewDidLoad 的范围,它就会消失。在 Flowers 示例中,该代码仅在视图加载期间运行。由于值的变化,它永远不会运行。

    public partial class MainViewController : UIViewController
    {
        private Binding<bool, bool> _isUpdatedBinding;
        private MainViewModel ViewModel { get; set; }
    
        public MainViewController()
            : base("MainViewController", null)
        {
            this.ViewModel = new MainViewModel();
        }
    
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
    
            _isUpdatedBinding = this.SetBinding(() => this.ViewModel.IsUpdated).WhenSourceChanges(() =>
                {
                    this.updateLabel.Text = this.ViewModel.IsUpdated ? "It's okay !" : "Nope ...";
                });
    
            this.updateButton.SetCommand("TouchUpInside", this.ViewModel.UpdateCommand);
    
        }
    }
    

    我相信这些更改应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2011-02-01
      • 1970-01-01
      相关资源
      最近更新 更多