2017-11-29 4 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 형식 : "이미지", returnType이 : 대해서 typeof (문자열), declaringType : 대해서 typeof (ExpandableStackLayout), DEFAULTVALUE : String.Empty로)'. [here] (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 또는 그리드 등일 수 있습니다. 재사용 가능한 컨트롤을 만들고 싶습니다. –

+0

@ MiguelAngelMuñoz 나는 그것을 얻지 않는다. ExpandableStackLayout 안에 View를 가지고 싶다면 거기에 코드를 옮기십시오. –

3

.xaml에서 .cs 코드로 바인딩 변수의 경우 Bindable Properties를 사용해야합니다.

귀하의 .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입니다. 점점 각각 명시 적 getter 및 setter에 을 BindableProperty 설정

  • public static BindableProperty
  • AC 번호 속성 :

    은 기본적으로 당신이 제몫을 바인딩 속성을 만드는 두 가지를 만들어야합니다

    예를 들어 Title 속성을 사용하여 설명하겠습니다. 당신이 BindableProperty

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

    TitleProperty 만드는 두 번째 단계에서는

    public string Title { get; set; } 
    

    우선은 (는 C# 속성과 BindableProperty이 상호 의존적) 간단한 자동 속성을 만들기로 속성에 액세스 할 수있는 명명 규칙입니다 XAML의 Title

    마지막 단계는이 TitleProperty 액세스 단순화 Title

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

    통해 TitleProperty의 값에 접근한다.