0
votes

We are using some MT.D StringElements, and their Value Property is bound to properties in the ViewModel.

The initial value is correctly shown but when the ViewModel changes some values and triggers PropertyChanged then the StringElements contain the good value but the display is not refreshed.

If we scroll the Controller or touch the StringElement then it is refreshed: the correct value is displayed.

Do you have any idea?


This is our 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)
            }
        }
    }
}

This is our ViewModel (simplified)

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

2 Answers

1
votes

I found two solutions to my problem:

  • If I use UIView.Transition to replace the content then, in the new View, nothing is refreshed when I change the ViewModel (unless I scroll or tap it) UNLESS if the ViewModel properties have some default value non null and non empty

  • If I don't transition but use another method like this one to replace the content:

Sample code

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

In this case the content is replaced and the view is refreshed when a ViewModel property changes: everything works correctly in this case.

0
votes

I tried a viewmodel like:

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

with a dialog view defined like:

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

It worked fine - ticking up every second...