使用MasterDetailPage或ToolbarItem会在Xamarin.Forms上引发异常

时间:2016-10-23 18:19:00

标签: xaml exception windows-runtime windows-phone-8.1 xamarin.forms

我在Xamarin.Forms可移植库XAML项目中有以下两段XAML代码:

repaint()

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestApp.Views.TestView">
  <ContentPage.ToolbarItems>
    <ToolbarItem Text="{Binding ToolbarItemText}" Command="{Binding ToolbarItemCommand}" Order="Default" Priority="0"/>
  </ContentPage.ToolbarItems>
  <ContentPage.Content>
  </ContentPage.Content>
</ContentPage>

运行Windows Phone 8.1项目(WinRT,而不是Silverlight)时,我有以下例外:

  

E_UNKNOWN_ERROR

     

错误HRESULT E_FAIL已从调用COM组件返回。

1 个答案:

答案 0 :(得分:0)

问题在于,必须 MasterDetailPage.Master 页面和 ToolbarIcon <指定图标路径 / strong>即可。此外,对于 MasterDetailPage.Master 页面,您必须指定标题属性。

以下是更正的代码示例:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestApp.Views.Test2View">
  <MasterDetailPage.Master>
    <ContentPage Title="Currencies">

    </ContentPage>
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <ContentPage>
        </ContentPage>
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestApp.Views.TestView">
  <ContentPage.ToolbarItems>
    <ToolbarItem Text="{Binding ToolbarItemText}" Command="{Binding ToolbarItemCommand}" Order="Default" Priority="0">
      <ToolbarItem.Icon>
        <OnPlatform x:TypeArguments="FileImageSource"
               Android="my-icon"
               WinPhone="Assets/my-icon.png" />
      </ToolbarItem.Icon>
    </ToolbarItem>
  </ContentPage.ToolbarItems>
  <ContentPage.Content>
  </ContentPage.Content>
</ContentPage>