【问题标题】:App doesn't build with two ToolbarItem应用程序不使用两个 ToolbarItem 构建
【发布时间】:2018-08-26 00:06:32
【问题描述】:

我正在尝试在 XAML 中创建一个页面,其中包含两个 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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="VFood.Views.EntregadorEdit"
             Title="{Binding Title}">

    <ContentPage.ToolbarItems>
        <OnPlatform x:TypeArguments="ToolbarItem">
            <OnPlatform.iOS>
                <ToolbarItem x:Name="SalvarItemIOS" Name="Done" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>
                <ToolbarItem x:Name="RemoveItemIOS" Name="Trash" Command="{Binding RemoveCommand}" Order="Primary" Priority="1"/>
            </OnPlatform.iOS>
            <OnPlatform.Android>
                <ToolbarItem x:Name="SalvarItemDroid" Icon="ic_check" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>
                <ToolbarItem x:Name="RemoveItemDroid" Icon="ic_delete" Command="{Binding RemoveCommand}" Order="Primary"  Priority="1"/>
            </OnPlatform.Android>
        </OnPlatform>
    </ContentPage.ToolbarItems>

    <ContentPage.Content>
        <StackLayout Spacing="8">
            <StackLayout.Padding>
                <OnPlatform x:TypeArguments="Thickness"  Android="16,16,16,16" iOS="10,10,10,10"/>
            </StackLayout.Padding>
            <Entry Placeholder="Nome" Text="{Binding Entregador.Nome}"/>
            <Entry Placeholder="Telefone" Text="{Binding Entregador.Telefone}" Keyboard="Telephone" />
        </StackLayout>
    </ContentPage.Content>

</ContentPage>

如果我为每个平台评论一个工具栏项,就像下面的代码一样,构建就像一个魅力,但如果我尝试使用两个工具栏项进行构建,我就会出错。

<?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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="VFood.Views.EntregadorEdit"
             Title="{Binding Title}">

    <ContentPage.ToolbarItems>
        <OnPlatform x:TypeArguments="ToolbarItem">
            <OnPlatform.iOS>
                <!--<ToolbarItem x:Name="SalvarItemIOS" Name="Done" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>-->
                <ToolbarItem x:Name="RemoveItemIOS" Name="Trash" Command="{Binding RemoveCommand}" Order="Primary" Priority="1"/>
            </OnPlatform.iOS>
            <OnPlatform.Android>
                <!--<ToolbarItem x:Name="SalvarItemDroid" Icon="ic_check" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>-->
                <ToolbarItem x:Name="RemoveItemDroid" Icon="ic_delete" Command="{Binding RemoveCommand}" Order="Primary"  Priority="1"/>
            </OnPlatform.Android>
        </OnPlatform>
    </ContentPage.ToolbarItems>

    <ContentPage.Content>
        <StackLayout Spacing="8">
            <StackLayout.Padding>
                <OnPlatform x:TypeArguments="Thickness"  Android="16,16,16,16" iOS="10,10,10,10"/>
            </StackLayout.Padding>
            <Entry Placeholder="Nome" Text="{Binding Entregador.Nome}"/>
            <Entry Placeholder="Telefone" Text="{Binding Entregador.Telefone}" Keyboard="Telephone" />
        </StackLayout>
    </ContentPage.Content>

</ContentPage>

【问题讨论】:

    标签: xaml xamarin xamarin.forms prism


    【解决方案1】:

    这样的事情应该可以工作(没有人说 XAML 不够冗长?)

    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="SalvarItemIOS" Command="{Binding SalvarCommand}" Order="Primary" Priority="0">
            <ToolbarItem.Name>
                <OnPlatform x:TypeArguments="x:String">
                    <On Platform="iOS" Value="Done"/>
                </OnPlatform>
            </ToolbarItem.Name>
            <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="Android" Value="ic_check"/>
                </OnPlatform>
            </ToolbarItem.Icon>
        </ToolbarItem>
        <ToolbarItem x:Name="RemoveItemIOS" Command="{Binding RemoveCommand}" Order="Primary" Priority="1">
            <ToolbarItem.Name>
                <OnPlatform x:TypeArguments="x:String">
                    <On Platform="iOS" Value="Trash"/>
                </OnPlatform>
            </ToolbarItem.Name>
            <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource">
                <On Platform="Android" Value="ic_delete"/>
            </OnPlatform>
            </ToolbarItem.Icon>
        </ToolbarItem>
    </ContentPage.ToolbarItems>
    

    【讨论】:

    • 谢谢你!像魅力一样工作,是的,多么冗长的语言哈哈哈
    • @ViníciusdaCruzMaia 很高兴它有帮助 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多