2016-03-08 77 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

相關問題