简体   繁体   中英

Basic data binding (Xamarin Forms)

I'm trying to achieve the most basic form of data binding in Xamarin forms though the text that I set in my code are not shown on the labels at all. Any pointers would be great.

CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace XamarinOuderportaal
{
    public partial class LoginPage : ContentPage
    {
        public string UsernamePlaceHolder { get; set; }
        public string PasswordPlaceHolder { get; set; }

        public LoginPage()
        {
            this.UsernamePlaceHolder = "gebruikersnaam";
            this.PasswordPlaceHolder = "wachtwoord";
            InitializeComponent();
        }
    }
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinOuderportaal"
             x:Class="XamarinOuderportaal.LoginPage">

  <StackLayout VerticalOptions="Center" HorizontalOptions="Fill" Spacing="20" Padding="20">
    <Entry Placeholder="This is the placeholder" HorizontalOptions="Fill" IsVisible="{Binding ShouldDisplayUrl}"/>
    <Entry Placeholder="{Binding UsernamePlaceHolder}" HorizontalOptions="Fill"/>
    <Entry Placeholder="{Binding PasswordPlaceHolder}" HorizontalOptions="Fill" IsPassword="true"/>
    <Button Text="test 2" HorizontalOptions="Fill"/>
  </StackLayout>
</ContentPage>

Assign the bindingcontext and onpropertychange event for member variable in the viewmodel file

Check it basic of Xamarin binding http://www.c-sharpcorner.com/article/quick-start-tutorial-creating-universal-apps-via-xamarin-binding-in-xaml-par/

Binding seeks on the instance of the source object, so the properties must not be static.

If you want to use static binding then you must use the Static extension:

<Entry Placeholder="{x:Static local:LoginPage.UsernamePlaceHolder}" HorizontalOptions="Fill"/>

EDIT: You have edited the question and removed the "static" definition, so the code must work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM