2017-11-29 11 views
0

プロパティを持つカスタムコントロールを作成する必要があります。プロパティでカスタムコントロールを作成する方法

これは私のカスタムコントロールです:

public class ExpandableStackLayout : StackLayout 
{ 
    public String Title{get;set;} 

    public ContentView View { set; get; } 

    public String Image{set;get;} 


    public ExpandableStackLayout(string title, string image, ContentView view) 
    { 
     Title = title; 
     Image = image; 
     View = view; 
    } 
} 

しかし、私は、XAMLでそれを使用しようとすると言う:「いいえデフォルトコンストラクタが見つかりません」と、私はこれをデフォルトコンストラクタ呼び出しを作成していない場合はパラメータでコンストラクタを呼び出します。

これは私のXAMLです:

<control:ExpandableStackLayout Title="Title" Image="imag.png"> 
      <control:ExpandableStackLayout.View> 
       <ContentView> 
        <OtherView/> 
       </ContentView> 
      </control:ExpandableStackLayout.View> 
     </control:ExpandableStackLayout> 

UPDATE

私はあなたのヒントを試してみましたし、私は他の問題を抱えて:コンストラクタを作成する必要はありません

public static readonly BindableProperty TitleProperty = BindableProperty.Create(
     propertyName: "Title", 
     returnType: typeof(String), 
     declaringType: typeof(String), 
     defaultValue: String.Empty); 

    public String Title 
    { 
     get => (String)GetValue(TitleProperty); 
     set => SetValue(TitleProperty, value); 
    } 

    public ContentView View { set; get; } 

    public static readonly BindableProperty ImageProperty = BindableProperty.Create(
     propertyName: "Image", 
     returnType: typeof(String), 
     declaringType: typeof(String), 
     defaultValue: string.Empty); 

    public String Image 
    { 
     get => (String)GetValue(TitleProperty); 
     set => SetValue(ImageProperty, value); 
    } 

    public ExpandableStackLayout() 
    { 
     //Do somethings whith parameters but are null 
     usemyParameters(); 
    } 
+1

declaringTypeはtypeof(ExpandableStackLayout)でなければなりません。 –

+0

プロパティのタイプは?あなたはそれをどこに置くかという少しの例を私に見せることができますか? –

+0

確か: 'パブリック静的読み取り専用BindableProperty ImageProperty = BindableProperty.Create( propertyNameの: "イメージ"、 戻り値の:typeof演算(文字列)、 declaringType:typeof演算(ExpandableStackLayout)、 はdefaultValue:String.Emptyを);'。あなたはそれについてもっと読むことができます[ここ](https://developer.xamarin.com/guides/xamarin-forms/xaml/bindable-properties/) –

答えて

3

を。 TitleプロパティとImageプロパティを実装する方法を示します。

public class ExpandableStackLayout : StackLayout 
{ 
    public static readonly BindableProperty TitleProperty = 
     BindableProperty.Create<ExpandableStackLayout, string>(p => p.Title, string.Empty, BindingMode.TwoWay); 

    public string Title 
    { 
     get { return (string)base.GetValue(TitleProperty); } 
     set { base.SetValue(TitleProperty, value); } 
    } 

    public static readonly BindableProperty ImageProperty = 
     BindableProperty.Create<ExpandableStackLayout, string>(p => p.Image, string.Empty, BindingMode.TwoWay); 

    public string Image 
    { 
     get { return (string)base.GetValue(ImageProperty); } 
     set { base.SetValue(ImageProperty, value); } 
    } 
} 

私はあなたがビューで何を達成したいのか分かりません。まず、these controlsStacklayout implementationを勉強することをお勧めします。

+0

他のビューを内部に持つStackLayoutが必要です。私は特別な機能とビューでコントロールを作成したい。このビューには、stackLayoutやgridなどがあります。私は再利用可能なコントロールを作成したい。 –

+0

@MiguelAngelMuñoz私はそれを取得しません。 ExpandableStackLayout内のビューにコードを移動したい場合 –

3

変数を.xamlから.csにバインドする場合は、バインド可能なプロパティを使用する必要があります。

あなたは.csファイルには、カスタムビューの

[XamlCompilation(XamlCompilationOptions.Compile)] 
public partial class ExpandableStackLayout : StackLayout 
{ 
    public ExpandableStackLayout() 
    { 
     this.InitializeComponent(); 
    } 

    public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(ExpandableStackLayout), string.Empty); 

    public string Title 
    { 
     get 
     { 
      return (string)GetValue(TitleProperty); 
     } 
     set 
     { 
      SetValue(TitleProperty, value); 
     } 
    } 
} 
1

プロパティはコンストラクタを介して設定されることになっていない、次のようになります。カスタムビューのプロパティを実装する方法はBindable Propertiesです。

基本的には本格的なバインド可能なプロパティを作成するために2つのものを作成する必要があります。

  • public static BindableProperty
  • のAC#プロパティが明示的なゲッターとセッターにBindablePropertyを取得および設定をそれぞれ

例としてTitleプロパティを使用して説明します。まず第一に、あなたがBindableProperty

public static readonly BindableProperty TitleProperty 
    = BindableProperty.Create(nameof(Title), typeof(string), typeof(ExpandableStackLayout)); 

TitlePropertyを作成する第二の工程において

public string Title { get; set; } 

シンプルautoプロパティの作成(C#のプロパティをとBindablePropertyは相互に依存している)としてプロパティにアクセスするための命名規則ですXAMLのTitle

最後のステップは、これはTitlePropertyにアクセス簡素化Title

public string Title 
{ 
    get => (string)GetValue(TitleProperty); 
    set => SetValue(TitleProperty, value); 
} 

介しTitlePropertyの値にアクセスすることです。

関連する問題