サービス業で働く社内SEのブログ

技術メモを適当にかいつまんで記載します

【Xamarin】Xamarin.FormsのMasterDetailPageインスタンス時にエラーが出る

XamarinFormsのMasterDetailPageを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:local="clr-namespace:ChapterZero.Views;assembly=ChapterZero"
             x:Class="ChapterZero.Views.MainPage">
  <MasterDetailPage.Master>
    <local:MasterPage></local:MasterPage>
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <local:TimeLinePage />
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

MasterDetailPageをこんな感じで書きつつ

<?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:vm="clr-namespace:ChapterZero.ViewModels;assembly=ChapterZero"
             x:Class="ChapterZero.Views.MasterPage"
             Padding="0,40,0,0">
  <ContentPage.BindingContext>
    <vm:MasterPageViewModel></vm:MasterPageViewModel>
  </ContentPage.BindingContext>
  <ContentPage.Content>
    <StackLayout VerticalOptions="FillAndExpand">
      <ListView VerticalOptions="FillAndExpand" ItemsSource="{Binding MasterPageList}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
  </ContentPage.Content>

</ContentPage>

MasterPageをこんな感じで書いたところ、以下のようなエラーが出ました。

f:id:Einherjar1632:20160214162836p:plain

System.Reflection.TargetInvocationExceptionとのことで
どうやらMasterPageに割り当てるContentPageのTitleを設定していなかった事が原因のようです。

<?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:vm="clr-namespace:ChapterZero.ViewModels;assembly=ChapterZero"
             x:Class="ChapterZero.Views.MasterPage"
             Padding="0,40,0,0"
             Title="たいとる">
  <ContentPage.BindingContext>
    <vm:MasterPageViewModel></vm:MasterPageViewModel>
  </ContentPage.BindingContext>
  <ContentPage.Content>
    <StackLayout VerticalOptions="FillAndExpand">
      <ListView VerticalOptions="FillAndExpand" ItemsSource="{Binding MasterPageList}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
  </ContentPage.Content>

</ContentPage>

上記のように、Titleを設定したところインスタンスされました。
この事象、どなたかが何処かで書かれていた気がするのですがすっかり忘れていたのでメモがてら記載しておきます。