Debugging and Breakpoints not working in Visual Studio 2015

If you are opening your old solutions in Visual Studio 2015 you may experience some problems with hitting breakpoints and debugging therefore debugging is not possible especially in Unit Test projects.

Solution
I found out that the solution (.sln) was the problem. I created a new empty Solution in VS 2015 added all existing projects and everything is working fine again.

Tagged , ,

Dump Silverlight 8.1 PRI file to xml

“C:\Program Files (x86)\Windows Kits\8.1\bin\x86\makepri.exe” dump /ex “C:\Program Files (x86)\Windows Phone Kits\8.1\bin\x86\MrmEnvironmentExtDl.dll” /if “..\resources.pri” /of “…resources.pri.xml”

WPF DateTimeControl – The Outlook-Style DateTimePicker control for WPF

WPF does not offer a DateTimePicker out of the box. The WPF toolkit contains a DateTimePicker control, but I dislike it’s look and feel with the up- and down buttons. So I wanted an Outlook-style DateTimePicker control that look like this…
DateTimeSample

You can embedd the control in the normal XAML way…

 <Grid>
        <StackPanel VerticalAlignment="Center">
            <local:DateTimeControl x:Name="dateTimeControl"/>
...

Here is a sample application containing all the code…
https://1drv.ms/u/s!AnjbhF0srlTbgq5WHia7vSfk7wzIMg

Tagged , ,

Async/Await Snipped: Ensure one-time execution

Think if you have some initialization code that you want to execute only once. Multiple threads could call in. Your code uses async and await keywords and you want to be able to await until it completes…

        Task _workerTask;

        object _workerSyncRoot = new object();

        async Task EnsureWorkerDoneAsync()
        {
            // ensure that the worker is executed only once
            lock (_workerSyncRoot)
                if (_workerTask == null)
                    _workerTask = WorkerAsync();
            
            // await the worker, if it has not
            if (_workerTask.Status != TaskStatus.RanToCompletion)
                await _workerTask;
        }
Tagged , , ,

Async/Await Snipped: Avoid multiple calls by using delays

When you work with async and await keywords in C#. You sometimes want to avoid unnecessary calls, that are caused by multi-threading. Here is a simple snipped that works fine…

Scenario: Last Wins

        Task _delayTask;

        async void StartWithDelay()
        {
            // keep a local copy and a member copy of the task
            Task delayTask = _delayTask = Task.Delay(1000);

            // await the delay
            await delayTask;

            // is it still the same task (or is there a new one)
            if(delayTask == _delayTask) 
            {
                // do your work here...
            }
        }
Tagged , , ,

Design-Time Dictionary

In my current VSTO project I need a design-time only resource dictionary. I found the following solution and modified it just a bit.

http://social.technet.microsoft.com/wiki/contents/articles/23287.trick-to-use-a-resourcedictionary-only-when-in-design-mode.aspx

I had some problems with the DesignMode detection in that sample so I used the version here…

https://bernhardelbl.wordpress.com/2011/06/16/howto-detecting-design-mode-in-wpf-asp-net-and-windows-forms-applications/

 


public class ResourceDictionary : System.Windows.ResourceDictionary
{
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
/// <returns>The source location of an external resource dictionary. </returns>
public new Uri Source
{
get
{
if (Util.DesignMode)
return base.Source;
else
return null;
}

set
{
if (Util.DesignMode)
{
base.Source = value;
}
}
}
}

Encrypt and Decrypted a String using symetric key

Encrypt and decrypt a string value to a encrypted base64 string and back by Rijndael.

You can use it as snipped. The keys are hard coded for simplification…


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CryptoTest
{
/// <summary>
/// Can encrypt and decrypt a string
/// </summary>
public class CryptoService
{
static byte[] Key = new Guid("{1F2D888B-9566-4F99-92DA-13C37A7ACBDA}").ToByteArray();
static byte[] IV = new Guid("{2A4261C5-A53E-47B3-BF4A-97D944C93813}").ToByteArray();

public string EncryptString(string plainText)
{
// Instantiate a new RijndaelManaged object to perform string symmetric encryption
using (var rijndaelCipher = new RijndaelManaged())
{
// Set key and IV

rijndaelCipher.Key = Key;
rijndaelCipher.IV = IV;

// Instantiate a new MemoryStream object to contain the encrypted bytes
using (var memoryStream = new MemoryStream())
{
// Instantiate a new encryptor from our RijndaelManaged object
using (var rijndaelEncryptor = rijndaelCipher.CreateEncryptor())
{

// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write))
{
// Convert the plainText string into a byte array
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

// Encrypt the input plaintext string
cryptoStream.Write(plainBytes, 0, plainBytes.Length);

// Complete the encryption process
cryptoStream.FlushFinalBlock();

// Convert the encrypted data from a MemoryStream to a byte array
byte[] cipherBytes = memoryStream.ToArray();

// Convert the encrypted byte array to a base64 encoded string
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

// Return the encrypted data as a string
return cipherText;
}
}
}
}
}


public string DecryptString(string cipherText)
{
// Instantiate a new RijndaelManaged object to perform string symmetric encryption
using (RijndaelManaged rijndaelCipher = new RijndaelManaged())
{
// Set key and IV
rijndaelCipher.Key = Key;
rijndaelCipher.IV = IV;

// Instantiate a new MemoryStream object to contain the encrypted bytes
using (var memoryStream = new MemoryStream())
{
// Instantiate a new encryptor from our RijndaelManaged object
using (var rijndaelDecryptor = rijndaelCipher.CreateDecryptor())
{
// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write))
{

// Will contain decrypted plaintext
string plainText = String.Empty;

// Convert the ciphertext string into a byte array
byte[] cipherBytes = Convert.FromBase64String(cipherText);

// Decrypt the input ciphertext string
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

// Complete the decryption process
cryptoStream.FlushFinalBlock();

// Convert the decrypted data from a MemoryStream to a byte array
byte[] plainBytes = memoryStream.ToArray();

// Convert the encrypted byte array to a base64 encoded string
plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);

// Return the encrypted data as a string
return plainText;
}
}
}
}
}
}
}