【发布时间】:2023-02-03 07:50:07
【问题描述】:
我必须向我的 .Net MAUI 应用程序 (iOS) 添加自定义渲染器。该应用程序在启动画面后立即崩溃,我能看到的唯一错误是
2022-09-29 14:41:32.896 Xamarin.PreBuilt.iOS[41370:12914225] 无法解析程序集 Microsoft.VisualStudio.DesignTools.TapContract,Version=17.0.0.0,Culture=neutral,PublicKeyToken=null。详细信息:无法加载文件或程序集“/var/mobile/Containers/Data/Application/114BDA8C-ED16-4E18-B706-8D492B7703EB/Documents/My_MobileApp.content/Microsoft.VisualStudio.DesignTools.TapContract.dll”或其之一依赖项。
这是我的代码Maui程序.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 })...该应用程序适用于 Android。如果我删除这个
#elif IOS部分,它可以在 iOS 上运行,但是自定义渲染器当然不会完成它的工作。这是渲染器本身:
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; } } }
【问题讨论】:
-
您确定输出日志中没有其他相关错误吗?没有可以帮助排除故障的额外信息?堆栈跟踪 ?调用堆栈?
-
@Cfun 没有堆栈。
-
您是否尝试通过在代码中放置断点和步进来进行调试?
-
@Cfun 好的,我发现了一些东西。异常是由行标签引起的。Lines = 2;因为标签为空。我想知道如何解决这个问题...此代码用于允许 2 行 TabBar 选项卡标题。
-
view.Subviews[1] 中有什么元素?
标签: c# ios .net-maui custom-renderer