0

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;
    }
}
Fabien
  • 1,015
  • 2
  • 11
  • 22

2 Answers2

1

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.

Fabien
  • 1,015
  • 2
  • 11
  • 22
0

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...

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • Sorry - not a very good answer, but I did try to repro the issue. I think your question might benefit from some code demonstrating your setup and the issue - see http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx for some other ideas to help you help SO to provide answers. – Stuart Sep 24 '13 at 14:01
  • Ok I have tried using also your timer in my ViewModel and the result is quite weird: the StringElement is updated but not refreshed, and once I scroll then it's updated and refreshed, and continues to be refreshed for every ticks. – Fabien Sep 24 '13 at 14:30
  • Ok it seems that when the initial property value is null then... it doesn't refresh the display if the value changes. If the initial property is string.Empty then everything works – Fabien Sep 24 '13 at 14:44
  • 1
    Ok it's different: if the initial value is null or empty it doesn't work. If the initial value is a real string like test then it will behave correctly. I think that it has something to do with RaisePropertyChanged that does some checks. But I can't be sure. – Fabien Sep 24 '13 at 14:56
  • I am also seeing this - it's a pain if the string doesn't need a value until a certain point. Such as an error summary label or something –  Nov 27 '13 at 16:14