Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, April 23, 2015

Step by Step : Windows Phone 7 : First Application

Hi,
Welcome to my blog of Windows Phone 7 Development. In this series of posts, I will try to explain various concepts in Windows Phone 7 Development. We will try to learn how to develop applications for Windows Phone 7.  So lets start. This post is not an exhaustive account about .NET Framework, C# concepts, Visual Studio 2010 and doesnot tell you all about Phone Emulator. This post takes into consideration that you have background knowledge ( at least beginner level ) on concepts like .NET, C#, Visual Studio and so on.

Please see my previous post titled "Windows Phone 7 : Setting Up Development Environment" to know how to setup your Windows Phone 7 Development environment. In this post we will see step by step to do the following
  • Develop of First Application which is Hello World application
  • How to change our start up page for our application

Hello World application:
Lets roll up our sleeves and start developing Windows Phone 7 applications. Lets start by developing typical Hello World application so that we can get familiar with developing and running Windows Phone 7 applications.

Launch Microsoft Visual Studio 2010 Express for Windows Phone.























Add new project. Make sure you select Silverlight for Windows Phone under Visual C# in Installed Templates. Select Windows Phone Application as shown in below screen shot and give name as HelloWorld

















Next you will get a popup as shown below that will ask for Target Windows Phone OS version. For our learning we will use Windows Phone OS 7.1 and click OK.













Next as shown in below figure drag and drop a Button and TextBlock from Toolbox and change below properties
Button
Name : btnClickMe
Content : Click Me

TextBlock
Name : txtMessage
Text:
 Width : 400














Now double click on Click Me button. This will open MainPage.xaml.cs and will add a method btnClickeMe_Click. This means whenever we click/press Click Me button this method will be executed. Please change the method as shown below. Here we are assigning text for our txtMessage TextBlock to Hello World !

private void btnClickMe_Click(object sender, RoutedEventArgs e)
        {
            txtMessage.Text = "Hello World !";
        }

Running and Debugging the application:
The easiest way to run the application is to press F5 key on your keyboard. So please press it. If you have any errors it will show them in Error list. If there are no errors Phone Emulator will launch as shown in below image. Phone Emulator is a component that runs your Windows Phone 7 apps that simulates as if you are running the application in any one of the Windows Phone mobiles. This will be helpful in testing all most all features of Windows Phone 7 application features except few. Sometimes Phone Emulator will take sometime to load your application so please be patient.
















Now press Click Me button and you will notice the Hello World ! text will be displayed. You can even keep a break point as shown in below screen shot and notice that it hits the line and there by you can debug your code.















Setting start up page:
Instead of setting default MainPage.xaml as our startup page we can set our own page as startup page. Lets see how we can do this. Right click on MainPage.xaml in Solution Explorer and press delete as shown in below image
























Build your solution to make sure you donot have any errors.

Add a new Windows Phone Portrait Page as shown in below screen shots by right clicking on your project ( here HelloWorld) in Solution Explorer. Name the page as MyFirstXamlPage.xaml
























Now add a button and textblock as you did for MainPage.xaml. Please see above for more details.
Now open WMAppManifest.xaml as shown in below image and update below DefaultTask tag.

 <Tasks>
      <DefaultTask  Name ="_default" NavigationPage="MyFirstXamlPage.xaml"/>
    </Tasks>














Now press F5 and if everything goes fine the application will be launched in Phone Emulator.

Conclusion:
In this post we saw how to develop a simple Windows Phone 7 application and how to change the start up page.I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.




Friday, December 5, 2014

Step By Step : InternalsVisibleTo attribute usage


Hi,

Welcome to my blog. In this post we will see how we can use InternalsVisibleTo attribute in your assembly and make the types that are ordinarily visible only within the current assembly are visible to a specified assembly. I had a method called "Add" in my class called "MathsDao.cs" and I need to use it in my unit test class called "MathsUnitTest.cs". I donot want to make my Add method public. I did some research and came across "InternalsVisibleTo" that helps me to expose my MathsDao.cs library to my unit test classes library. Lets see how I did it with a step by step example.

This is just one of the way which I followed  I am not saying this is the best and it doesnot have cons. My main aim is to share my experience so that it will be useful for the needy. I am new to blogging and I tried to make this post as much interesting as possible. Please excuse me for my grammatical errors and typos. I welcome your constructive comments and suggestions which helps in improving this post and my technical skills. 

Overview:
In this example we will see how we can expose a type in one assembly to another specified assembly. We will create the following projects

1) DataAccessObjects : This is C# class library that has MathsDao.cs with one internal method called Add.
2) DaoUnitTest: This is C# unit test project that has MathsDaoUnitTest.cs with one method called UT_Add. This will test the internal method Add in MathsDao.cs

Now let's start our work
For this article, I am using the following tools/technologies to develop a my sample application
Visual Studio 2013
C#
.NET Framework 4.5

I always have the habit of creating empty solution first and then work on it. Here also I did same thing. I created an empty visual studio solution titled "InternalsVisibleToDemo

I added a new C# class library project called "DataAccessObjects" and added "MathsDao.cs" to it.
I added a new C# unit test project called "DaoUnitTest" and added "MathsDaoUnitTest.cs" to it.



















 Now let's add below code to our MathsDao.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessObjects
{
    public class MathsDao
    {
        private int Add(int a, int b)
        {
            return a + b;
        }
    }
}
 

 Build solution and add DataAccessObjects.dll to DaoUnitTest project

















Open MathsDaoUnitTest and add below code

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using DataAccessObjects;

namespace DaoUnitTest
{
    [TestClass]
    public class MathsDaoUnitTest
    {
        [TestMethod]
        public void UT_Add()
        {
            MathsDao dao = new MathsDao();
           
        }
    }
}


If you try to access Add inside MathsDao you can see it is not available.















This is our problem how we can expose Add from MathsDao.cs into MathsUnitTest.cs without making it public. The answer to this is we can done by using InternalsVisibleTo.

Why we need InternalsVisibleTo?
Using this attribute we can successfully share the components which would otherwise be hidden with another assembly as we choose. The sharing of the components can be done by performing below two steps

1) Make your private methods as internal methods
2) Add InternalsVisibleTo attribute to the class whose internal types and methods you want to share with other assemblies.

Internal methods:
As I said in above steps, we need to convert our private methods to internal. 

Open MathsDao.cs and change Add method like below
 
internal int Add(int a, int b)
        {
            return a + b;
        }



InternalsVisibleTo attribute: 

As per Microsoft's MSDN : This attribute is used to identify one or more friend assemblies for a given assembly. A friend assembly is an assembly that can access another assembly's internal types and members. If you identify an assembly as a friend assembly, you no longer have to mark types and members as public in order for them to be accessed by other assemblies

Open MathDao.cs and add change code as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

[assembly: InternalsVisibleTo("DaoUnitTest")]
namespace DataAccessObjects
{
    public class MathsDao
    {
        internal int Add(int a, int b)
        {
            return a + b;
        }
    }
}


assembly: InternalsVisibleTo(the assembly namespace which needs to have access to internal methods of MathsDao.cs)

Build your solution.

Now go to MathsDaoUnitTest.cs and you will see now you have access to Add method





















Conclusion :
In this article we
saw how we can use InternalsVisibleTo attribute in your assembly and make the types that are ordinarily visible only within the current assembly are visible to a specified assembly. We created a simple application and saw step by step how a non public method is one assembly can be accessed in other assembly.

I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.