locked
Universal Apps: Sharing MainPage Xaml

    Question

  • I have an app that I am making as a Universal App. The difference between the two is a total of about 5 lines or so of code total, even the Xaml, so I am trying to figure out how to share Xaml by putting it in the shared project. Here is the only real difference between the two:

    The Windows version has a content of a ViewBox. The Windows Phone version has that exact same ViewBox inside a Grid with one other element as follows:

    <Grid>
       <Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions>
       <MyOtherControl Grid.Row="0"/>
       <ViewBox Grid.Row="1"/>
    </Grid>

    The MyOtherControl element comes from another library, so none of it's code (Xaml or codebehind) would be in the shared or Windows Phone projects, and the ViewBox is exactly the same as the one in the Windows version. I know that having a Windows and Windows Phone version so similar that EVERYTHING is the same, but in my case it is (it is a game where the interface is simpler than a game of checkers, it will look the same no matter where you see it). Is there any way I can share all this code but still have the ability to make my one and only very simple change in MainPage.xaml? Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, November 18, 2014 2:22 AM

Answers

  • No. Xaml doesn't support conditional compiling, so you can't have a shared file that is almost the same for phone and Windows. To share the Xaml file it needs to be exactly the same.

    Depending on the exact code you may be able to pull differences out in UserControls, styles, etc. so that the main file itself is the same and can be shared.

    Another option would be to include both versions in the Xaml and light up only one of them depending on the platform (visual states can be good for this) or have one version and shift things around in code-behind.


    Tuesday, November 18, 2014 2:33 AM
    Owner

All replies

  • No. Xaml doesn't support conditional compiling, so you can't have a shared file that is almost the same for phone and Windows. To share the Xaml file it needs to be exactly the same.

    Depending on the exact code you may be able to pull differences out in UserControls, styles, etc. so that the main file itself is the same and can be shared.

    Another option would be to include both versions in the Xaml and light up only one of them depending on the platform (visual states can be good for this) or have one version and shift things around in code-behind.


    Tuesday, November 18, 2014 2:33 AM
    Owner
  • That sounds like it should be good enough for now. I have never used visual states for that purpose (I have only used them when editing templates), so if there are any good examples on how to use them for that I would love to learn more about how to do it. Also, where do I go to specify that my opening page (MainPage.xaml) is located in my shared project instead of in the Windows or Windows Phone project? Thanks again.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, November 18, 2014 3:11 AM
  • It's just normal visual state usage and set the appropriate visual state based on platform.

    There is nothing to specify about where MainPage.xaml is. The Shared project isn't built separately. The Windows build uses the files from the Windows and Shared projects. The Windows Phone build uses the files from the Windows Phone and Shared projects. If you move MainPage.Xaml from the platform specific projects to Shared then the same file will be used in both.

    Tuesday, November 18, 2014 3:44 AM
    Owner