0

PCL Xamarin Forms Project를 사용하여 크로스 플랫폼 애플리케이션을 개발 중입니다. 내 응용 프로그램은 ios, android, windows 10 및 windows 8.1 데스크탑에서 실행됩니다.사용자 정의 렌더러 : 둥근 버튼 용 사용자 정의 렌더러

내가 있었어 .-

  1. 테두리 색상
  2. 테두리 폭
  3. 국경 뒤에 XAML 페이지에서 코드의 특성을 다음과 지정할 수있는 자 마린 형태의 버튼 제어를위한 사용자 정의 렌더러를 만들고 싶어 반경
  4. 배경 색상
  5. 텍스트 색상
  6. 텍스트 글꼴 크기, 색상은 굵은처럼 때문이다. 나는 자 마린 형태의하지만 국경 반경 일반 단추 컨트롤을 시도

버튼의

  • 높이 및 너비는 안드로이드와 윈도우 (10) 에서 호버 버튼 색상 변경에서 작동하지 않습니다 그래서 달성 할 수있는 방법 이? 안드로이드에서

  • +0

    질문은 무엇입니까? – Jason

    +0

    @ Jason 제 질문은 아직도 당신에게 명확하지 않습니까? – Sonali

    +0

    "필요합니다 ..."는 질문이 아닙니다. – Jason

    답변

    1

    AppCompat 만 작업 RadiusProperty 정지 자사의 known issue

    APPCOMPAT

    당신이 버튼에서 상속해야하는 동시에 일반 버튼을 APPCOMPAT을 사용하고 CustomRenderer을 등록 할 경우

    . 당신은 당신이 두 가지

    을 수행해야합니다 APPCOMPAT을 제거하려면 APPCOMPAT

    없이

    [assembly: ExportRenderer(typeof(RoundButton), typeof(RoundButtonRenderer))] 
    namespace Project.Droid.Renderers 
    { 
        public class RoundButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer 
        { 
         ButtonDrawable _backgroundDrawable; 
         Drawable _defaultDrawable; 
         bool _drawableEnabled; 
    
         protected override void Dispose(bool disposing) 
         { 
          if (disposing) 
          { 
           if (_backgroundDrawable != null) 
           { 
            _backgroundDrawable.Dispose(); 
            _backgroundDrawable = null; 
           } 
          } 
    
          base.Dispose(disposing); 
         } 
    
    
         protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) 
         { 
          base.OnElementChanged(e); 
    
          if (e.OldElement != null && _drawableEnabled) 
          { 
           _drawableEnabled = false; 
           _backgroundDrawable.Reset(); 
           _backgroundDrawable = null; 
          } 
          UpdateDrawable(); 
         } 
    
    
    
         protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
         { 
          if (_drawableEnabled && 
           (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || e.PropertyName == Xamarin.Forms.Button.BorderColorProperty.PropertyName || e.PropertyName == Xamarin.Forms.Button.BorderRadiusProperty.PropertyName || 
           e.PropertyName == Xamarin.Forms.Button.BorderWidthProperty.PropertyName)) 
          { 
           _backgroundDrawable.Reset(); 
           Control.Invalidate(); 
          } 
    
          base.OnElementPropertyChanged(sender, e); 
         } 
    
         private void UpdateDrawable() 
         { 
          if (Element.BackgroundColor == Color.Default) 
          { 
           if (!_drawableEnabled) 
            return; 
    
           if (_defaultDrawable != null) 
            Control.SetBackground(_defaultDrawable); 
    
           _drawableEnabled = false; 
          } 
          else 
          { 
           if (_backgroundDrawable == null) 
            _backgroundDrawable = new ButtonDrawable(); 
    
           _backgroundDrawable.Button = Element; 
    
           if (_drawableEnabled) 
            return; 
    
           if (_defaultDrawable == null) 
            _defaultDrawable = Control.Background; 
    
           Control.SetBackground(_backgroundDrawable.GetDrawable()); 
           _drawableEnabled = true; 
          } 
    
          Control.Invalidate(); 
         } 
        } 
    
    
        public class ButtonDrawable : IDisposable 
        { 
         object _backgroundDrawable; 
    
         PropertyInfo ButtonProperty; 
         public Xamarin.Forms.Button Button 
         { 
          get 
          { 
           return (Xamarin.Forms.Button)ButtonProperty.GetMethod.Invoke(_backgroundDrawable, null); 
          } 
          set 
          { 
           ButtonProperty.SetMethod.Invoke(_backgroundDrawable, new object[] { value }); 
          } 
         } 
    
         public ButtonDrawable() 
         { 
          _backgroundDrawable = typeof(Xamarin.Forms.Platform.Android.ButtonRenderer).Assembly.CreateInstance("Xamarin.Forms.Platform.Android.ButtonDrawable"); 
          this.ResetMethod = _backgroundDrawable.GetType().GetMethod("Reset", BindingFlags.Instance | BindingFlags.Public); 
          this.DisposeMethod = _backgroundDrawable.GetType().GetMethod("Dispose", BindingFlags.Instance | BindingFlags.Public); 
          this.ButtonProperty = _backgroundDrawable.GetType().GetProperty("Button", BindingFlags.Instance | BindingFlags.Public); 
         } 
    
         MethodInfo ResetMethod; 
         public void Reset() 
         { 
          ResetMethod.Invoke(_backgroundDrawable, null); 
         } 
    
         MethodInfo DisposeMethod; 
         public void Dispose() 
         { 
          DisposeMethod.Invoke(_backgroundDrawable, null); 
         } 
    
         public Android.Graphics.Drawables.Drawable GetDrawable() 
         { 
          return _backgroundDrawable as Android.Graphics.Drawables.Drawable; 
         } 
        } 
    } 
    

    당신의 MainActivity 비 APPCOMPAT 스타일에서 상속해야합니다 global::Xamarin.Forms.Platform.Android.FormsApplicationActivity 보통 resources/values/styles.xml에서 당신의 스타일에서 지금 상속해야합니다 like android:Theme.Material

    <resources> 
        <!-- inherit from the material theme --> 
        <style name="AppTheme" parent="android:Theme.Material"> 
        <!-- Main theme colors --> 
        <!-- your app branding color for the app bar --> 
        <item name="android:colorPrimary">@color/primary</item> 
        <!-- darker variant for the status bar and contextual app bars --> 
        <item name="android:colorPrimaryDark">@color/primary_dark</item> 
        <!-- theme UI controls like checkboxes and text fields --> 
        <item name="android:colorAccent">@color/accent</item> 
        </style> 
    </resources> 
    

    +0

    국경 반경은 IOS에서 작동합니까? 나는 아직 그것을 시도하지 않았다. – Sonali

    +1

    절대로 문제가 없었지만 어떤 Windows 플랫폼에서도 시도한 havent – Patrick

    +0

    Windows 10 및 8.1에서 일반 단추 컨트롤을 사용해 보았습니다. – Sonali

    1

    내 애플 리케이션 및 나를 위해 속성을 잘 작동합니다. "스타일"과 함께이 속성을 사용하고 있습니다.

    예 :

    <Style x:Key="buttonStyle" TargetType="Button"> 
        <Setter Property="BackgroundColor" Value="{DynamicResource Snow}"/> 
        <Setter Property="TextColor" Value="{DynamicResource LightBlue}" /> 
        <Setter Property="BorderColor" Value="{DynamicResource LightBlue}"/> 
        <Setter Property="BorderRadius" Value="15"/> 
        <Setter Property="BorderWidth" Value="1"/> 
        <Setter Property="FontAttributes" Value="Bold" /> 
        </Style> 
    

    내 버튼 :

    <Button Text="Login" Command="{Binding LoginCommand}" Style="{DynamicResource buttonStyle}" /> 
    

    Xamarin Styles

    +0

    나는 그것을 시도했지만 안드로이드에서 작동하지 않습니다. 경계선이없고 둥근 모서리가없는 버튼이 제공됩니다 – Sonali

    +0

    프로젝트를 지우고 다시 컴파일하십시오. 어쩌면 파일에있는 쓰레기 일뿐입니다. 저는 현재 프로젝트에서 이것을 사용하고 있으며 문제는 아닙니다. – BrianSouza

    +0

    오류가 발생했습니다. 이름 공간 접두사 'x'가 정의되지 않았습니다. –