正如在第一個答案中提到的那樣,設置鍵盤標誌是不可能的。雖然你當然可以繼承Entry
,還有就是創建一個attached property一個更優雅的方式:
public class KeyboardStyle
{
public static BindableProperty KeyboardFlagsProperty = BindableProperty.CreateAttached(
propertyName: "KeyboardFlags",
returnType: typeof(string),
declaringType: typeof(InputView),
defaultValue: null,
defaultBindingMode: BindingMode.OneWay,
propertyChanged: HandleKeyboardFlagsChanged);
public static void HandleKeyboardFlagsChanged(BindableObject obj, object oldValue, object newValue)
{
var entry = obj as InputView;
if(entry == null)
{
return;
}
if(newValue == null)
{
return;
}
string[] flagNames = ((string)newValue).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
KeyboardFlags allFlags = 0;
foreach (var flagName in flagNames) {
KeyboardFlags flags = 0;
Enum.TryParse<KeyboardFlags>(flagName.Trim(), out flags);
if(flags != 0)
{
allFlags |= flags;
}
}
Debug.WriteLine("Setting keyboard to: " + allFlags);
var keyboard = Keyboard.Create(allFlags);
entry.Keyboard = keyboard;
}
}
然後從內XAML中使用它(不要忘記添加local
命名空間):
<?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:KeyboardTest"
x:Class="KeyboardTest.KeyboardTestPage">
<Entry x:Name="entry" Text="Hello Keyboard" local:KeyboardStyle.KeyboardFlags="Spellcheck,CapitalizeSentence"/>
</ContentPage>
你也可以使用它作爲毯子樣式的一部分,如下所示:
<Style TargetType="Entry">
<Setter Property="local:KeyboardStyle.KeyboardFlags"
Value="Spellcheck,CapitalizeSentence"/>
</Style>
哇這是一個好主意。我完全忘記了附加屬性! – user1
只是關於附加屬性的後續問題。是否可以將它們設置爲blankey樣式的一部分?即使用'TargetType'還是必須爲每個'Entry'元素設置? – user1