Search code examples
c#xamarinmvvmdata-bindinginotifypropertychanged

Xamarin using INotifyPropertyChanged binding with Entry and Label and MVVM


I'm getting a string by an intent in MainActivity.cs of the Android-project. I'm using a Sharedproject. The string is a barcode which is updated by the intent if a barcode is scanned. I want to show the barcode-string in a Entry or Label of the .xaml-file via data binding.

The problem is that the barcode only updates in the Entry and in the Label aswell, if I manually clicked the entry. How can I make sure the Label or the Entry is updated when the string from the intent is changing?

Code:

MainActivity.cs:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity Instance;
    myBroadcastReceiver receiver;
    BarcodeModel barcodeModel;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        // Barcode
        MainActivity.Instance = this;
        receiver = new myBroadcastReceiver();
        barcodeModel = new BarcodeModel();

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    protected override void OnResume()
    {
        base.OnResume();
        //  Register the broadcast receiver dynamically
        RegisterReceiver(receiver, new IntentFilter(Resources.GetString(Resource.String.activity_intent_filter_action)));
    }

    protected override void OnPause()
    {
        base.OnPause();
        UnregisterReceiver(receiver);
    }

    public void DisplayResult(Intent intent)
    {
        //  Output the scanned barcode to ViewModel
        barcodeModel.decodedData = intent.GetStringExtra(Resources.GetString(Resource.String.datawedge_intent_key_data));


    }
}

//  Broadcast receiver to receive scanned data
[BroadcastReceiver(Enabled = true)]
public class myBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        String action = intent.Action;
        if (action.Equals(MainActivity.Instance.Resources.GetString(Resource.String.activity_intent_filter_action)))
        {
            //  A barcode has been scanned
            MainActivity.Instance.RunOnUiThread(() => MainActivity.Instance.DisplayResult(intent));
        }
    }

}

}

ViewModel:

 public class BarcodeModel : INotifyPropertyChanged
{
    private string data;
    public event PropertyChangedEventHandler PropertyChanged;

    public BarcodeModel()
    {

    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public string decodedData
    {
        get { return data; }
        set { data = value; OnPropertyChanged(); }
    }

ContentPage:

        <Label Text="{Binding decodedData}"
                   HorizontalOptions="StartAndExpand"
                   VerticalOptions="Center"
                   TextColor="Accent"/>

        <Entry Keyboard="Text"
                   Placeholder="No Object selected"
                   VerticalOptions="Center"
                   HorizontalOptions="StartAndExpand"
                   x:Name="eintrag"
                   Text="{Binding decodedData}"/>

BindingContext in .xaml.cs: BindingContext = new BarcodeModel();


Solution

  • The model you changing in the Android project is not the same one as your bindingContext, so it doesn't work.

    Solution:

    Use messagingCenter to send the model from your Android project to your shared project and update the model there:

    In the Android project, send the message everytime you scanned a barode:

    public void DisplayResult(Intent intent)
    {
        //  Output the scanned barcode to ViewModel
        barcodeModel.decodedData = intent.GetStringExtra(Resources.GetString(Resource.String.datawedge_intent_key_data));
        barcodeModel.decodedData = "test";
    
        MessagingCenter.Send<object, BarcodeModel>(new object(), "barCodeScanned", barcodeModel);
    }
    

    In the shared project, subscribe to that message and update the model:

    public partial class MainPage : ContentPage
    {
        BarcodeModel barCodeModel;
    
        public MainPage()
        {
            InitializeComponent();
    
            barCodeModel = new BarcodeModel();
            barCodeModel.decodedData = "defaultValue";
            this.BindingContext = barCodeModel;
    
            MessagingCenter.Subscribe<object, BarcodeModel>(new object(), "barCodeScanned", (sender, arg) =>
            {
                BarcodeModel tempbarCodeModel = arg as BarcodeModel;
                barCodeModel.decodedData = tempbarCodeModel.decodedData;
            });
        }
    }
    

    I uploaded my test project here and feel free to ask me any question.