Category Archives: C#

Windows.Web.HttpClient and it’s PasswordCredential class

Microsoft is changing the Api to get HTTP content from version to version. Also the type of possible authentication mechanism behind is changing all the time. So in Universal App for Windows 10, Windows Phone or Windows Store 8 or 8.1. the underlying platform for HTTP access is different in several areas.

System.Net.HttpClient

The System.Net.HttpClient from NuGet that is using the different underlying platforms and therefore is making the same problems. To not being totally confused, it is better not to use System.Net and always use the underlying platform directly instead

Windows.Web.HttpClient or HttpWebRequest

It is better to use one these clients.

  • Windows Phone 8: HttpWebRequest
  • Windows 10 and 8.1 should use Windows.Web.HttpClient

Known problems and bugs in Windows.Web.HttpClient or the old HttpWebRequest:

There are some known bugs available but for a lot of them there are work arounds you can use:

If you have any problems by using the included authentication methods (for example: PasswordCredential) you “just” need to do the authentication by your self creating and adding specific request headers.

Hereinafter is a sample showing how these header entries must be used if you need Digest authentication. The sample uses the old HttpWebRequest but you can rewrite the sample to use Windows.Web.HttpClient easily.

DigestFixer

 

Advertisements

Hamburger Menu with SplitView in Windows 10 Apps

Download Hamburger Sample Project

I created a sample project to demonstrate using the SplitView to create a hamburger menu for universal app (Windows 10 apps). I found two other samples online and took what I need from them.

Some features shown in this sample:

  • Displaying the back button in the title line
  • IsSelected property for navigation items changes the color
  • Showing an Options item command on bottom and displaying a OptionsControl (also SplitView)
  • Very basic and simple MVVM approach with no additional libraries.
  • Custom coloring through resources
  • Windows 10 mobile switch added by using VisualStateManager

Look at the screen shots

2015-11-17 (3)

Next one is showing the options fly out.

2015-11-17 (2)

This one is showing the back button…

2015-11-17 (4)

Mobile (small window) mode and different coloring

2015-11-18

Load Embedded Resource in Portable Class Libraries

Really difficult to find the right snipped…


var assembly = Assembly.Load(new AssemblyName("MyAssemblyName"));
using (var s = assembly.GetManifestResourceStream(@"FullNameSpaceName.MyFile.xml"))
{
using (StreamReader sr = new StreamReader(s))
{
string xml = sr.ReadToEnd();
XDocument x = XDocument.Parse(xml);
...

}
}

Windows Ribbon for WinForms in Visual Studio 2015

The same solution is still working for Visual Studio 2015. Please ensure that the registry keys must be changed to support VS 2015.

https://bernhardelbl.wordpress.com/2013/11/26/windows-ribbon-for-winforms-in-visual-studio-2013/

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 , , ,