Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Why does my GUI not refresh although I have implemented Binding (MVVM)

$
0
0

Hi Guys,

So the point of complete desperation has arrived... I work with Xamarin.Forms and MVVM. I created a Content page containing a StackLayout. Then I added a Child to the StackLayout wich contains a ScrollView. Here is the code of the child:

namespace VCP_Touch
{
    public partial class Ruhestand : ScrollView
    {
        public Ruhestand ()
        {
                InitializeComponent ();
        }
    }
}

and the Xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            xmlns:local="clr-namespace:VCP_Touch;assembly=VCP_Touch"
            x:Class="VCP_Touch.Ruhestand"
            Orientation="Horizontal">
    <ScrollView.BindingContext>
        <local:RuhestandModel />
    </ScrollView.BindingContext>
    <StackLayout>
        <Label  Text="{Binding Eingabe}"
                IsVisible="true"/>

        <Button Text="Refresh"
                Command="{Binding Refresh}"
                HorizontalOptions="FillAndExpand"/>
    </StackLayout>
</ScrollView>

I added a ViewModel, which is named "RuhestandModel":

namespace VCP_Touch
{
    public class RuhestandModel : INotifyPropertyChanged
    {
        public RuhestandModel ()
        {
            Eingabe = "Default";

            this.Refresh = new Command ((nothing) => {
                VO_Datenerfassung test2 = new VO_Datenerfassung ();
                this.Eingabe = test2.Interessent.Vorname;
            });
        }
        string _Eingabe;

        public string Eingabe {
            get {
                return _Eingabe;
            }
            set {
                if (_Eingabe != value) {
                    _Eingabe = value;
                    OnPropertyChangedModel ("Vorname");
                }
            }
        }
        public ICommand Refresh { protected set; get; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChangedModel(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, 
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

"VO_Datenerfassung" is a class, containing several Properties. It can be changed with another button (and it works!!)...The code is not displayed here.
Coming to my problem. Principly the code works fine! When I click the "Refresh" button, the Command Refresh in the constructor "public RuhestandModel ()" is activated. Then also the field "_Eingabe" is changed. But on the GUI the text of the Label (which is bound to Eingabe) does not change.
Where is my fault? I looked at all the examples on the Xamarin Developpers page and I searched the Forum, but I did not found any answer.
You see, I'm truly desperate, because the Binding thing HAS to work, otherwise App programming is not possible...
Thanks in advance!

Ben


Viewing all articles
Browse latest Browse all 58056

Trending Articles