Xamarin ToolbarItem图标未在Android中显示

时间:2017-04-05 10:14:43

标签: c# xaml xamarin xamarin.android

尝试在Xamarin中加载ToolbarItem的图标时遇到问题:它没有显示出来。工具栏肯定存在。我已经处理了Click方法并且它可以工作,但Icon不可见。 “.ico”文件存在于Android项目的所有“可绘制”文件夹中。它与我在另一个解决方案中使用的.ico文件相同,并且它可以正常工作作为ToolbarItem的图标,但在这里它不起作用。我试图加载嵌入的图像,但它也不起作用。如果我设置了Text属性,则会正确显示。我有一个MasterPage,这是一个MasterDetailPage,这个页面的细节是RootPage。在RootPage上我有ToolbarItem。这是我的xaml代码和背后的代码:

首页:

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:ITEC.Views;assembly=ITEC"
             x:Class="ITEC.Views.HomePage"
             IsPresented="True">

    <MasterDetailPage.Master>
        <ContentPage Title="Home" Padding="0,20,0,0">
            <StackLayout>
                <ListView x:Name="listView">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextCell Text="{Binding Title}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </ContentPage>
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <views:RootPage/>
    </MasterDetailPage.Detail>
</MasterDetailPage>

代码背后:

using ITEC.Services;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ITEC.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : MasterDetailPage
    {
        private OptionService _optionService;
        public HomePage()
        {
            InitializeComponent();
            Detail=new NavigationPage(new RootPage());
            _optionService = new OptionService();
            listView.ItemsSource = _optionService.GetOptions();
        }
    }
}

RootPage:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:embedded="clr-namespace:ITEC.MarkupExtensions"
             x:Class="ITEC.Views.RootPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Icon="settings.ico" Clicked="MenuItem_OnClicked"></ToolbarItem> 
    </ContentPage.ToolbarItems>
    <StackLayout>
        <Label>Hello world</Label>
    </StackLayout>
</ContentPage>

代码背后:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ITEC.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RootPage : ContentPage
    {
        public RootPage()
        {
            InitializeComponent();
        }

        private void MenuItem_OnClicked(object sender, EventArgs e)
        {
            DisplayAlert("Hello", "OK", "No");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试 png 文件作为您的图标。我认为这可能有用。

相关问题