locked
How do I add a XAML page to a Universal App?

    Question

  • I created a new Blank App. Everything works as it should. Now I want to add a new page. This is the wrong way I'm doing it:

    First, I add a new Blank Page to the Windows Project

    foo.xml.cs

    namespace testapp
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class foo : Page
        {
            public foo()
            {
                this.InitializeComponent();
            }
        }
    }

    Then I add the same class name to the Shared project

    foo.cs

    namespace ZUMOAPPNAME
    {
        class foo
        {
        }
    }

    I modify foo.cs to

    namespace testapp
    {
        public sealed partial class foo
        {
            public void shared()
            {
                win();
            }
        }
    }

    And then add a method to foo.xaml.cs to make it

    namespace testapp
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class foo : Page
        {
            public foo()
            {
                this.InitializeComponent();
            }
    
            public void win()
            {
                shared();
            }
        }
    }

    The win method can call the shared method.

    However, for the shared method, the compiler gives me this error:
    The name 'win' does not exist in the current context

    What's the proper way to add a new page?

    Tuesday, February 10, 2015 9:31 AM

Answers

  • Hi Greg,

    Take a look at my blog entry Strategies for sharing code in XAML-based Universal apps

    As Magnus notes, all parts of a partial class need to be in the same namespace to be part of the same class. ZUMOAPPNAME.foo and testapp.foo are unrelated classes.

    It's not clear exactly what you're trying to do here, but a common pattern is to have different Xaml for the Windows and WindowsPhone projects but to share data. A good way to do that is to create the new page with the same name (for example: "foo") in both the Windows and WindowsPhone projects. This will add a foo.xaml and foo.xaml.cs file in each and will define the foo class in both foo.xaml.cs files.

    You can then add a foo.cs file in the shared project with your shared functions:

    Shared/Foo.cs:

    namespace testapp { public sealed partial class foo { public void shared() { do_shared_stuff(); do_plat_specific_stuff(); } } }


    And call it from the platform specific functions:

    Windows\foo.xaml.cs:

    namespace testapp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class foo : Page { public foo() { this.InitializeComponent(); shared(); } public void do_platform_specific_stuff() { // do windows stuff } } }

    You can use this to mix and match as much as you'd like. If you have very straigtforward UI you may be able to copy the entire .Xaml and .Xaml.cs into the shared directory or you may have platform specific Xaml but share the Xaml.cs file. In most cases you'll have a mix of platform specific xaml and code and shared code.

    Tuesday, February 10, 2015 4:52 PM
    Owner
  • The functions need to be in the exact same class to be shared this way.

    That means that the partial class has to have the same name and be in the same namespace. The examples you posted at the top of this thread are in different namespaces (you have testapp.foo and ZUMOAPPNAME.foo) and so are completely unrelated.

    If you still have the problem after fixing the namespaces can you share a minimal repro on your OneDrive? This works in general, so we'd need to see exactly what you're doing to explain what is going wrong.

    Wednesday, February 11, 2015 9:17 PM
    Owner

All replies

  • No...  these are different projects and won't compile together.  If you have code that you want to share between both Phone and Big Windows, simply create a custom class.  XAML Pages are the UI, and the UIs are different between the two.  Create a separate XAML page for each Phone and Big Windows.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, February 10, 2015 1:16 PM
    Moderator
  • Partial class definitions must be defined in the same assembly and in the same namespace for the compiler to merge them into a single class: https://msdn.microsoft.com/en-us/library/wa80x488.aspx

    Othwerwise there will just be two completely different classes and you won't be able to call a method defined in the second partial class from a method that's defined in the first partial class and vice versa.

    Please remember to mark helpful posts as answer to close your threads.

    Tuesday, February 10, 2015 2:36 PM
  • Hi Greg,

    Take a look at my blog entry Strategies for sharing code in XAML-based Universal apps

    As Magnus notes, all parts of a partial class need to be in the same namespace to be part of the same class. ZUMOAPPNAME.foo and testapp.foo are unrelated classes.

    It's not clear exactly what you're trying to do here, but a common pattern is to have different Xaml for the Windows and WindowsPhone projects but to share data. A good way to do that is to create the new page with the same name (for example: "foo") in both the Windows and WindowsPhone projects. This will add a foo.xaml and foo.xaml.cs file in each and will define the foo class in both foo.xaml.cs files.

    You can then add a foo.cs file in the shared project with your shared functions:

    Shared/Foo.cs:

    namespace testapp { public sealed partial class foo { public void shared() { do_shared_stuff(); do_plat_specific_stuff(); } } }


    And call it from the platform specific functions:

    Windows\foo.xaml.cs:

    namespace testapp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class foo : Page { public foo() { this.InitializeComponent(); shared(); } public void do_platform_specific_stuff() { // do windows stuff } } }

    You can use this to mix and match as much as you'd like. If you have very straigtforward UI you may be able to copy the entire .Xaml and .Xaml.cs into the shared directory or you may have platform specific Xaml but share the Xaml.cs file. In most cases you'll have a mix of platform specific xaml and code and shared code.

    Tuesday, February 10, 2015 4:52 PM
    Owner
  • Yes, what I wrote was not straight forward. Let's instead take a look at the graphic on your blog entry.

    Windows & WindowsPhone have a file called MainPage_Platform.cs
    Shared has a file called MainPage.xaml.cs

    My methods in MainPage_Platform.cs can see and access all of the methods/structs/etc in MainPage.xaml.cs
    However, the methods in MainPage.xaml.cs CAN NOT see or access ANY methods/structs/etc in MainPage_Platform.cs. It's as if they are private.

    What I'm trying to do is put all of notification code in MainPage.xaml.cs, and then call a method in MainPage_Platform.cs to show the notifications.

    Wednesday, February 11, 2015 8:10 AM
  • The functions need to be in the exact same class to be shared this way.

    That means that the partial class has to have the same name and be in the same namespace. The examples you posted at the top of this thread are in different namespaces (you have testapp.foo and ZUMOAPPNAME.foo) and so are completely unrelated.

    If you still have the problem after fixing the namespaces can you share a minimal repro on your OneDrive? This works in general, so we'd need to see exactly what you're doing to explain what is going wrong.

    Wednesday, February 11, 2015 9:17 PM
    Owner