First Guide to MEF and Silverlight (Part–II)
In my previous article “First Guide to MEF & Silverlight (Part–I)” I discussed about MEF with a small simple console application. Hope that was useful to you to understand the basic knowledge of MEF. In this article, I will guide you to create a simple Silverlight application using the MEF. If you are new to MEF, I strongly recommend you to read my previous article to gain knowledge on the basic things of MEF like Importing, Exporting, Catalog, Container etc. Read the complete article and at the end if you have any queries, please let me know. I will try to answer them as soon as possible. Always Appreciate your valuable feedbacks. To start working with the Silverlight & MEF application, you need to setup your development environment. In your development PC, you need the following things already installed: Visual Studio 2010 with .Net Framework 4.0 Silverlight 4.0 Tools for Visual Studio 2010 Once your environment is ready with the above tools, we can start with our next step. First of all, we need to create a Silverlight application project and then we have to add some assembly reference in order to work with the MEF. Hence, follow the following steps to setup your project: Open your Visual Studio 2010 IDE Now go to File –> New –> Project or just press Ctrl + Shift + N to open the “New Project” dialog window. From the left panel expand “Visual C#” and then select “Silverlight”. This will populate the right pane with the Silverlight templates.
Now, in the MainPage.xaml add an ItemsControl & name it as “widgets”. Wrap the ItemsControl with a StackPanel to hold the items. The XAML file will look as below: 
Now it’s time to create a UserControl. Right click on the Silverlight project and add one UserControl & name it as “EmployeeWidget”. We will not design more inside it as it is not require to understand the MEF. To make it properly visible just add one TextBlock with some strings. In our example, I am setting “Employee Widget” as the text string for the TextBlock and also setting a color “Red” to the Grid background. Resize the UserControl to 150 x 150, so that, it will set properly in the screen. Let’s see the below code for detailed layout:

As mentioned above, I resized the control to 150 x 150 and then changed the background color to Red. Added a TextBlock having “Employee Widget” as the value to the TextProperty of the TextBlock with a foreground color of White, so that, it will be visible on top of the Red color. No need to describe more on it. Just check the above xaml and you will get the idea behind it.
Press F7 in the EmployeeWidget.xaml page to open up the code behind file. Inherit the EmployeeWidget class from IWidget. Once done, add the attribute “Export” having the type of IWidget to the class. This will ensure that the class will export for the MEF to Satisfy. Look into the code here:

As our UserControl has been exported for the MEF to satisfy, it’s the time to import it in our MainPage. To do this, open the MainPage.xaml.cs and create a property “Widgets” of IWidget type array. We are using array type to ensure that, we can import many widgets there. So, mark the property with the atrribute “ImportMany” of type IWidget. Once that is done, our code is ready to import the exported class. Now inside your MainPage constructor, iterate through the array and add all the Widgets as the item to the “widgets” items control which we already added in the XAML. Now if you run your application, you will notice that the Widgets array is null. Why? Just think. Oh yea!!! We forgot to satisfy the MEF Initializer to satisfy the property. What to do for that? To satisfy the imports, you need to call “CompositionInitializer.SatisfyImports(this);” just before iterating through the array list. Have a look into the code:

Now, run your application once again and this time you will see the EmployeeWidget added to the UI screen with a text “Employee Widget” having a Red background. Here is the screenshot of the same:
Woho!!! Nice. We didn’t create the object of the UserControl and add it to the ItemsControl. The MEF framework did it for us. Once satisfied, it created the object and imported it to the MainPage.
That’s it. Now we will do one more thing. We will create another UserControl named “CustomerWidget” and follow the same steps mentioned above for exporting the control. This time we will set the text to “Customer Widget” and will set the background color to Green. This will make it easy for us to distinguish between the items. Here is the XAML code of the CustomerWidget usercontrol:

Now open the code behind file of CustomerWidget by pressing F7 in the xaml page and follow the same step as we did for the EmployeeWidget i.e. implement the CustomerWidget from the interface named IWidget and mark the class exportable by setting the Export attribute of the IWidget. Here is the code for your reference:

That’s it. This time we didn’t add/modify anymore code in our MainPage. Just run the application and you will see the CustomerWidget added in the panel along with the control named EmployeeWidget. See the screenshot of the same here:
View the original article here
Categories: C# Tags: First, guide, PartII, sample DragLeave in c#, silverlight, silverlight shared whiteboard
Checking if User is connected to Internet or Not using Win32 API and C# | Win32 API and C#
In application like Messenger or Application that provides automatic update need to check that user is connected to internet or not before proceeding further.
so we need to check whether he is connected to internet or not we can check that with use of WIN32 API.
we can use Win32 API Wininet.dll ’sInternetGetConnectedState() method to check Internet Status
for working with Pinvoke we need to add name space
using System.Runtime.InteropServices;
now we need to write prototyping of the function like below
[DllImport("wininet.dll")]private extern static bool InternetGetConnectedState(outint connectionDescription, int reservedValue);
Now simply we can use this function to check internet connectivity
take one button and label on form In buttons’s click even write following code
private void btnCheckConnection_Click(objectsender, EventArgs e)
{
int Description=0;
bool isConnected = InternetGetConnectedState(out Description, 0);
if (isConnected == true)
{
label1.Text = ”User is Connected to Internet “;
}
else
{
label1.Text = ”Disconnected”;
}
}
Here is how complete code look like
———————————————–
public partial class Form1 : Form
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription,int reservedValue);
public Form1()
{
InitializeComponent();
}
private void btnCheckConnection_Click(object sender, EventArgs e)
{
int Description=0;
bool isConnected = InternetGetConnectedState(out Description, 0);
if (isConnected == true)
{
label1.Text = ”User is Connected to Internet “;
}
else
{
label1.Text = ”Disconnected”;
}
}
}
Thank you.
Categories: C# Tags: Checking, connected, download different Types Of Compilers in C#, Internet, sample DragLeave in c#, silverlight shared whiteboard, Using, Win32