2016-03-08 78 views
1

说我想Entry元素不具有自动更正或自动大写。这可以通过设置它的键盘属性,像这样做:Xamarin.Forms - 可以在全局设置一个元素属性?

new Entry { Keyboard = Keyboard.Create(0) } 

现在,我怎么做,对所有入境元素全局默认

我知道我可以创建一个定制元素,从内置的元素继承,并覆盖的方式,像财产:

public class EntryCustom : Entry 
{ 
    public EntryCustom() 
    { 
     Keyboard = Keyboard.Create(0); 
    } 
} 

,然后简单地调用它:

new EntryCustom { ... } 

但是,有没有办法直接在内置的元素类型做到这一点,而无需创建一个自定义的元素类型?

回答

2

可以通过保存自定义键盘为静态,然后将其绑定到使用默认样式的所有输入字段做到这一点。您可以将该默认样式放入应用程序范围的资源字典中,该字典会自动应用于整个应用程序。下面是我刚刚测试了一个新的空表格项目来验证示例代码:

步骤1.保存自定义键盘为静态。

Keyboards.cs(静态自定义键盘):

using Xamarin.Forms; 

namespace KeyboardDemo 
{ 
    public static class Keyboards 
    { 
     public static Keyboard Unassisted { get; private set; } 

     static Keyboards() 
     { 
      Unassisted = Keyboard.Create (0); 
     } 
    } 
} 

第2步:为您的项目创建一个App.xaml中。

关注这个头进行的App.xaml添加到您的表格项目:http://jfarrell.net/2015/02/02/centralize-your-styles-with-xamarin-forms/

第3步:添加默认样式的App.xaml

应用。XAML:

<?xml version="1.0" encoding="UTF-8"?> 
<Application 
     xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:demo="clr-namespace:KeyboardDemo" 
     x:Class="KeyboardDemo.App"> 

    <Application.Resources> 
     <ResourceDictionary> 
      <Style x:Key="EntryStyle" TargetType="Entry"> 
       <Setter Property="Keyboard" Value="{x:Static demo:Keyboards.Unassisted}" /> 
      </Style> 
      <Style BasedOn="{StaticResource EntryStyle}" TargetType="Entry" /> 
     </ResourceDictionary> 
    </Application.Resources> 

</Application> 

第4步:添加一个新的页面到项目

一个ContentPage添加到应用程序,与普通的输入控件来验证的造型。

App.xaml.cs:

using Xamarin.Forms; 

namespace KeyboardDemo 
{ 
    public partial class App : Application 
    { 
     public App() 
     { 
      InitializeComponent(); 
      MainPage = new MyPage(); 
     } 
    } 
} 

MyPage.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" 
     x:Class="KeyboardDemo.MyPage"> 
    <ContentPage.Padding> 
     <OnPlatform x:TypeArguments="Thickness" iOS="0,20,0,0" Android="0" WinPhone="0" /> 
    </ContentPage.Padding> 

    <StackLayout> 
     <Entry /> 
    </StackLayout> 
</ContentPage> 
0

*编辑:说回这个答案获得一些新发现的XAML知识,搞清楚如何做到这一切的XAML后!

请参阅下文,仅使用XAML将Keyboard与​​s相加到Style

另见here可用​​值。下面我只使用None

<ContentPage.Resources> 
    <ResourceDictionary> 
    <Keyboard x:Key="NoCapitalizationKeyboard" 
       x:FactoryMethod="Create"> 
     <x:Arguments> 
     <KeyboardFlags>None</KeyboardFlags> 
     </x:Arguments> 
    </Keyboard> 

    <Style x:Key="NoCapitalizationEntryStyle" 
      TargetType="Entry"> 
      <Setter Property="Keyboard" 
        Value="{StaticResource NoCapitalizationKeyboard}"> 
     </Style> 
    <Style BasedOn="{StaticResource NoCapitalizationEntryStyle}" 
      TargetType="Entry" /> 
    </ResourceDictionary> 
</ContentPage.Resources> 

<Entry /> 

- 老回答 -

不幸的是,我不认为你可以做到这一点与全局样式,因为你可以在键盘上只设置于上市here预定义的键盘组中的一个而不是作为能够单独打开和关闭特定的功能。

另一种选择是创建条目控制本身就是一个自定义渲染,然后实现代码来关闭它在每个平台。适用于iOS的this(除了我认为您会使用Control.AutocapitalizationType = UITextAutocapitalizationType.None;)。

+0

的'Keyboard'属性是可绑定的,所以它的确可以通过样式来分配。 –

+0

@KiethRome对,但是这迫使你选择Xamarin提供的预选键盘。您不仅可以使用样式禁用自动大写。因此,您可以选择文本或数字,或者除此之外还会更改其他键盘属性,而不仅仅是自动大写。 *编辑:看到你现在回答!我把它全部拿回来;) – hvaughan3

相关问题