Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly do data binding in Xamarin Forms?

I’m incredibly new to Xamarin Forms and I’m super lost. Could someone please give me an example of primary data binding to a label using MVVM?

I currently have View of InformationPage ViewModel of InformationPageModel and Model of ResourceGatheringLogic.

I was trying to figure out how to read JSON from embedded resources but I can’t even properly bind a string to display HelloWorld for data binding.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would only like a very simple example of how to properly do this and I can most likely do the rest. Many Thanks in advance.

My .xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:uCue_Game.ViewModel"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="uCue_Game.View.InformationPage">
    <ContentPage.BindingContext>
        <local:InformationPageModel/>
    </ContentPage.BindingContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <Frame BackgroundColor="LightGreen"
               VerticalOptions="FillAndExpand"
               HorizontalOptions="FillAndExpand">
            <Label Text="{Binding MediaSource}"
                   TextColor="Black"
                   FontSize="20"
                   FontAttributes="Bold"
                   VerticalOptions="CenterAndExpand"
                   HorizontalOptions="CenterAndExpand"
                   HeightRequest="300"
                   WidthRequest="300"/>
        </Frame>
    </Grid>
    
</ContentPage>

My .xaml.cs

namespace uCue_Game.View
{
    using Xamarin.Forms;


    public partial class InformationPage : ContentPage
    {
        public InformationPage()
        {
            InitializeComponent();
        }
    }
}

My ViewModel .cs Ignore the SetMediaSource it is for later use.

namespace uCue_Game.ViewModel
{
    using global::Model;
    using Xamarin.Forms;

    public class InformationPageModel
    {
        IMediaSource mediaSource;
        public string MediaSource = "Hello";

        public InformationPageModel()
        {
            this.mediaSource = DependencyService.Get<IMediaSource>();
            //SetMediaSource();
        }

        public void SetMediaSource()
        {
            if (mediaSource != null)
                return;

            MediaSource = mediaSource.GetMediaSource();
        }
    }
}

>Solution :

you can only bind to public properties

public string MediaSource { get; set; } = "Hello";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading