You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm following a tutorial here which shows how to customize a control throughout the entire app as well as specific instances.
The control is an Entry which, as per the documentation:
In this example, the Entry customization occurs in a page class. Therefore, all Entry controls on Android, iOS, and Windows will be customized once an instance of the CustomizeEntryPage is created.
This is kinda basic but what's the best way to customize without having to load up an instance of a specific content page? Can I load it at startup by putting a custom class in the App.xaml.cs or something?
I previously built what I wanted a while ago using an alternate method than the one explained on the documentation page linked above and I'm not sure if it was the right approach, just something hacked together from a Stack Overflow post I read.
The actual content of the class doesn't really matter but just for clarity, to change the colour of the search icon and search plate in a search bar control on Android I've inherited from the existing SearchBarHandler then extracted some colours from existing properties of the default control to match the design I want.
public partial class SearchBarBureauHandler : SearchBarHandler
{
public static readonly IPropertyMapper<ISearchBar, SearchBarHandler> CustomSearchMapper =
new PropertyMapper<ISearchBar, SearchBarHandler>(Mapper)
{
["SearchIconColor"] = MapIconColor,
["SearchUnderlineColor"] = MapUnderlineColor,
};
public SearchBarBureauHandler() : base(CustomSearchMapper, CommandMapper)
{
//Microsoft.Maui.Handlers.SearchBarHandler.Mapper.AppendToMapping("SearchIconColor", (handler, view) =>
//{
//});
}
public override void UpdateValue(string propertyName)
{
base.UpdateValue(propertyName);
if (propertyName == SearchBar.PlaceholderColorProperty.PropertyName)
{
SetIconColor(GetPlaceholderColor());
}
if (propertyName == SearchBar.TextColorProperty.PropertyName)
{
SetSearchPlateColor(GetSearchBackgroundColor());
}
}
public void SetIconColor(AColor value)
{
var searchIcon = (ImageView)PlatformView.FindViewById(Resource.Id.search_mag_icon);
searchIcon.SetColorFilter(value, PorterDuff.Mode.SrcAtop);
}
public void SetSearchPlateColor(AColor value)
{
var searchPlate = PlatformView.FindViewById(Resource.Id.search_plate);
searchPlate.SetBackgroundColor(value);
}
public AColor GetSearchBackgroundColor()
{
AColor searchBackgroundColor = VirtualView.Background.ToColor().ToAndroid();
Debug.WriteLine(searchBackgroundColor.ToString());
return searchBackgroundColor;
}
public AColor GetPlaceholderColor()
{
AColor searchPlaceholderColor = VirtualView.PlaceholderColor.ToAndroid();
return searchPlaceholderColor;
}
public static void MapIconColor(ISearchBarHandler handler, ISearchBar searchBar)
{
if (handler is SearchBarBureauHandler customHandler)
{
customHandler.SetIconColor(customHandler.GetPlaceholderColor());
}
}
public static void MapUnderlineColor(ISearchBarHandler handler, ISearchBar searchBar)
{
if (handler is SearchBarBureauHandler customHandler)
{
customHandler.SetSearchPlateColor(customHandler.GetSearchBackgroundColor());
}
}
}
This seems to work and that's great but it's different from the way explained on the doc site so I wasn't sure if it's efficient or the best way to have like a universally customized control that looks the same throughout the entire app.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, I'm following a tutorial here which shows how to customize a control throughout the entire app as well as specific instances.
The control is an Entry which, as per the documentation:
This is kinda basic but what's the best way to customize without having to load up an instance of a specific content page? Can I load it at startup by putting a custom class in the App.xaml.cs or something?
I previously built what I wanted a while ago using an alternate method than the one explained on the documentation page linked above and I'm not sure if it was the right approach, just something hacked together from a Stack Overflow post I read.
The actual content of the class doesn't really matter but just for clarity, to change the colour of the search icon and search plate in a search bar control on Android I've inherited from the existing
SearchBarHandler
then extracted some colours from existing properties of the default control to match the design I want.Then in my MainProgram.cs I've got
This seems to work and that's great but it's different from the way explained on the doc site so I wasn't sure if it's efficient or the best way to have like a universally customized control that looks the same throughout the entire app.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions