Creating a Custom Control by Inheriting from Button

njsokalskinjsokalski Member ✭✭✭

I am working on converting my apps from UWP to Xamarin. In my UWP apps, I had a custom control that inherited from Button. The control overrode and added several properties (including the Template property, which used my added properties using TemplateBinding). I have found information on how to inherit from ContentView in Xamarin, but I need the functionality & properties inherited from Button. How can I do something similar to create a custom Button in Xamarin? Thanks.

Answers

  • igorkr_10igorkr_10 Member ✭✭✭

    You can inherit from button and add your own properties. ContentView is using for more complex layouts f. e. if you need to add several views in your custom control.

  • njsokalskinjsokalski Member ✭✭✭

    Thank you for that information. However, as I mention in my original post, I override the Template property (of the Button). In my *.xaml file, the root is a Button (since that is what I am inheriting from). It may be easiest to show what I did by attaching the *.xaml & *.xaml.vb files (they are txt files here because it would not let me attach xaml/vb files). Is there anywhere that I can see an example of doing what you say (I realize that I will need to use C# instead of VB, that is not a problem)? Thanks.

  • igorkr_10igorkr_10 Member ✭✭✭

    @njsokalski said:
    Thank you for that information. However, as I mention in my original post, I override the Template property (of the Button). In my *.xaml file, the root is a Button (since that is what I am inheriting from). It may be easiest to show what I did by attaching the *.xaml & *.xaml.vb files (they are txt files here because it would not let me attach xaml/vb files). Is there anywhere that I can see an example of doing what you say (I realize that I will need to use C# instead of VB, that is not a problem)? Thanks.

    I can't get those files. It says: Error 1016 Origin DNS error

  • JoeHarvey_MSFTJoeHarvey_MSFT Member, Xamarin Team Xamurai

    If you have already created a template for the UWP button then you need to do 2 things. 1 add that template to the UWP resource's 2) apply this "style" to the button by using a custom Renderer.

    in this example the App.xaml for the UWP project contains several of my Custom Templates

    Then for example i create a CustomTabbedNavigationRenderer to apply this custom template to a style

    if you would like additional help, please submit a ticket to Microsoft Support and we will be happy to assets you.
    https://support.microsoft.com/en-us/supportforbusiness/productselection?sapId=211dd84f-3474-c3c5-79bf-66db630c92a6

  • njsokalskinjsokalski Member ✭✭✭

    I think you still misunderstand my question. My Xamarin project does not include a UWP project, I am converting a UWP project TO a Xamarin project. And from my understanding, the Xamarin.Forms Button does not have a Template property like controls did in UWP. I think I am still confused about what to do with what, and where to put the different parts.

Sign In or Register to comment.