Can Of Code

Tag Archives: C#

Windows 8 – Adding a Share Contract to your App in C#

I’ve finally started playing around with Windows 8 after spending ages getting it to work in VirtualBox. I struggled to find any sample code / tutorials on adding a share contract in C# so here is my guide.


First things first we need to add a import statement:

Using Windows.ApplicationModel.DataTransfer;


Next up we need to fetch a DataTransferManager and then associate a method with the DataRequested event. I placed this code in the constructor of the MainPage.xaml.cs.

DataTransferManager manager = DataTransferManager.GetForCurrentView();
manager.DataRequested += new TypedEventHandler(share_DataRequested);


Now we need to create the share_DataRequested Method that’s called when a share request is made.

private void share_DataRequested(object sender, DataRequestedEventArgs e)
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Sharing something";
    request.Data.Properties.Describtion = "Sharing something with someone";
    request.Data.setText("I'm sharing!");



The method above is where you tell Windows what data your app wants to share. The example above shows the sharing of a simple string saying “I’m sharing”. The picture below shows how the message will apear once the user select a Share compatible App to share with. On the Enterprise Edition you can only share using Mail by default so you may need to download some other Apps to test the sharing.


Your App now makes use of a share contract! If you want to manually launch the Share Contract UI for the user you can use the following code:



Which will bring up the below UI.


Posted in Software | Tagged , , , , | Leave a comment

C# Quick LINQ example

Recently I’ve been working with LINQ so I thought i’d do a quick post to share the two basic LINQ approaches.

First up is an example of a query that returns a collection of objects. In this case they are RouteItem objects.


// The LINQ Query
var routesInDB = from RouteItem route in DB.routeItems select route;

// Populate a ObservableCollection with the query results.
routeItems = new ObservableCollection<RouteItem>(routesInDB);


The second example is for when you want to get one item from a query similar to “SELECT TOP 1” or  “SELECT … Limit 1” in SQL.


 // The LINQ Query
IQueryable<setting> cityQuery = from c in DB.setting where c.ID == 1 select c;

// populate the setting object with the query result.
setting sett = cityQuery.FirstOrDefault();


The example above shows the returning of a single setting object instead of a collection.

The final example shows how to get the number of results. This may be useful if you wanted to see if an item was already present in the collection.


// The LINQ Query
var query = from c in routeItems where c.routeName == "home" select c;

// get the size of the returned result set.
int size = new ObservableCollection<RouteItem>(query).Count;


This example shows a query to count how many RouteItems have the name “home”. We place the result into an int variable.


Posted in c#, WPF | Tagged , , , | Leave a comment

C# Modifying a Application Settings Connection String

On the application I have currently been working on I discovered a small issue. The connection string was stored as a Application setting which meant it was read-only. Its important to set the connection string as a ConnectionString type within the Application settings so that its secure.

But what happens if the connection string changes? This isn’t so bad in a internal application as you can just rollout a new build, but what if each user is likely to have a different connection string.

To change the connection string, help (as usual) came from stack overflow Programmatically change connection string.

That was all fine and dandy but when I went to go and use the application setting by going:


It returned the old unchanged connection string. the simple way round it was instead of getting the connection string using the code above I had to do this:

System.Configuration.Configuration config2 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 pconnectionString = config2.ConnectionStrings.ConnectionStrings["ConString"].ToString();

Bingo! that worked!



Posted in c#, Software | Tagged , , | Leave a comment