question

KamranAli-0346 avatar image
0 Votes"
KamranAli-0346 asked WenyanZhang-MSFT commented

Xamarin Forms Binding Cotext for Activity type of Class

Hello Folks,
For all the code I can find online, it seems that the BindingContext command can only be used when the type of class is ContentPage. Is it possible for me to use BindingContext for Activity type of class?
Visual Studio generates Activity classes automatically when creating Xamarin Forms for android solutions, by the way.

Any help is welcome here.

Thanks.

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@WenyanZhang-MSFT Thanks for your note.
Below is part of my code, which got auto generated when I created a new Prodject in Visual Studio 2019.
It is in this class that I am unable to use the BindingContext functionality.

I think what you are saying is that I will not be able to use BindinContext in this class at all.
Can you then guide me what I should do? My App has 2 screens and I want to pass data from one to the other. MainActivity.cs (shown below) is the first page, by the way.

 using System;
 using Android.App;
 using Android.OS;
 using Android.Runtime;
 using Android.Views;
 using AndroidX.AppCompat.App;
 using Android.Widget;
 using Azure;
 using Azure.AI.TextAnalytics;
 using System.Threading.Tasks;
 using System.Collections.Generic;

 namespace hikmatNotes
 {
     [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
     public class MainActivity : AppCompatActivity
     {
         EditText inputNotes;
         Android.Widget.Button btnProcess;





0 Votes 0 ·

Try to pass value in MainActivity

 var serviceIntent = new Intent(this, typeof(MyActivity));
             serviceIntent.PutExtra("Name", "111");
             this.StartActivity(serviceIntent);

and get the valve in MyActivity

 protected override void OnCreate(Bundle savedInstanceState)
         {
             base.OnCreate(savedInstanceState);
             string name = Intent.GetStringExtra("Name");
          }

I have converted the comment to an answer, you could check it. I'm not very clear about what value you want to pass to the second interface, you could provide more code snippets about your first, second Activity and describe more details about what you want to pass.
In this thread, we are mainly discussing about this issue(If it is possible to use BindingContext for Activity type of class) as indicated by the first post. And it is solved. Then please try to mark the answer which help you.  It will encourage the person who help you. Then for your new question, it would be best if you try to open up a new thread for it. In this way, it will make answer searching in the forum easier and be beneficial to other community members as well.

1 Vote 1 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT edited

Hi @KamranAli-0346 ,
You mean you want to use binding under Android platform, I have to say, the answer is No.

Data binding is the technique of linking properties of two objects so that changes in one property are automatically reflected in the other property. Data binding is an integral part of the Model-View-ViewModel (MVVM) application architecture.
And the BindingContext property indicates the binding source. It's for Xamarin.Forms, refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.