Part I
Part II
Part III
Part IV
Part V
Part VI
This is the sixth and final installment of a several part series/journey to use social network resources for Windows Phone 7 development and produce a reusable code base.
What Has Come Before
In Part I the idea of going to social media for Windows Phone 7 research was introduced and we started putting together a solution with Visual Studio Express for Windows Phone 7. In Part II we took the great leap forward and added the MVVM pattern and IoC pattern to the solution. In Part III the Silverlight Toolkit controls, Coding4Fun controls, Bindable ApplicationBar control, and other Silverlight behaviors and actions were added. In Part IV, we found a web service to get random quotes and hooked up the design time data using Assembly.Load to avoid referencing the Design project in our main phone application. Finally, in Part V, we did some nice refactoring, added a custom page navigation solution to MVVM, added an About page, and implemented a bindable ApplicationBar control.
The application that was built gets random quotes from a public web service as JSON formatted data. It displays the quote on the Main page. A refresh button on the ApplicationBar will go get a new random quote. The user can also navigate to an About page and the use the hardware back button on the phone to get to the Main page.
The final task for this blog post is to talk about how to use simple copy/paste/rename techniques to be able to reuse the solution for other project. It is possible to export individual projects as project templates. It is not as easy to do this for a whole solution. So instead a somewhat more manual and tedious process can be used.
Note: This entire series of articles was based on using the free development tools. Renaming and refactoring code is easier using the professional versions of Visual Studio 2010. There is also a nice refactoring extension for Visual Studio 2010 Professional (or higher) called ReSharper.
Copy / Paste / Rename / Refactor / Reuse
The first thing to do is make a copy of the solution folder from Windows Explorer. This copied source code will be used for each separate project. The process when starting a new project with this source is:
1. Copy the solution source code folder using Windows Explorer
2. Name the new solution folder as the name of your project. For example, if you want to build a Silverlight Windows Phone application called, maybe, “Weather Report” you would name the solution folder “WeatherReportSolution”. Again, this is from Windows Explorer and not from within Visual Studio.
3. Open the solution and rename/refactor as needed to use the project name desired. “WeatherReport” as in the example above.
4. Compile and test to locate other items that require renaming.
Before continuing on it is a good idea to copy the solution folder and save it in a backup location. A good reference article on MSDN for what needs to be done can be found here: How to: Rename Solutions, Projects, and Items.
Without further ado, we simply follow those directions to rename the solution. This application will be called “RandomQuoter”. We rename the solution WindowsPhoneApplication1 to RandomQuoterSolution, the WindowsPhoneApplication1 project to RandomQuoter, the WindowsPhoneApplication1.Design project to RandomQuoter.Design, the WindowsPhoneApplication1.Shared project to RandomQuoter.Shared, and then update the namespaces within each project property editors.
If you follow the step to rename projects through the Solution Explorer the project references will also update properly. Make sure to manually change the Assembly name and Default namespace in WindowsPhoneApplication1, WindowsPhoneApplication1.Design, and WindowsPhoneApplication1.Shared. Once the projects and solution are renamed correctly compile the application and fix any errors. There should not be any if it was done correctly.
The next step is to do a Replace in Files operation on the entire solution. This can be a tricky operation without a good way to undo it. So avoid typos in the Replace in Files fields. The image below has the settings to use for this Replace operation:
Fill in the fields as pictured above and click Replace All. You are given a choice to open all files affected and therefore have a way to undo the changes.
Now build the solution again to find any errors and correct them. The build should succeed with all the XAML and code using the corrected namespaces.
Now we go back to the main phone application properties dialog and see that the xap file still shows the startup xap is pointing to WindowsPhoneApplication1.xap:
This needs to be fixed from outside of Visual Studio. Note too that the folder structure is not renamed in this process. Folders in Windows Explorer still have names with WindowsPhoneApplication1 in it. Also, because of this, the solution file itself will be pointing to these folders. These must be updated manually.
We save all the changes and close the solution in Visual Studio, or close Visual Studio entirely. First up, the folder name change. Change all the project folders within the solution folder to use the new project name.
From the above image to the names displayed in the image below.
Now use Notepad to update the folder paths within the solution file. This will be the .sln file located at the root of the solution folder. Once it is opened in Notepad (or Notepad2 pictured below) the task is a simple string replace. Replace the string “WindowsPhoneApplication1” with “RandomQuoter”. Save the file and this round of renaming is complete.
Now open the solution up in Visual Studio and locater the properties window for the RandomQuoter project. You will see that the startup object is blank. Select the RandomQuoter.xap item from the drop down list and recompile the solution.
The refactor and renaming steps are complete. Test that the application runs correctly using the Emulator.
With practice, this process can take as little as 5 minutes.
Summary
That is it! I hope you enjoyed reading through the series or that you found this post and the source code useful. There are many different approaches for the patterns used in the code, MVVM, IoC, and so on. Investigate and improve at your leisure. There are daily updates to the open source projects discussed here so be vigilant and check the authors blogs, twitter, and project sites for updates. Windows Phone 7 Development, I have found, is based on a strong community so whenever possible share with people on twitter and use the #wp7dev hash tag. Your applications will get better as a result of participating in this vibrant community.
Source Code
4cdc4b6b-3b43-4a8a-8b64-8f59ef6bee37|0|.0
Code, Silverlight, WP7