1

I am working on a Xamarin project that includes a build for GTK. I am attempting to create a custom renderer for many of the Controls, but am having trouble finding, accessing and changing the properties for the control. For example, I would like to replace the "magnifying glass" icon for the SearchBar control with something more similar to the default icon on the Android platform.

I've created the custom renderer:

namespace MyProject.GTK.CustomRenderers
{
    public class CustomSearchBarRenderer : Xamarin.Forms.Platform.GTK.Renderers.SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            var searchBar = Control;
            // How do I replace the image?
        }
    }
}

but from there I am at a loss as there are practically no resources on custom renderers for GTK. I've tried looking at the GTK.Renderers.SearchBarRenderer to see if the class its derived from contains any useful properties or methods, as well as trying to find something meaningful in the GTK documentation and the repository for the Xamarin.Forms.GTK package, to no avail. I'm just not really sure how to understand the inner workings of the controls in this build so I can't figure out what I should even be looking for. Any pointers or resources for this or any GTK specific custom renderer work would be much appreciated.

Blake Simmons
  • 426
  • 1
  • 8
  • 23

1 Answers1

1

You can check Xamarin Forms GTK

SearchBar is implmented by the use of element called SearchEntry which uses ImageButton and the icon is set by below code

_searchButton.ImageWidget.Pixbuf = RenderIcon("gtk-find", IconSize.SmallToolbar, null); // Search icon

Refer

This should help you begin modifying, if you can get access to SearchEntry in your custom renderer you can change icon, otherwise you will have to create your own search bar, which takes lot of effort.

Morse
  • 8,258
  • 7
  • 39
  • 64
  • Alright I'll take a look at this and update ASAP. Thanks very much for trying to clarify that for me. – Blake Simmons Mar 25 '20 at 18:30
  • So I've been learning more about custom renderers and I can see where all of the properties are being set in SearchEntry, but it looks like SearchEntry has no method to access _searchButton.ImageWidget and the _searchButton is private. Am I right to think that I am unable to access this property in SearchEntry or is there potentially a way to get around that? – Blake Simmons Mar 26 '20 at 23:47
  • 1
    yeah. Thats why I said `if you can get access to SearchEntry in your custom renderer you can change icon, otherwise you will have to create your own search bar` , try posting a question on Github , someone in the GTK team might assist. I will try to take deeper look tomorrow. – Morse Mar 27 '20 at 01:52
  • I'll do that. I really appreciate the input. I've also started looking into creating custom controls in case I have to go that route. – Blake Simmons Mar 27 '20 at 02:34