Forum Xamarin.Forms

MasterDetailPage, Use Master and Detail from constructed parameters in XAML?

ChaseFlorellChaseFlorell CAInsider, University mod
edited July 2015 in Xamarin.Forms

I'm using the Dependency Injection pattern throughout my app (not service location), and in doing so, I never new up a Page(), but instead I just add the page to the parent's constructor.

public class MyMDPage : MasterDetailPage
{
    public MyMDPage(MyDetailPage myDetailPage, MyMasterPage, myMasterPage) 
    {
        _myMasterPage = myMasterPage;
        _myDetailPage = myDetailPage;
        Master = _myMasterPage;
        Detail = _myDetailPage;
    }
}

Now I realize that this is splitting hairs, but I'd really like to use _myMasterPage and _myDetailPage in the 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"
    x:Class="TimeTracker.Pages.RootPage">
  <MasterDetailPage.Master>

        <!-- How Do I bring in _myMasterPage variable 
             so that I don't need to set it in C#?-->

  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage BarBackgroundColor="{StaticResource BaseBlue}" 
                    BackgroundColor="{StaticResource BaseBlue}" 
                    BarTextColor="{StaticResource LightBlue}" >
      <NavigationPage.Icon>
        <OnIdiom x:TypeArguments="FileImageSource">
          <OnIdiom.Tablet></OnIdiom.Tablet>
        </OnIdiom>
      </NavigationPage.Icon>
      <x:Arguments>

       <!-- How Do I bring in _myDetailPage variable 
             so that I don't need to set it in C#?-->

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

If you're wondering why I inject the page instead of newing it up, it's because each of those pages also use DI and therefore also have constructor parameters.

Is there any way to do what I'm looking for?

site note: I was thinking it would be through a StaticResource or something along those lines, but I just don't understand those bits well enough.

Best Answer

Answers

  • ChaseFlorellChaseFlorell CAInsider, University mod

    This does make sense, and I do want to avoid the need for a DataTemplate. So I suppose I'll leave it as is.

Sign In or Register to comment.