[英]Basic data binding (Xamarin Forms)
盡管我在代碼中設置的文本根本沒有顯示在標簽上,但我正在嘗試以Xamarin形式實現最基本的數據綁定形式。 任何指針都很棒。
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>
為viewmodel文件中的成員變量分配bindingcontext和onpropertychange事件
檢查Xamarin綁定的基礎知識http://www.c-sharpcorner.com/article/quick-start-tutorial-creating-universal-apps-via-xamarin-binding-in-xaml-par/
綁定查找源對象的實例,因此屬性不能為靜態。
如果要使用靜態綁定,則必須使用靜態擴展名:
<Entry Placeholder="{x:Static local:LoginPage.UsernamePlaceHolder}" HorizontalOptions="Fill"/>
編輯:您已經編輯了問題,並刪除了“靜態”定義,所以該代碼現在必須工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.