0

Iam very much new to xamarin and wondering about how BindingContext is used.

I was going through a tutorial wherein they used BindingContext = Application.Current

according to the docs Application.Current should return Application. so how can this above statement work?

Mahi Tej Gvp
  • 984
  • 1
  • 14
  • 34
  • `BindingContext` can be any `object`. It does not have to be any specific type. – Jason Feb 27 '20 at 12:22
  • Thank you Jason, but if i am using any `members` defined in `App class` from my code behind i have to use `(App)Application.Current` where iam `casting` `Application.Current` to `App`, whereas while using `BindingContext = Application.Current` its not needed. I can directly bind the `members` defined in `App` class, and was wondering about whats going on there in `BindingContext`. – Suraj Suddala Feb 28 '20 at 06:16
  • Binding uses Reflection to determine which properties are available – Jason Feb 28 '20 at 11:54
  • I have two scenarios where i can acheive the samae work i.e., accessing the members of `App()` class. They are: **1.** `(Application.Current as App).memberName` and **2.** `BindingContext = Applicaiton.Current`. First one is clear that we have declared members in `App()` class and have to cast the `Application` object returnded by `Application.Current` into `App`, But how does Binding know to check for `App()` class when we write `Application.Current` there. – Suraj Suddala Mar 02 '20 at 08:27
  • @SurajSuddala yes, if you must use Application.Current to binding, you can define property in App.cs, and implement INotifyPropertyChanged, then you can use BindingContext = Application.Current in contentpage, I test it and have no issue. – Cherry Bu - MSFT Mar 05 '20 at 07:16
  • @CherryBu-MSFT thank you, but my doubt is that how can we use the members defined in `App()` class when we are using `Application.Current` in the `BindingContext` which returns an instance of `Application` not an instance `App()`. – Suraj Suddala Mar 06 '20 at 09:04

1 Answers1

0

Firstly, create one property in APp.cs, implement interface INotifyPropertyChanged.

 public partial class App : Application, INotifyPropertyChanged
{
    private string _str;
    public string str
    {
        get { return _str; }
        set
        {
            _str = value;
            RaisePropertyChanged("str");
        }
    }
    public App()
    {
        InitializeComponent();
        str = "this is test";
        MainPage = new NavigationPage(new simplecontrol.Page26());         
    }


    public event PropertyChangedEventHandler PropertyChanged;      
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

Then binding Application.Current for ContentPage BindingContext.

<ContentPage.Content>
    <StackLayout>
        <!--<local:View2 Items="{Binding modelas}" />-->
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="{Binding str}"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

 this.BindingContext = Application.Current;
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16