ToolbarItem图标停止工作

我的应用程序有一个ToolbarItem定义了一个像这样的图标:

<ContentPage.ToolbarItems>
    <ToolbarItem Name="Refresh" Order="Primary" Text="Refresh" Icon="Refresh.png" Priority="0" Command="{Binding RefreshCommand}" />
</ContentPage.ToolbarItems>

该图像位于Resources / drawable / Refresh.png中,这个XAML在Xamarin Forms 2.3.4.247中运行良好 .

突然,在同一版本2.3.4.247上,同一个XAML失败并出现空引用异常:

{System.NullReferenceException:对象引用未设置为对象的实例 . at Xamarin.Forms.Platform.Android.ResourceManager.GetId(System.Type type,System.String propertyName)[0x0000d]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ ResourceManager.cs:76 at Xamarin.Forms.Platform.Android.ResourceManager.IdFromTitle(System.String title,System.Type type)[0x00007]在Xamarin的C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ ResourceManager.cs:70 C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ ResourceManager.cs中的.Forms.Platform.Android.ResourceManager.GetDrawable(Android.Content.Res.Resources资源,System.String名称)[0x00000]: 42位于Xamarin.Forms的C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ AppCompat \ NavigationPageRenderer.cs:747中的Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.UpdateMenu()[0x000f7] . Platform.Android.AppCompat.NavigationPageRenderer.ToolbarTrackerOnCollectionChanged(System.Object sender,System.EventArgs eventArgs)[0x00000]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ AppCompat \ NavigationPageRenderer.cs:715 at Xamarin.Forms.Internals.ToolbarTracker.EmitCollectionChanged()[0x00008] in C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Internals \ ToolbarTracker.cs:60位于C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Core中的Xamarin.Forms.Internals.ToolbarTracker.set_Target(Xamarin.Forms.Page value)[0x00031] Internals \ ToolbarTracker.cs:37 at C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin .Forms.Platform.Android \ AppCompat \ NavigationPageRenderer.cs:258,位于C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms中的Xamarin.Forms.Platform.Android.VisualElementRenderer1 [TElement] .SetElement(TElement element)[0x000f4] . Platform.Android \ VisualElementRenderer.cs:196 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1 [T元素] .Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement(Xamarin.Forms.VisualElement元素)[0x00027]在Xamarin的C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:137 Xamarin.Forms.Platform中的.Forms.Platform.Android.Platform.CreateRenderer(Xamarin.Forms.VisualElement元素)[0x0001f]位于C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Platform.cs:289 .Android.AppCompat.Platform.AddChild(Xamarin.Forms.Page页面,System.Boolean布局)[0x00015]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ AppCompat \ Platform.cs:250 at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage(Xamarin.Forms.Page newRoot)[0x00089]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ AppCompat \ Platform.cs:239 at C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platfor中的Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage(Xamarin.Forms.Page页面)[0x0001a]在M:C:\ BuildAgent3 \ work \ ca3766cfc22 .Forms.Platform.Android \ AppCompat \ FormsAppCompatActivity.cs:321位于C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Core \ BindableObject.cs中的Xamarin.Forms.BindableObject.OnPropertyChanged(System.String propertyName)[0x0000a] :136 at Xamarin.Forms.OnPropertyChanged(System.String propertyName)[0x00000]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Element.cs:380 at Xamarin.Forms.Application.set_MainPage(Xamarin) .Forms.Page值)[0x0008b]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Application.cs:90 at Intertain.Mobile.Custom.CustomMvxFormsPagePresenter.TryShowPage [T](MvvmCross.Core.ViewModels . MvxViewModelRequest请求)[0x0005c]在D:\ Visual Studio \ My Projects \ Intertain \ Mobile \ Intertai中n.Mobile \ Intertain.Mobile \ Custom \ CustomMvxFormsPagePresenter.cs:76}

研究问题我发现我应该使用小写名称作为资源,我试图修复,但无论我做什么,我仍然会得到同样的错误 .

我尝试将XAML更改为OnPlatform:

<ContentPage.ToolbarItems>
    <ToolbarItem Name="Refresh" Order="Primary" Text="Refresh" Priority="0" Command="{Binding RefreshCommand}">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource" Android="refresh.png" WinPhone="refresh.png" />
        </ToolbarItem.Icon>
    </ToolbarItem>
</ContentPage.ToolbarItems>

尝试使用Icon值作为“Refresh.png”,“refresh.png”,“刷新”和“刷新”,使用drawable文件夹中名为Refresh.png或refresh.png的图像会给出同样的错误 .

如果我在android上将Icon属性设置为空白:

<ContentPage.ToolbarItems>
    <ToolbarItem Name="Refresh" Order="Primary" Text="Refresh" Priority="0" Command="{Binding RefreshCommand}">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource" Android="" WinPhone="refresh.png" />
        </ToolbarItem.Icon>
    </ToolbarItem>
</ContentPage.ToolbarItems>

页面加载很好,没有问题,我看到文本“刷新”,当然没有图标 .

所以我确信问题在于它没有找到图像,但我不能为我的生活弄清楚如何让它找到它 .

图像被设置为AndroidResource,但我确实尝试了EmbeddedResource和None ...

我到底做错了什么?

编辑:史蒂夫鼓励我尝试使用转换器绑定到资源,所以我掀起了这个:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var source= ImageSource.FromFile(parameter.ToString());
    return source;
}

我传入一个虚拟绑定和参数来创建源:

<ToolbarItem Name="Refresh" Order="Primary" Text="Refresh" Icon="{Binding IsLoading, Converter={StaticResource IconSourceConverter}, ConverterParameter=refresh.png}" Priority="0" Command="{Binding RefreshCommand}" />

它被实例化(我从转换器返回的源值没有得到空引用错误)

但是在我退回之后,我得到了同样的愚蠢错误 .

我正式放弃,我只是要留下文字而不是android上的图标,因为有一个糟糕的用户界面比有一个破坏的用户界面更好,并且将它粉碎到xamarin形式仍然很糟糕,我需要继续生活 .

如果你有任何想法,我会再次尝试游戏,但我正在通过黑客攻击 .

回答(2)

2 years ago

您是否尝试过从代码中添加项目?

var searchToolbarItem = new ToolbarItem { Icon = AssetNames.SearchIcon };
ToolbarItems.Add(searchToolbarItem);
searchToolbarItem.SetBinding(MenuItem.CommandProperty, nameof(BookPageViewModel.OpenAppointmentsSearchCommand));

AssetNames.SearchIcon 的位置是:

public const string SearchIcon = "ic_search_white.png";

我还注意到你将Xamarin.Forms更新为1.2?该套餐版本从2014年7月开始 .

如果是这种情况,请尝试使用: Xamarin.Forms 2.3.4.247

Also delete the bin/obj folders and rebuild the project!

2 years ago

我是这样做的:

视图

<ContentPage.ToolbarItems>
    <ToolbarItem Icon="{Binding AddIcon}" Command="{Binding AddCommand}" />
</ContentPage.ToolbarItems>

查看模型

public ImageSource AddIcon => "ic_add.png";

该图标位于Resources \ drawable \ ic_add.png中,并具有AndroidResource的构建操作 .

确保视图模型中的文件名和名称均为小写 .