0

In Xamarin.Forms, typically I'd write a custom renderer for buttons using a class name like "ButtonExt". Is it possible to write a custom renderer using the original name of (I guess overriding) the element, like "Button"?

Basically, I want to be able to add horizontal padding to all the buttons in my app, but without having to rename all Buttons to ButtonExt (there is no Padding property for Buttons in Xamarin.Forms). In other words I want to be able say new Button { Padding = 20 } and not have to say new ButtonExt { Padding = 20 } (project is C#, not XAML).

Normally, in the iOS project, I'd have:

[assembly: ExportRenderer(typeof(ButtonExt), typeof(ButtonExtRenderer))]
namespace StoreFulfillment.iOS
{
    public class ButtonExtRenderer : ButtonRenderer

and in the shared project, I'd have:

using MySharedProject;
using Xamarin.Forms;

namespace MySharedProject.Renderers
{
    public class ButtonExt : Button
    {
        ...

And then I'd say new ButtonExt { Padding = 20 }. But in order to be able to write new Button { Padding = 20 }, I can't write public class Button : Button...so how do I accomplish this?

jbyrd
  • 5,287
  • 7
  • 52
  • 86
  • @hvaughan3 - just expanded my question - can you address the update question? – jbyrd Jul 28 '17 at 16:46
  • Wondering if Extension Methods is the answer - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods – jbyrd Jul 28 '17 at 16:47

1 Answers1

2

Sure! Just register it to a normal Button type and make sure you call all the base methods of the ones you override so the original working also remains.

So your attribute to register the renderer would just look like this:

[assembly: ExportRenderer(typeof(Button), typeof(YourButtonRenderer))]

Also, make sure you actually need a custom renderer. If you just want to have a simple styling issue, look at the Xamarin.Forms styling possibilities and especially implicit styling.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • Thanks for the quick response - will try. And yes I know all about styling, and how Buttons do *not* have a padding property :) – jbyrd Jul 28 '17 at 15:35
  • But in the Xamarin shared project, how would I create the class that I want to add custom properties to (i.e. Padding)? Using ButtonExt, I declare the class like `public class ButtonExt : Button {...` -- I can't do `public class Button : Button`...? – jbyrd Jul 28 '17 at 16:24
  • 1
    @jbyrd You cannot add custom properties to a class that has already been created unless that class is a `partial` class, which `Button` is not. So that leaves you with the option of, like you said, create extension methods or creating [Attached Properties](https://developer.xamarin.com/guides/xamarin-forms/xaml/attached-properties/) which is what the `Grid.Row` property is (meaning the `Button` class does not actually have a `Button.Grid.Row` property, but it can still be used on it). I do not think I would use attached properties for what you are trying to do, but it is an option nonetheless. – hvaughan3 Jul 28 '17 at 17:46