将StringElement(MT.D)绑定到MvvmCross

时间:2013-09-24 13:24:08

标签: mvvmcross monotouch.dialog

我们正在使用一些MT.D StringElements,它们的Value属性绑定到ViewModel中的属性。

正确显示初始值但是当ViewModel更改某些值并触发PropertyChanged时,StringElements包含良好的值但显示不会刷新。

如果我们滚动Controller或触摸StringElement,则刷新它:显示正确的值。

你有什么想法吗?


这是我们的ViewController

public class ContactView : MvxDialogViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        var bindings = this.CreateInlineBindingTarget<ContactViewModel> ();

        Root = new RootElement()
        {
            new Section()
            {
                new StringElement("Company Name").Bind(bindings, vm => vm.CompanyName)
            }
        }
    }
}

这是我们的ViewModel(简化)

public class ContactViewModel : MvxViewModel
{
    private string companyName;
    public string CompanyName{
        get{return companyName;}
        set{companyName = value; RaisePropertyChanged(() => CompanyName);}
    }

    public async Task Init(string id)
    {
        var contact = await someService.SomeMethodAsync();
        CompanyName = contact.CompanyName;
    }
}

2 个答案:

答案 0 :(得分:1)

我找到了两个解决问题的方法:

  • 如果我使用UIView.Transition来替换内容,那么在新视图中,当我更改ViewModel时不会刷新任何内容(除非我滚动或点击它) UNLESS 如果ViewModel属性有一些默认值非null且非空

  • 如果我不转换,但使用其他方法替换内容:

示例代码

 MasterNavigationController.PopToRootViewController(false);
 MasterNavigationController.ViewControllers = new UIViewController[] { viewController };

在这种情况下,替换内容并在ViewModel属性更改时刷新视图:在这种情况下一切正常。

答案 1 :(得分:0)

我尝试了一个像:

的视图模型
public class FirstViewModel 
    : MvxViewModel
{
    private Timer _timer;
    private int _count;

    public FirstViewModel()
    {
        _timer = new Timer(DoThis, null, 1000, 1000);    
    }

    private void DoThis(object state)
    {
        _count++;
        TextProperty = _count.ToString();
    }

    private string _textProperty = "T";
    public string TextProperty
    {
        get { return _textProperty; }
        set { _textProperty = value; RaisePropertyChanged(() => TextProperty); }
    }
}

使用对话框视图定义如下:

        Root = new RootElement("Example Root")
            {
                new Section("Debut in:")
                    {
                        new EntryElement("Login", "Enter Login name").Bind(bindings, vm => vm.TextProperty)
                    },
                new Section("Debug out:")
                    {
                        new StringElement("Value is:").Bind(bindings, vm => vm.TextProperty),
            };

它工作得很好 - 每秒钟滴答......