ToolbarItem未显示

时间:2018-11-23 10:10:30

标签: xaml xamarin xamarin.forms toolbar toolbaritems

我目前正在阅读低谷this book

在本书的开头,我已经遇到了问题。我有Primary ToolbarItems和Secondary ToolbarItems。但是都没有显示出来。我在做什么错了?

<?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:local="clr-namespace:BookCode" 
             x:Class="BookCode.MainPage"
             Title="Visuals">


    <StackLayout Padding="10,0">
        <Label Text="Hello, Xamarin.Forms!"
               FontSize="Large"
               VerticalOptions="CenterAndExpand"
               HorizontalOptions="Center"/>

        <Button Text="Click Me!"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"/>

        <Switch VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"/>
    </StackLayout>

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="edit" Order="Primary">
            <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="FileImageSource"
                            iOS="edit.png"
                            Android="ic_action_edit.png"
                            WinPhone="Images/edit.png"/>

            </ToolbarItem.Icon>
        </ToolbarItem>
        <ToolbarItem Text="search" Order="Primary">
            <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="FileImageSource"
                            iOS="search.png"
                            Android="ic_action_search.png"
                            WinPhone="Images/feature.search.png"/>
            </ToolbarItem.Icon>
        </ToolbarItem>

        <ToolbarItem Text="refresh" Order="Primary">
            <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="FileImageSource"
                            iOS="reload.png"
                            Android="ic_action_refresh.png"
                            WinPhone="Images/refresh.png"/>
            </ToolbarItem.Icon>
        </ToolbarItem>

        <ToolbarItem Text="explore" Order="Secondary"/>
        <ToolbarItem Text="discover" Order="Secondary"/>
        <ToolbarItem Text="evolve" Order="Secondary" />
    </ContentPage.ToolbarItems>


</ContentPage>

2 个答案:

答案 0 :(得分:1)

你在这里。

<ContentPage.ToolbarItems>
    <ToolbarItem Text="edit" Order="Primary">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource"
                        Android="icon.png"/>

        </ToolbarItem.Icon>
    </ToolbarItem>
    <ToolbarItem Text="search" Order="Primary">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource"
                       Android="icon.png"/>
        </ToolbarItem.Icon>
    </ToolbarItem>

    <ToolbarItem Text="refresh" Order="Primary">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource"
                        Android="icon.png"/>
        </ToolbarItem.Icon>
    </ToolbarItem>

    <ToolbarItem Text="explore" Order="Secondary"/>
    <ToolbarItem Text="discover" Order="Secondary"/>
    <ToolbarItem Text="evolve" Order="Secondary" />
</ContentPage.ToolbarItems>


<ContentPage.Content>
    <StackLayout Padding="10,0">
        <Label Text="Hello, Xamarin.Forms!"
           FontSize="Large"
           VerticalOptions="CenterAndExpand"
           HorizontalOptions="Center"/>

        <Button Text="Click Me!"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"/>

        <Switch VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"/>
    </StackLayout>
</ContentPage.Content>

Toolbar

答案 1 :(得分:1)

ToolbarItems需要一个导航页面来显示它们。因此,修改导航页面包裹的MainPage:

MainPage = new NavigationPage(new MainPage());