Sunday, December 5, 2010

MS Tech ED 2010 Day 1, Tuesday November 9, 2010

Getting your Return on Investment with Microsoft .NET Framework 4.0

Speaker: Drew Robbins
Link: http://www.msteched.com/2010/Europe/DEV201
Timeslot: 09:00-10:00

Notes:

  • It’s possible to run .NET 4.0 side-by-side with older .NET frameworks. It’s also possible to install VS 2010 side-by-side with older Visual Studio versions.
  • With an application manifest file it’s possible to force existing .NET applications to use the new .NET Framework 4.0
  • Starting with the .NET Framework version 4, you can use in-process side-by-side hosting to run multiple versions of the common language runtime (CLR) in a single process. By default, managed COM components run with the .NET Framework version they were built with, regardless of the .NET Framework version that is loaded for the process.
  • Install Visual Studio 2010 trough the new Web Platform Installer: http://www.microsoft.com/web/ 
  • The .NET 4.0 Client profile has been reduced in download size:
    • 28 MB for x86 platform only
    • 41 MB for x86+X64 platforms
  • A new Enterprise Framework (version 4.0): http://msdn.microsoft.com/en-us/data/ef.aspx
  • Introduction of Managed Extensibility Framework (MEF)

Building Data Visualization Applications with WPF & Silverlight

Speaker: Tim Huckaby
Link: http://www.msteched.com/2010/Europe/WCL321
Timeslot: 10:30-11:30

Notes:

Seen a few interesting demos showing the power of WPF/SilverLight and the Microsoft Surface device:

Windows Azure Boot Camp, Part 1

Speaker: Brian Prince
Link: n.a.
Timeslot: 12:00-13:00

Notes:

  • If anyone wants to give a Windows Azure Boot Camp within his/her own company, take a look at: www.windowsazurebootcamp.com. The Windows Azure Boot Camp team is helpful in supplying training material and free hours on “the cloud” for training purposes.
  • The GreyBox application is designed to alert a user if their Windows Azure compute services are currently running, or are even simply deployed. Great for Azure speakers and POCs you don't want left running: http://greybox.codeplex.com

The Total Noob’s Guide Windows Workflow Foundation 4

Speaker: Max Knorr (http://www.knor.net)
Link: http://www.msteched.com/2010/Europe/DEV211
Timeslot: 14:30-15:30

Notes:

  • WF basically is a visual programming language.
  • Activity can be seen as a control
  • Activity consists of “work to be done” and data.
  • Learned something new along this demonstration about code-snippets with CTRL K,X in Visual Studio: http://saraford.net/2007/12/10/did-you-know-ctrlk-ctrlx-inserts-a-code-snippet-103/
  • Different kind of activities
    • CodeActivity
    • AsyncCodeActivity
    • NativeActivity
    • DynamicActivity
    • CodeActivity<TResult>
    • AsyncCodeActivity<TResult>
    • NativeActivity<TResult>
    • DynamicActivity<TResult>
  • Windows Workflow foundation can be hosted in AppFabric (which in turn is build on IIS 7).

Tips & Tricks: Visual Studio 2010 IDE & Extensions

Speaker: Chris Dias
Link: http://www.msteched.com/2010/Europe/DEV302
Timeslot: 16:30-17:30

Notes:

10 Things Every New Silverlight and WPF Developer Must Know

Speaker: Pete Brown
Link: http://www.msteched.com/2010/Europe/DEV205
Timeslot: 18:00-19:00

Notes:

  • Very little new stuff for me as a experienced WPF developer.

Tuesday, February 16, 2010

Using batchfile to setup environment for running .NET Process

Today I faced a problem that I wanted to use a third party library, which depends on all kind of environment settings. Since this library came with an accompanying batch file for setting up this environment, I would like to reuse this batch file. After much googling I could not find a satisfying solution for my problem and decided to go for my own. The resulting code is here

// Run 3 commands in a "batch":
// setenv.bat: this will correctly setup the environment
// CLS: Ths will clear the screen (effectively sends a formfeed (\f) to the StandardOutput stream)
// SET: This will display the current environment variables (or send it to standard output
var processStartInfo = new ProcessStartInfo("cmd.exe")
{
Arguments = string.Format("/c {0} && CLS && SET", pathSetEnv),
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false,
};
var process = Process.Start(processStartInfo);

// Wait until all commands have been processed
process.WaitForExit();

// Grab standard output
var standardOutput = process.StandardOutput.ReadToEnd();

// Grab all lines since last formfeed
var lastFormFeedIndex = standardOutput.LastIndexOf('\f');
var environmentSettings = standardOutput.Substring(
lastFormFeedIndex).Split(new [] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);

// Adjust the current Environment Variables with the ones grabbed
foreach (var environmentSetting in environmentSettings)
{
SetEnvironmentVar(environmentSetting);
}


With the following helper for setting the environment:



[DllImport("msvcrt.dll")]
public static extern int _putenv(string varName);

private static void SetEnvironmentVar(string environmentSetting)
{
// We can not use Environment.SetEnvironmentVariable(variable, value);
// The problem relates to an unbalanced environment string encoding (ANSI/UNICODE).
//
// To prevent such errors, pair the getenv() call with a _putenv call to set
// the environment variables in C#. When using SetEnvironmentVariable() (from
// Kernel32.dll) pair it with a GetEnvironmentVariable in C++ and match your
// string encodings.
//
// So what uou need to do is replace your SetEnvironmentVariable call with ...
// [DllImport( "msvcrt.dll")]
// public static extern int _putenv( string varName);

// Environment.SetEnvironmentVariable(variable, value);
if (_putenv(environmentSetting) == -1)
{
throw new TcliWrapperException("Unable to set environment variable");
}
}

Monday, October 26, 2009

XamlParseException when no AssemblyInfo.cs file available

In my project I have an exe and a dll assembly. The App.xaml in the exe references a ResourceDictionary in the dll assembly. This had worked for a long time, but all of a sudden it stopped working. The following very frustating error showed up during runtime:

System.Windows.Markup.XamlParseException: Could not load file or assembly 'MyAssembly.MyResource, Version=0.0.0.0, Culture=neutral, PublicKeyToken=1cf5186c6493e5b0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Error in markup file 'MyAssembly.MyResource;component/MyResource.xaml'. ---> System.IO.FileLoadException: Could not load file or assembly 'Pulsim.UnitConversions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=1cf5186c6493e5b0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Strange thing is, that my compiled MyAssembly gets version 1.0.0.125.

So what this error basically says is: “I’m not able to load my self, because the version is incorrect.”

After several attempts to resolve this issue, I finally got to my build procedure which is based upon msbuild. I use CommunityTasks in combination with an automatic generation of AssemblyInfo.cs. This way makes it possible to include the SVN revision number in the assembly and file version. How to do that can be found here: http://rod.blogsome.com/2008/02/21/compiling-solution-in-visual-studio-with-generating-project-version-based-on-svn-revision/.

That article states that the following addition (among others) must be made to the .csproj file.

   1: … cut …
   2: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   3: <Import Project="$(RootPath)\Common.proj" />

I finally switched line 2 and 3 in order to get the AssemblyInfo.cs file generated before the XAML parser/compiler kicks in. And guess what: it worked!

Summary: Be sure that an AssemblyInfo.cs file exists and is referenced within your project if you want to use XAML ResourceDictionaries!

Monday, July 13, 2009

Why are my WPF MenuItems grayed out

Well not all of them, but only those bound to WPF Commands.

After a 2 hour search, I finally found that a control that had focus, became invisible because it was not necessary anymore. Upon that event, my menuitems became grayed out (IsEnabled became false). Focussing a visible control on the screen resolved my issue.

So it seems that WPF Commands can not be executed when no focussed control is available.

Thursday, May 28, 2009

Databinding and LostFocus (HOWTO Force SourceUpdate for any WPF FrameworkElement)

Today I faced a bug that was not so easy to handle. While editing a value within a WPF textbox I pressed CTRL+S to save my work. Unfortunately my edited data was not saved, but the previous data was instead. How did that happen.

Well my WPF textbox was data bound to my dataobject. The dataobject (source) normally gets updated upon a Lostfocus of the textbox (that’s the default). However pressing CTRL+S (or even selecting the save menuitem under the file menu) does not result in the textbox losing focus.

OK. The short way to handle this problem is to get the binding expression of the textbox on de Text Property. Upon this binding expression it’s possible to call UpdateSource() explicitly and the value gets stored in the bound dataobject.

However I searched for a more generic approach because I do not have textboxes only. And it’s not always the Text Property that is being bound. So searching on the internet, I found a reaction on Nigel Spencer's blog: http://blog.spencen.com/2008/05/02/how-to-get-a-list-of-bindings-in-wpf.aspx#comment-1015806[^].

I modified the code a little bit to fit my needs and here is the result:

using System; 
using System.Windows;
using System.Windows.Data;
namespace MyNamespace
{
internal static class WPFHelper
{
internal static void ActionAllBindings(FrameworkElement targetElement, Action<BindingExpression> action)
{
WalkElementTree(targetElement, action);
}

private static void WalkElementTree(object obj, Action<BindingExpression> action)
{
var dependencyObject
= obj as FrameworkElement;
// Sometimes leaf nodes aren’t DependencyObjects (e.g. strings)
if (dependencyObject == null)
{
return;
}

// Recursive call for each logical child
ActionBindings(dependencyObject, action);
foreach (var child in LogicalTreeHelper.GetChildren(dependencyObject))
{
WalkElementTree(child, action);
}
}

private static void ActionBindings(DependencyObject target, Action<BindingExpression> action)
{
var localValueEnumerator
= target.GetLocalValueEnumerator();
while (localValueEnumerator.MoveNext())
{
var current
= localValueEnumerator.Current;
var binding
= BindingOperations.GetBindingExpression(target, current.Property);
if (binding != null)
{
action(binding);
}
}
}
}
}


Using the helper is extremely simple:



public void ForceSourceUpdateActiveElement()    
{
var focusedElement
= Keyboard.FocusedElement as FrameworkElement;
if (focusedElement != null)
{
MyNamespace.ActionAllBindings(focusedElement, bindingExpression
=> bindingExpression.UpdateSource());
}
}

Friday, January 25, 2008

IList is not serializable in combination with XmlSerializer

Because IList<T> does not implement ICollection (but the generic ICollection<T> instead), the XMLSerializer is not able to serialize the items within the collection. More info on this "bug" can be found here:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=105767

Friday, January 4, 2008

Implementing a custom Dispose for a form or usercontrol

Have you ever wanted to implement a custom dispose method for a usercontrol or a form. Trying to override protected void dispose(bool disposing) learns that the designer generated file (mycontrol.designer.cs) already implements this override.

Mark Seemann wrote an intersting approach for dealing with this situation in his blog. It makes use of an Disposer component. A new instance will be created with a reference to a method implementing the custom dispose functionality. This instance is being added to the components collection. When the components get disposed, the this Disposer calls the delegate function with custom dispose logic. That's it.

Thanks Mark for this article.