Hello,
System.InvalidCastException: 'Specified cast is not valid.'
This error is usually caused when the wrong type conversion is used.
This is an expected error, SearchBar should use SearchBarHandler, not EntryHandler.
Please refer to the following code sample and documentation:
namespace MauiApp12.Platforms.Droid
{
public class CustomerSearchBarHandler:SearchBarHandler
{
protected override void ConnectHandler(SearchView platformView)
{
// this code sample shows how to remove the underline of searchbar.
base.ConnectHandler(platformView);
Android.Widget.LinearLayout linearLayout = platformView.GetChildAt(0) as Android.Widget.LinearLayout;
linearLayout = linearLayout.GetChildAt(2) as Android.Widget.LinearLayout;
linearLayout = linearLayout.GetChildAt(1) as Android.Widget.LinearLayout;
linearLayout.Background = null;
}
}
}
//register handler
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler(typeof(SearchBar), typeof(CustomerSearchBarHandler));
#endif
})
You could see the view handlers in View handlers.
Update:
You could refer to the following code to remove the magnifying glass and clear button in SearchBar.
protected override void ConnectHandler(SearchView platformView)
{
base.ConnectHandler(platformView);
var search = platformView as SearchView;
ImageView searchViewIcon = (ImageView)search.FindViewById<ImageView>(Resource.Id.search_mag_icon);
searchViewIcon.SetImageDrawable(null);
ImageView closeViewIcon = (ImageView)search.FindViewById(Resource.Id.search_close_btn);
closeViewIcon.SetImageDrawable(null);
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.