2017-09-16 5 views
0

버튼에 2 개의 라벨 또는 2 개의 사진을 추가하고 싶습니다. 세로 옵션과 가로 옵션을 설정해야합니다. 어떻게해야합니까? 현재 iOS에서 작업하고 있습니다. 아이오스 렌더러 (itos renderer)에게 말해 주면 좋을 것 같습니다.xamarin 양식의 합성 단추는 어떻게합니까?

이렇게하면;

enter image description here

감사합니다 여러분.

답변

0

Custom Renderer을 통해 버튼을 맞춤 설정할 수 있습니다. 버튼 렌더러를 만들고 관련 BindableProperty을 추가하십시오. 그런 다음 Xaml에서 레이블 텍스트를 설정할 수 있습니다. 여기에 BindableProperty을 설정, PCL에서

  1. MyButton.cs :

    여기 예를 들어 내 코드의 아이폰 OS 플랫폼에서

    public class MyButton : Button 
    { 
        public static readonly BindableProperty LabelOneTextProperty = BindableProperty.Create("LabelOneText", typeof(string), typeof(MyButton), defaultBindingMode: BindingMode.TwoWay); 
        public static readonly BindableProperty LabelTwoTextProperty = BindableProperty.Create("LabelTwoText", typeof(string), typeof(MyButton), defaultBindingMode: BindingMode.TwoWay); 
    
    
        public string LabelOneText 
        { 
         get { return (string)GetValue(LabelOneTextProperty); } 
         set { SetValue(LabelOneTextProperty, value); } 
        } 
    
        public string LabelTwoText 
        { 
         get { return (string)GetValue(LabelTwoTextProperty); } 
         set { SetValue(LabelTwoTextProperty, value); } 
        } 
    } 
    
  2. MyButtonRenderer.cs, MyButton에에 레이블을 추가하고 프레임을 설정하십시오 :

    class MyButtonRenderer: ButtonRenderer 
    { 
    
        UILabel labelOne; 
        UILabel labelTwo; 
    
        public override void LayoutSubviews() 
        { 
         base.LayoutSubviews(); 
         nfloat height = Control.Frame.Size.Height; 
         nfloat width = Control.Frame.Size.Width; 
    
         if (labelOne!=null) 
          labelOne.Frame = new CGRect(0, 0, width, height/2); 
    
         if (labelTwo != null) 
          labelTwo.Frame = new CGRect(0, height/2, width, height/2); 
    
        } 
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e) 
        { 
         base.OnElementChanged(e); 
    
         if (Control != null) 
         { 
          var button = e.NewElement as MyButton; 
    
          labelOne = new UILabel(); 
          labelOne.Text = button.LabelOneText; 
          labelOne.TextAlignment = UITextAlignment.Center; 
    
          labelTwo = new UILabel(); 
          labelTwo.Text = button.LabelTwoText; 
          labelTwo.TextAlignment = UITextAlignment.Center; 
    
          this.AddSubview(labelOne); 
          this.AddSubview(labelTwo); 
         } 
        } 
    } 
    
  3. 부 텍스트 PCL에서에서 MainPage.xaml에서 tton의 레이블 :

    <ContentPage.Content> 
        <StackLayout> 
         <local:MyButton LabelOneText="OneText" LabelTwoText="TwoText" BackgroundColor="Red"/> 
        </StackLayout> 
    </ContentPage.Content> 
    

    은 다음과 같이 실행합니다

    enter image description here

은 또한, 사진을 추가, 당신은 단지 UIImageViews에 UILabels를 교체해야합니다.

+0

좋은 답변 주셔서 감사합니다. 그러나 나는 문제가있다. 가로 옵션과 세로 옵션을 설정할 수 없습니다. 버튼 효과는 보이지 않습니다. 버튼 클릭 이벤트가 작동하지만 효과가 작동하지 않습니다. 가로, 세로 옵션 및 단추의 가시성을 어떻게 설정할 수 있습니까? –

+0

@KadirAlan, "가로 옵션과 세로 옵션을 설정할 수 없습니다"- 관련 BindableProperty를 추가 할 수 있습니다. 프레임 또는 구속 조건으로 위치를 설정합니다. 내 코드는 레이블의 프레임을 단추의 가로 및 세로 중앙에 놓을 수 있도록 설정합니다. 사용자 정의 할 수 있습니다. 기본 단추 효과는 단추 제목입니다. 이 버튼에는 제목이 없습니다. –

+0

@ KadirAlan 또는 단추의 다른 상태에 대한 사용자 지정 배경 이미지를 설정할 수 있습니다. 'btn.SetBackgroundImage (aImage, UIControlState.Highlighted); 그리고'btn.SetBackgroundImage (bImage, UIControlState.Normal); ' –

0

격자를 만들고 모든 행과 열을 차지하는 단추를 추가 한 다음 원하는보기 (이 경우 두 개의 레이블)를 InputTransparent = true과 함께 추가 할 수 있습니다.

그게 도움이되기를 바랍니다.

관련 문제