Montag, 8. September 2008

Workaround to avoid WPF Designer Error in VS 2008 SP1

If you are working with the WPF designer of Visual Studio 2008 SP1 you might get the following error message:


Object reference not set to an instance of an object.
at MS.Internal.Package.VSIsolationProviderService.CreateIsolationProvider(String identity, AssemblyReferenceProvider assemblyReferences, IEnumerable`1 assemblyFolders)
at MS.Internal.Providers.VSDesignerContext.GetIsolationProvider(IServiceProvider provider, IVsHierarchy hierarchy, AssemblyReferenceProvider assemblyReferences)
at MS.Internal.Providers.VSDesignerContext.Initialize(IServiceProvider provider, IVsHierarchy hierarchy, UInt32 itemid, Object docDataObj)


If you get this error, make sure your startup project is a managed .NET project. The WPF designer seems to have a problem with C++ projects selected as startup project of your solution.
You can still start the win32 application for debugging by configuring the debug options of your managed .NET project ("Start external program" setting).

Donnerstag, 4. September 2008

Using Application resources within Win32 hosted WPF application

Hosting a WPF application within a Win32 application can be very cumbersome when it comes to application wide resources. By default there is no Application.Current set when a WPF application is hosted and the dispatcher thread is part of the Win32 world. In an article of Dr. WPF he describes, how such an Application.Current can be created by your own.

His method works very well but when it comes to replace a merged dictionary (e.g. for doing some kind of “skin” change during runtime), the following tweak seems to be required:

// This window is required that Resource changes (like new skin)
// are propagated to all windows and controls
var window = new Window();
window.Content = shell;

IntPtr hwndParent = new IntPtr(1); // Here you need the window handle of the parent window

HwndSourceParameters parameters = new HwndSourceParameters();
parameters.WindowStyle = (int)(WS_VISIBLE | WS_CHILD);
parameters.ParentWindow = hwndParent;

hwndSource = new HwndSource(parameters);
hwndSource.RootVisual = shell;
hwndSource.SizeToContent = System.Windows.SizeToContent.Manual;