locked
Sharing code for a UserControl between Metro and WPF? RRS feed

  • Question

  • I am trying to use the example here but can't get it to work... There seems to be something wrong with the namespaces. Is there any example sharing a UserControl between Metro and Wpf explicitly?

    To make it simple I am just using an empty UserControl without any additional XAML Controls.

    Wednesday, December 18, 2013 8:18 PM

Answers

  • You can use the same techniques in the article to share much of the code between WPF and Windows Store apps. The stripped down Xaml in the article works in WPF as well as in Windows Phone and Windows Store apps.

    As step 11 suggests, you'll need to conditionally compile the namespaces to use valid WPF namespaces in WPF. You can use #if NETFX_CORE for Windows Store apps and #if WINDOWS_PHONE for phone. I'm not sure if there's a predefined directive for the full .Net framework used in WPF. You can either /define one yourself in the compiler options or use the #else clause.

    --Rob

    Wednesday, December 18, 2013 10:02 PM
    Moderator
  • The example you pointed out is how to share code between Store apps and WP8, not WPF and W8.  The namespaces are completely different between the two.


    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.

    Wednesday, December 18, 2013 8:34 PM
    Moderator
  • in the wpf version there is no xaml? i also linked the xaml and now its working: http://sdrv.ms/1g9X37r

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#


    • Edited by Dave SmitsMVP Saturday, December 28, 2013 2:07 PM
    • Marked as answer by Southbranch Saturday, December 28, 2013 2:19 PM
    Saturday, December 28, 2013 2:04 PM

All replies

  • The example you pointed out is how to share code between Store apps and WP8, not WPF and W8.  The namespaces are completely different between the two.


    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.

    Wednesday, December 18, 2013 8:34 PM
    Moderator
  • Fully aware of that and tried to make adjustments. But I was wondering if it should be possible at all or just me mixing things up?
    Wednesday, December 18, 2013 9:14 PM
  • You can use the same techniques in the article to share much of the code between WPF and Windows Store apps. The stripped down Xaml in the article works in WPF as well as in Windows Phone and Windows Store apps.

    As step 11 suggests, you'll need to conditionally compile the namespaces to use valid WPF namespaces in WPF. You can use #if NETFX_CORE for Windows Store apps and #if WINDOWS_PHONE for phone. I'm not sure if there's a predefined directive for the full .Net framework used in WPF. You can either /define one yourself in the compiler options or use the #else clause.

    --Rob

    Wednesday, December 18, 2013 10:02 PM
    Moderator
  • I can get it to work with sharing METRO->WP8 but not METRO->WPF. When the WPF project compiles I get the error message "...does not contain definition for InitializeComponent..". Seems to be something wrong with namespaces after all...

    Code with stripped UserControl and XAML/code behind as below, any ideas..?

    <UserControl
        x:Class="App1.Shared.MyUserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
            <TextBlock Text="Hello"/>
        </Grid>
    </UserControl>

    using System;
    using System.Collections.Generic;
    
    if NETFX_CORE // METRO STORE app
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    #elif WPF // does not work..
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    #elif WINDOWS_PHONE // works fine
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    #endif
    
    namespace App1.Shared
    {
        public sealed partial class MyUserControl1 : UserControl
        {
            public MyUserControl1()
            {
                this.InitializeComponent();
            }
        }
    }



    • Edited by Southbranch Wednesday, December 25, 2013 12:14 PM
    Wednesday, December 25, 2013 12:13 PM
  • That doesn't sound like a namespace issue. InitializeComponent should be part of the cs file automatically generated from the Xaml.

    How did you add your user control to your project? I just confirmed that if I do "Add.New Item..." and select User Control then copy your code in it works correctly. The only code change I made was to switch App1.Shared to my namespace in both files.

    --Rob

    Thursday, December 26, 2013 10:11 PM
    Moderator
  • Not sure I understand. Since I want to share the code in both cases, WPF and WP8, the user control was Add-As-Link after it was added originally as a New-Item in the Metro app in the first place. The .cs file is there in both cases.

    wh

    Thursday, December 26, 2013 10:50 PM
  • When linking the files make sure you set the build actions correctly so that the Xaml file gets compiled and generates the appropriate code-behind file (which includes InitializeComponent). Add a User Control from the wizard and copy the file properties:

    File properties: Build Action Page, Custom Tool MSBuild: Compile

    Thursday, December 26, 2013 11:27 PM
    Moderator
  • Properties in my end are the same as yours so I am not sure what I am doing wrong..

    I did exactly the same when Add-As-Link in the WPF as in the WP8 project (from Metro) and the WP8 Projects works and compiles just fine.

    Friday, December 27, 2013 2:59 PM
  • Try doing a clean build of your project. I downloaded your solution and the Windows Store app (App1) built and ran with no errors. I added the MyUserControl1 to MainPage.xaml and it appeared as expected.

    --Rob

    Friday, December 27, 2013 10:29 PM
    Moderator
  • I can't beleive it compiled in your end without any changes..?

    Yes, I have tried clean + re build several times and still get the message below.


    • Edited by Southbranch Saturday, December 28, 2013 12:55 PM
    Saturday, December 28, 2013 12:51 PM
  • can you share the project with us so we can have a look? think something with the x:Class in the UserControl tag isnt correct

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Saturday, December 28, 2013 1:24 PM
  • Saturday, December 28, 2013 1:39 PM
  • in the wpf version there is no xaml? i also linked the xaml and now its working: http://sdrv.ms/1g9X37r

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#


    • Edited by Dave SmitsMVP Saturday, December 28, 2013 2:07 PM
    • Marked as answer by Southbranch Saturday, December 28, 2013 2:19 PM
    Saturday, December 28, 2013 2:04 PM
  • You are right it ins't... For some reason it didn't add when I did Add-As-Link but now when I re-added it everything works fine.

    Many thanks, problem finally solved!!

    Saturday, December 28, 2013 2:19 PM