Câu hỏi phỏng vấn Xamarin
Câu hỏi

How to perform Binding in Code Behind?

Câu trả lời

To perform data binding in the code-behind in Xamarin.Forms, you need to follow these steps:

  1. Create the Binding Object: Instantiate a Binding object and set its properties, such as the path to the source property.

  2. Set the Source: Specify the source of the binding, which can be a control or a data context.

  3. Apply the Binding: Use the SetBinding method to apply the binding to the target control.

Here is a step-by-step example:

Step-by-Step Example

  1. Define the XAML Layout:

    xml Copy
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="YourNamespace.MainPage">
        <StackLayout>
            <Entry x:Name="entry" Placeholder="Enter text here" />
            <Label x:Name="label" />
        </StackLayout>
    </ContentPage>
  2. Create the Binding in Code-Behind:

    csharp Copy
    using Xamarin.Forms;
    
    namespace YourNamespace
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
    
                // Create a new Binding object
                var binding = new Binding
                {
                    Source = entry, // Set the source to the Entry control
                    Path = "Text"   // Bind to the Text property of the Entry
                };
    
                // Apply the binding to the Label's Text property
                label.SetBinding(Label.TextProperty, binding);
            }
        }
    }

Explanation

  1. XAML Layout: The XAML defines a simple layout with an Entry and a Label. The x:Name attribute is used to reference these controls in the code-behind.

  2. Code-Behind:

    • Binding Object: A Binding object is created and configured. The Source property is set to the Entry control, and the Path property specifies that the ...
middle

middle

Gợi ý câu hỏi phỏng vấn

senior

How many ways you can Bind a ViewModel with XAML?

senior

How to render different types of ViewCell in the same ListView during runtime?

senior

Name some types of Behaviors in Xamarin?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào