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

Tuesday, June 14, 2011

Localizing Windows Phone 7 ApplicationBar

In order to provide a seamless user experience on your Windows Phone 7 you also have to localize the ApplicationBar within your app.

First, you will encounter that the ApplicationBar on the Microsoft.Phone.Shell namespace is not an usual UIElement and therefore can not be named via
x:name
The second thing you will recognize is that the ApplicationBar property of an PhoneApplicationPage is not initialized due the usual InitializeComponent method.

On my search over the internet I spotted some suggested hints, but often the solution is obvious.

Localizing Silverlight toolkit for Windows Phone 7

Due my localization adventures of my apps for Windows Phone 7 I spotted out an library to get the toolkit for Silverlight localized.

You could do this easily by yourself but Peter Foot already provides a zip with needed resource files. It's available on Localised Resources for Silverlight Toolkit Nov 2010

Wednesday, November 24, 2010

Store data from async web service call with Silverlight

Did you ever tried to store data on Silverlight from an async web service call using SaveFileDialog - and get told it doesn’t work?
But that is only half the truth. Here it how it works!

Sunday, October 31, 2010

Auto checkout with VS2010 and TFS2010 not working

On my project “Simple Proxy Switch” I decided to access codeplex version control system through VS2010 TFS integration.

As I mainly use Subversion I like that you can edit files without locking them. As I arrived on TFS I missed this a lot. Searching for other options I found the ability of TFS to check out files on edit.

image

Nevertheless it doesn’t work for me. Files still are not getting checked out on edit.

Fast search at the web discovers some helpful links mainly again in the Microsoft Developer Network.

The missing setting was the binding of the solution to TFS. This can be achieved on Selecting solution and than go through

File –> Source Control –> Change Source Control

select solution and hit <Bind>.

Et voilĂ . Now VS2010 checks out files on edit.

Thursday, October 21, 2010

Detect current network setting with C#

In cause of my latest project Simple Proxy Switch I had to face the challenge of detecting the setting of the current network the computer is connected to.

Now here’s my snippet how do I retrieve the current network setting:

// zero conf ip address
IPAddress zeroConf = new IPAddress(0);
// get current assigned addresses
IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName());

var networkData = NetworkInterface.GetAllNetworkInterfaces()
  // filter running network interfaces
  .Where(network => network.OperationalStatus == OperationalStatus.Up)
  // filter unknown interfaces
  .Where(network => network.NetworkInterfaceType != NetworkInterfaceType.Unknown)
  // filter loopback interfaces
  .Where(network => network.NetworkInterfaceType != NetworkInterfaceType.Loopback)
  // get the properties
  .Select(network => network.GetIPProperties())
  // filter initialized gateways
  .Where(ipProps => ipProps.GatewayAddresses.All(gateway => gateway.Address != zeroConf))
  // filter and get ip addresses
  .SelectMany(ipProps => ipProps.UnicastAddresses.Where(ucAddress => hostAddresses.Contains(ucAddress.Address)))
  .Where(ucAddress => hostAddresses.Contains(ucAddress.Address))
  // simply use the first
  .FirstOrDefault();

Be the force with you!

Wednesday, August 11, 2010

ReSharper shortcuts for VS2010 to improve TDD productivity

ReSharper is a powerful tool that helps you to produce better code.
One very nice feature is to run your NUnit-Test within the integrated Testrunner. But it’s a little bit annoying to click on the play button every time you want to run a test.
The easiest thing to handle this is to set up keyboard shortcuts to run you tests. Visual Studio supports setting shortcuts for a lot of actions, so let’s go straight ahead and set up shortcuts.
Simple open the options dialog for keyboard settings via Tools –> Customize –> hit “Keyboard” button and then search for
ReSharper.ReSharper_UnitTest_

As result you get something like the following:
image

Now you can set the shortcut for the desired operation.

I have chosen combinations of

Ctrl + Shift + Alt & '<'

A short explanation of the shown possibilities:
  • ContextRun runs the current selected test
  • ContextDebug debugs the current selected test
  • RunCurrentSession reruns a previous test session
  • RunSolution runs all test within the solution
Have fun.

Thursday, June 24, 2010

Remote Certificate validation

Sooner or later every developer has to deal on retrieving information from some resource accessible through HTTP protocol.

While it is easy to get this information from unsecured HTTP resources it's often the case that such a resource is only available for secured access through HTTP.

In that case and to ensure that this connection is trusted you have to check the server certificate for that connection.
Some sample piece of code to access and download data over http can look like this one:
string data = String.Empty;
using (WebClient webClient = new WebClient())
{
   byte[] rawData = webClient.DownloadData(new Uri(/* path to resource */));

   using (MemoryStream memStream = new MemoryStream(rawData))
   {
      using (StreamReader reader = new StreamReader(memStream, true))
      {
         data = reader.ReadToEnd();
      }
   }
}

This code works well for connections through HTTP.

Now you have to access the same resource over HTTPS and this is where you have to face a new challenge:

Validation of Certificates.

In the case of .NET there is the ServicePointManager that let you intercept the mechanism of validation.
It defines the ServerCertificateValidationCallback method which is called on secure resource access.

The adjusted code is now something like this:
string data = String.Empty;
using (WebClient webClient = new WebClient())
{
   RemoteCertificateValidationCallback validationCallback =
      (sender, cert, chain, errors) =>
      {
         return true;
      };

   ServicePointManager.ServerCertificateValidationCallback += validationCallback;

   try
   {
      byte[] rawData = webClient.DownloadData(new Uri(/* path to resource */));

      using (MemoryStream memStream = new MemoryStream(rawData))
      {
         using (StreamReader reader = new StreamReader(memStream, true))
         {
            data = reader.ReadToEnd();
         }
      }
   }
   finally
   {
      ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
   }
}

This code simply declares all certificates as valid which is some kind of stupid.

Let's focus on the validation of the certificate. Feel free to implement you own code of validation or just simply use .NET integrated features.

Something I did at bugtraqplugin is to use the default validation chain which depends on certificates stored at the Windows certification storage.

My simple approach:
RemoteCertificateValidationCallback validationCallback =
   (sender, cert, chain, errors) =>
   {
      return (certificate as X509Certificate2 ?? new X509Certificate2(certificate)).Verify();
   };

All code put together can be available at source code of bugtraqplugin.