[英]How to auto-capitalize keyboard/entry in xamarin forms
我有兩個條目用戶名和密碼。 用戶名和密碼總是大寫。 我如何強制鍵盤或輸入項,當我輸入內容時,文本將是大寫的?
<Frame StyleClass="lpframe" x:Name="usernameFrame" CornerRadius="5" BorderColor="Transparent" HasShadow="False">
<StackLayout>
<Label Text="USERNAME" StyleClass="lbl-login">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android" Value="OpenSans-Semibold.ttf#OpenSans-Semibold"/>
</OnPlatform>
</Label.FontFamily>
</Label>
<local:CustomEntry Placeholder="Username" PlaceholderColor="#879baa" Unfocused="entUser_Unfocused" StyleClass="form-control" ReturnType="Next" x:Name="entUser">
<local:CustomEntry.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android" Value="OpenSans-Regular.ttf#OpenSans-Regular"/>
</OnPlatform>
</local:CustomEntry.FontFamily>
</local:CustomEntry>
</StackLayout>
</Frame>
<Frame StyleClass="lpframe" x:Name="passwordFrame" CornerRadius="5" BorderColor="Transparent" HasShadow="False">
<StackLayout>
<Label Text="PASSWORD" StyleClass="lbl-login">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android" Value="OpenSans-Semibold.ttf#OpenSans-Semibold"/>
</OnPlatform>
</Label.FontFamily>
</Label>
<local:CustomEntry Placeholder="Password" PlaceholderColor="#879baa" Unfocused="entPassword_Unfocused" IsPassword="True" StyleClass="form-control" ReturnType="Go" x:Name="entPassword">
<local:CustomEntry.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android" Value="OpenSans-Regular.ttf#OpenSans-Regular"/>
</OnPlatform>
</local:CustomEntry.FontFamily>
</local:CustomEntry>
</StackLayout>
</Frame>
現在您可以使用 Create 方法工廠來修改鍵盤行為,如下所示:
<Entry Placeholder="Enter text here">
<Entry.Keyboard>
<Keyboard x:FactoryMethod="Create">
<x:Arguments>
<KeyboardFlags>CapitalizeWord</KeyboardFlags>
</x:Arguments>
</Keyboard>
</Entry.Keyboard>
這是官方文檔: 自定義鍵盤
以下是所有支持的標志:
你可以很容易地通過PlatformEffect
做到這一點。
<Entry Placeholder="Password" IsPassword="true">
<Entry.Effects>
<sushi:CapsEntrytEffect />
</Entry.Effects>
</Entry>
注意:本例中的 xmlns:sushi 是一個用於RoutingEffect
實現位置的xmlns
此類將通過 XAML 示例中的sushi
xmlns 引用
public class CapsEntrytEffect : RoutingEffect
{
public CapsEntrytEffect() : base("Sushi.CapsEntrytEffect")
{
}
}
注意:您正在將InputFilterAllCaps
添加到 Android 小部件 EditText 的當前過濾器列表中。
public class CapsEntrytEffect : PlatformEffect
{
protected override void OnAttached()
{
var editText = Control as EditText;
editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());
}
protected override void OnDetached()
{
}
}
public class CapsEntrytEffect : PlatformEffect
{
protected override void OnAttached()
{
var editField = Control as UITextField;
editField.AutocapitalizationType = UITextAutocapitalizationType.AllCharacters;
}
protected override void OnDetached()
{
}
}
在您實現 PlatformEffect 的每個項目中,請確保包含ResolutionGroupName
和ExportEffect
屬性,以便 Forms 可以找到並激活該平台的效果。
[assembly: ResolutionGroupName("Sushi")]
[assembly: ExportEffect(typeof(CapsEntrytEffect), "CapsEntrytEffect")]
回復: https : //blog.xamarin.com/customizing-xamarin-forms-controls-with-effects/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.