最初の回答で正しく述べたように、キーボードフラグを設定することはできません。あなたは確かに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
添付されたプロパティに関する後続の質問だけ。それをブランケットスタイルの一部として設定することは可能ですか?すなわち 'TargetType'を使用するか、またはそれぞれの' Entry'要素に対してこれらを設定する必要がありますか? – user1