×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    Exclamation Performing code reuse in Windows Phone 8

    Hello guys,

    I am facing a certain situation where it needs your attention. I have to perform code reuse(I mean I dont want to copy files again and again). I have tried using Add as a link, but most of my shared classes contain namespaces. So I thought I can overcome this problem using macros in pre-processors. But I am not able to perform this process.

    Can any one help me, on how to achieve this code reuse. I want to explain in detail... I have a bunch of xaml files and I will be using them in 3 projects, so I dont want to copy-paste them again. I just want to use one file and just define with a macro saying, for Project 1 use namespace Project 1... for Project 2 use namespace Project 2 and for project 3 likewise.

    I want to perform this as this approach would save my time. Any help is really appreciated.

    Thanks a ton.

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    518

    Re: Performing code reuse in Windows Phone 8

    I'd suggest to put the stuff you want to reuse into a separate Assembly (project) and simply reference that assembly from all your actual App projects. They would still all reside in the same namespace (e.g. CompanyName.SharedControls) but you could use the same code in all projects.

    You can also add the separate projects to the solution of your current project and would have the ability to edit/compile them in the same Visual Studio instance.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Performing code reuse in Windows Phone 8

    You can also use linked files by adding files to the project but adding then as links: http://msdn.microsoft.com/library/vstudio/9f4t9t92.aspx

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    7

    Re: Performing code reuse in Windows Phone 8

    As suggested, the best way to do this is put your files into a separate project/assembly, where they will be put under the same, shared namespace. But if you absolutely need them put into separate assemblies under their very own namespaces, I would suggest exporting your files as Item templates or creating your code/xaml as snippets (see here http://msdn.microsoft.com/en-us/library/ms165394.aspx), defining your variable parts of that code (as what a namespace is) and then insert your code using that snippet into each project. There are useful tools to simplify this process, like the one here http://visualstudiogallery.msdn.micr...b-faee50f68392. Just note that in either of both cases, once the code is inserted, changing it in one project won't affect the other projects (as would be the case in actual shared code - one assembly).

    Andrej
    Last edited by andrejt; 2014-01-19 at 09:00.

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2011
    Location
    Portugal
    Posts
    957

    Re: Performing code reuse in Windows Phone 8

    It all depends on how you structure your projects.

    When you need to share code, even if only once, then you need to refactor the shared code to be independent of the project. This means that the shared code must be completely independent including on namespaces. The best way is without a doubt to use a separate Library Assembly as suggested by others where you have all shared components. You can even add the Library as a project reference so you can debug more easily.

    You can do a lot of code reuse these days, not only between WP projects but also on WP and WinRT projects. If you want to go all the way to reuse code between Windows Phone and Windows 8 apps, then I would recommend taking a loot at the PCL (Portable Class Library) as there are some extra steps to take in consideration,

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    518

    Re: Performing code reuse in Windows Phone 8

    PCLs are great for business logic or computation libraries. Unfortunately the WinRT and WP8 UI classes aren't the same so you can't put UI code (XAML, etc.) into a PCL. I hope this situation improves with future revisions of WP/WinRT. Currently the UI code on WP8 is based on the Silverlight APIs which uses the same concepts but different classes in different namespaces.

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2011
    Location
    Portugal
    Posts
    957

    Re: Performing code reuse in Windows Phone 8

    Quote Originally Posted by SB Dev View Post
    PCLs are great for business logic or computation libraries. Unfortunately the WinRT and WP8 UI classes aren't the same so you can't put UI code (XAML, etc.) into a PCL. I hope this situation improves with future revisions of WP/WinRT. Currently the UI code on WP8 is based on the Silverlight APIs which uses the same concepts but different classes in different namespaces.
    Yeap agree, but if you use MVVM for example you can reuse quite a bit of the Viemodel code that drives UI behavior. While UI design must be targeted per platform, you can reuse some of the logic behind it.

    I'll recommend to take a loot at Cristovão Morgado project iFixit that includes both WP8 and W8 projects because if i'm not mistaken works in this way: https://github.com/iFixit/ifixit-microsoft

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    568

    Re: Performing code reuse in Windows Phone 8

    There's a good set of videos on Channel 9 - Building Apps for Both Windows 8 and Windows Phone 8 Jump Start...

    http://channel9.msdn.com/Series/Buil...e-8-Jump-Start

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    518

    Re: Performing code reuse in Windows Phone 8

    As for UI decoupling - yes, MVVM works great as only the actual Views are platform dependent and you can share both Models and ViewModels. You might need to write some plugins to call into different platform capabilities though: e.g. Share Contract on WinRT, ShareMediaTask/ShareSocialMediaTask on WP.

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    Re: Performing code reuse in Windows Phone 8

    Thanks for all the answers. I have gone through all the answers and have come up with a way to perform code reuse. I have followed referencing of files, just as we do in iOS. Here how it goes,
    I have removed all the class files, for example MainPage.xaml and .cs file from the child project. I have referred all the classes(Add existing item, browse to Base project's classes and add as link) from my base project and ran the program it runs absolutely fine. I just have to check in the WMAppManifest.xml 's NavigationPage is set to MainPage.xaml. (Default value is that, but checking it again).

    Now solving this one, I want to know how do I share the local resources? I am able to refer all the Base Project's resource files. I would like to know how do I get the child's resources.

    Thanks.

  11. #11
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    518

    Re: Performing code reuse in Windows Phone 8

    Perhaps this article is of help to you: http://developer.nokia.com/Community...g_AppResources

  12. #12
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    Re: Performing code reuse in Windows Phone 8

    Thank you, I was actually looking for that.

  13. #13
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,559

    Re: Performing code reuse in Windows Phone 8

    FYI: This discussion has been featured and details can be found at http://developer.nokia.com/blogs/com...-8-development

  14. #14
    Nokia Developer Champion
    Join Date
    Dec 2012
    Posts
    45

    Re: Performing code reuse in Windows Phone 8

    Hello all, good discution and some people said the solution
    You can find one apresentation i did about it:
    Do not do more copy and paste between windows store and windows phone apps~

    There are some points that you need to know:
    - you cannot share UI code, xaml cannot be shared because uses different namespace and some controls don´t exist in WP or in Windows 8.
    - the code that you can share can be the core code, bussiness logic and for it you can use Portable Class Libraries or Link As and use directives.
    - if you use a pure MVVM implementation you will share code without problems.

    Resources, dataTemplates, sytles .... cannot be shared because it is depend the UI namespace.

    João Cardoso expose one project from Cristovão Morgado (IFixIt) that is a best example that you can follow.
    __________________________________________________

    @saramgsilva
    Microsoft MVP - Visual C#
    Nokia Developer Champion

Similar Threads

  1. Color code of 'Nokia Blue' in windows phone 7.5
    By StevenVT in forum General Development Questions
    Replies: 2
    Last Post: 2012-09-28, 09:52
  2. Pragmatic source code reuse study
    By cottrell in forum General Development Questions
    Replies: 5
    Last Post: 2012-01-03, 19:29
  3. Replies: 7
    Last Post: 2008-05-01, 08:26
  4. Replies: 0
    Last Post: 2006-04-06, 18:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×