Sharing code behind between xaml files

I have a situation where a particular screen will be laid out differently depending on some internal business logic. All the elements on the screen are the same, but it will be laid out differently. I'd ideally like to create the xaml files separately but have them share the same code behind file since all the logic is the same. Is there a recommended way to do this? I haven't found any documentation on this as yet.

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I would say the simplest way to achieve your goal is to have a SharedBasePage.cs with all the shared logic.

    Then your ImplementationPage1.xaml and ImplementationPage2.xaml use that as a Base Class.

  • The4DroogsThe4Droogs Member ✭✭

    Do you mean that you'd have a code behind for each xaml file, and both of those code behinds would basically be stubs that inherit from the base class?

  • LandLuLandLu Member, Xamarin Team Xamurai

    You can define multiple ContentViews and then embed them on your content page depending on your business logic.
    You only need to create one view model for these views as they have the same binding context as its parent content page.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @The4Droogs said:
    Do you mean that you'd have a code behind for each xaml file, and both of those code behinds would basically be stubs that inherit from the base class?

    Yes, that is what I would do. LandLu's suggestion is also fine, depending on your use case.

Sign In or Register to comment.