HoverProblem in WPF

by Geert 15. December 2010 15:02

Update: this issue is now posted to Microsoft Connect. Please vote for it.

We are working on quite a large problem, and had some users complaining that their windows disappeared as soon as they hovered a popup. But since the application is very big, and the users couldn't tell nor reproduce the problem, we were just ignoring them.

But then, a strange combination of user input actions made it happen at our place as well. This meant that we had to start a thorough investigation, but we couldn't get it reproduced. Until the day that I danced 3 times around my house during full moon, which is today (just kidding for the ones that don't understand sarcasm).

Here is a video showing the issue:

I also mad a demo application that shows the hovering bug that we encounter in WPF. I am not sure about the exact cause of the problem, but it seems to be a combination of a non-modal popup window and the web browser.

Problem description

When using the webbrowser control to load a PDF document, we found a very rare bug which was extremely hard to reproduce. However, we finally managed a way to reproduce the issue and hope that Microsoft (or someone else) can give us a clue in the right direction (or simply fix the bug).

How to reproduce

The demo application needs an existing PDF file as input. Then, there are several options to run the test. If you disable either one of them, you will notice that the problem will not occur. As soon as the combination of the PDF control and the PleaseWaitWindow is used, the problem occurs (and then durates for the rest of the application instance).

Once you enabled both checkboxes, and hit the "Run the test" button, you will notice that as soon as you hover the yellow bar at the top (InfoBarMessageControl, uses a popup to show error messages) or the combobox (uses a popup to show the items), the whole window except the popup will disappear.

How to "fix"

Put a breakpoint in the PleaseWaitHelper.Hide() method. You will see that the problem will not occur then, but that is of course not something we can do in a real-world scenario.

hoverproblem.zip (966.36 kb) [Downloads: 138]

kick it on DotNetKicks.com

Shows a reproducible bug in WPF when windows start disappearing as soon as you open a popup.

Tags:

C# Development | WPF

Getting a private field of a base class using reflection

by Geert 10. September 2010 16:05

Today, I needed a way to get a private field from a type (just for reading only, but since the designer of the class has no functionality to retrieve it). Anyway, I though to be smart by using this code:

 

FieldInfo myPrivateField = myType.GetField(“myPrivateFieldName”, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

 

Unfortunately, Microsoft “wisely” decided not to allow BindingFlags.FlattenHierarchy to be used on non-public fields declared in base classes. Because of this “wise” decision, I had to write the functionality myself, and lost another hour of valuable time. To spare you this time, here is the method. It recursively checks the base class for the private field:

 

/// 
/// Gets the field info for a specific field. But, the good thing about this is that it also supports
///  for private members in base classes.
/// 
/// The type.
/// Name of the field.
/// The binding flags.
///  or null if the field is not found.
public static FieldInfo GetFieldAndAllowPrivateBaseMembers(this Type type, string fieldName, BindingFlags bindingFlags)
{
	// Declare variables
	FieldInfo fieldInfo = null;
	bool flattenHierarchy = Enum.Flags.IsFlagSet(bindingFlags, BindingFlags.FlattenHierarchy);

	// Clear flatten hierarchy flag if included
	if (flattenHierarchy)
	{
		bindingFlags = Enum.Flags.ClearFlag(bindingFlags, BindingFlags.FlattenHierarchy);
	}

	// Search as long as there is a type
	while (type != null)
	{
		// Use reflection
		fieldInfo = type.GetField(fieldName, bindingFlags);

		// Should we flatten the hierarchy?)
		if (flattenHierarchy)
		{
			// Yes, do we have a field?
			if (fieldInfo != null) break;

			// Get base class
			type = type.BaseType;
		}
		else
		{
			// Break
			break;
		}
	}

	// Return result
	return fieldInfo;
}

 

Of course, I will also include this in Catel.

kick it on DotNetKicks.com

Method to get private fields of base classes

Tags:

C# Development

About the Author

Geert van Horrik is a independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!