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

  • Question

  • User28549 posted

    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.

    Tuesday, July 7, 2015 12:48 AM

Answers

  • User181 posted

    No matter what you will still need some C# code for this. Using the constructor and makes sense (assign the two properties after InitializeComponent is called), or you could also make new properties that are DataTemplates and allow your clients to create the pages in XAML. Even with that approach you would still be writing C# code to handle it here, though.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 7, 2015 12:56 AM

All replies

  • User181 posted

    No matter what you will still need some C# code for this. Using the constructor and makes sense (assign the two properties after InitializeComponent is called), or you could also make new properties that are DataTemplates and allow your clients to create the pages in XAML. Even with that approach you would still be writing C# code to handle it here, though.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 7, 2015 12:56 AM
  • User28549 posted

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

    Tuesday, July 7, 2015 5:27 PM