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

Understanding XAML binding

$
0
0

I seem to be missing something in my understanding of XAML binding and I was hoping someone could point me in the right direction. I want to create a tabbed navigation view of a complex object. My thought was to retrieve the object once and then have each page bind to that instance and show the portions that are relevant to the tab.

I created a BindableProperty and I'm using the GetValue and SetValue on the TabbedPage and and followed the same pattern on each of the ContentPage tabs.

In the XAML for the TabbedPage the MyObject="{Binding MyObject}" doesn't seem to do anything. The setter of the MyObject property on the tab pages is never triggered. I tried explicitly setting BindingContext="{Binding MyObject}" of the TabbedPage, and then setting the Binding to MyObject="{Binding .}", but it still didn't set the property of the tab pages.

I looked at an MVVM pattern but all that adds is property change events to the MyObject class and the properties won't change after retrieval. A refresh would be update the entire MyObject instance, as opposed to individual properties.

I can absolutely set the tab properties pragmatically, I was just wondering what I'm doing wrong in the context of data binding.

namespace MyApp.Pages
{
    public partial class MyObjectPage : TabbedPage
    {
        public static readonly BindableProperty MyObjectProperty = BindableProperty.Create<MyObjectPage, MyObject>(
            p => p.MyObject,
            null,
            BindingMode.OneWay,
            propertyChanged: MyObjectChanged);

        public MyObject MyObject 
        {
            get { return (MyObject)GetValue (MyObjectProperty); }
            set { SetValue (MyObjectProperty, value); }
        }

        private int ObjectId;

        public MyObjectPage (int objectId)
        {
            ObjectId = objectId;
            InitializeComponent ();
        }

        public async Task SetMyObject()
        {
            MyObject = await MyObjectService.GetObject (ObjectId);
        }

        protected async override void OnAppearing ()
        {
            base.OnAppearing ();
            await SetMyObject ();
        }

        private static void MyObjectChanged(BindableObject bindable, MyObject oldValue, MyObject newValue)
        {

        }

    }
}

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:pages="clr-namespace:MyApp.Pages;assembly=MyApp"
             x:Class="MyApp.Pages.MyObjectPage" >
    <TabbedPage.Children>
        <pages:MyObjectMainTab 
            x:Name="MainTab" 
            Title="Detail" 
            MyObject="{Binding MyObject}" />
        <pages:MyObjectContactTab
            x:Name="ContactsTab" 
            Title="Contacts" 
            MyObject="{Binding MyObject}" />
    </TabbedPage.Children>
</TabbedPage>

Viewing all articles
Browse latest Browse all 89864

Trending Articles