0

PCL Xamarinフォームプロジェクトを使用してクロスプラットフォームアプリケーションを開発しています。私のアプリは、iOS、アンドロイド、Windows 10、Windows 8.1のデスクトップで動作します。カスタムレンダラー:丸いボタン用のカスタムレンダラー

私は、XAMLページから国境

  • ボーダー幅
  • also-

    1. 境界線の色
    2. 背後にあるコードから次のプロパティを指定できるxamarinフォームにボタンコントロールのカスタムレンダラを作りたいです半径
    3. 背景色
    4. テキストの色
    5. テキストのフォントサイズ、色、太字などの属性。
    6. 私はxamarin形の通常のボタンコントロールを試してみましたが、その境界線の半径でアンドロイドでは動作しませんし、Windows 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プラットフォームで試してみることはできません – Patrick

    +0

    Windows 10と8.1で通常のボタンコントロールを試しました。 – Sonali

    1

    私はこれらを使用しています私のアプリと私のためのプロパティは正常に動作します。 "Styles"でこれらのプロパティを使用しています。

    例:

    <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'が定義されていません –

    関連する問題