I had to add a custom renderer to my .Net MAUI app (iOS). The app crashes right after the splash screen, and the only error I can see is
2022-09-29 14:41:32.896 Xamarin.PreBuilt.iOS[41370:12914225] Could not resolve assembly Microsoft.VisualStudio.DesignTools.TapContract, Version=17.0.0.0, Culture=neutral, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/114BDA8C-ED16-4E18-B706-8D492B7703EB/Documents/My_MobileApp.content/Microsoft.VisualStudio.DesignTools.TapContract.dll' or one of its dependencies.
Here is my code in MauiProgram.cs:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCompatibility()
.ConfigureMauiHandlers((handlers) => {
#if ANDROID
handlers.AddHandler(typeof(Shell), typeof(Platforms.Android.Renderers.MyShellRenderer));
#elif IOS
handlers.AddHandler(typeof(Shell), typeof(Platforms.iOS.Renderers.MyShellRenderer));
#endif
})...
The app works on Android. It works on iOS if I remove this #elif IOS
part, but of course then the custom renderer does not do its job.
Here is the renderer itself:
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform.Compatibility;
using UIKit;
namespace My_MobileApp.Platforms.iOS.Renderers
{
internal class MyShellRenderer : ShellRenderer
{
protected override IShellItemRenderer CreateShellItemRenderer(ShellItem item)
{
var renderer = base.CreateShellItemRenderer(item);
if (renderer != null)
{
if (renderer is ShellItemRenderer shellItem)
{
var items = shellItem.TabBar.Items;
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null) continue;
else
{
UITabBarItem item_temp = items[i] as UITabBarItem;
UIView view = item_temp.ValueForKey(new Foundation.NSString("view")) as UIView;
UILabel label = view.Subviews[0] as UILabel;
label.Lines = 2;
label.LineBreakMode = UILineBreakMode.WordWrap;
label.TextAlignment = UITextAlignment.Center;
}
}
}
}
return renderer;
}
}
}