Archive

Posts Tagged ‘WPF’

MSBuild fails to compile WPF with: error MC2000: Unkown build error ‘…’ does not have an implementation

June 14th, 2010 No comments

My initial solution in this post was WRONG, it just changed the compile order of our build which made the error occur later after other issues were resolved. I’ve updated the post accordingly. I’m sorry if anyone had to read this post twice.

We have been struggling with a build error lately, where the compilation works perfectly in Visual Studio, and in our case it also works in some MSBuild builds but not in this specific build. By that I mean that two builds are compiling the same code, with the same dependencies and compilation flags, yet one fails and the other doesn’t.

The error message given is:

D:\Invoicing\UQList.xaml(55,167): error MC2000: Unknown build error, 'Method 'add_PropertyChanged' in type 'UsageQuantityPresenter' from assembly 'ServiceManagementApplication' does not have an implementation. Line 55 Position 167.' 

If we look at the buildlog.txt produced by MSBuild in the failing build, we can find something similar to this:

  Input file 'D:\Invoicing\UQList.xaml' is resolved to new relative path 'Invoicing\UQList.xaml' at directory 'D:\'.
D:\Invoicing\UQList.xaml(55,167): error MC2000: Unknown build error, 'Method 'add_PropertyChanged' in type 'UsageQuantityPresenter' from assembly 'ServiceManagementApplication' does not have an implementation. Line 55 Position 167.'
  Input file 'D:\Trait\TraitsView.xaml' is resolved to new relative path 'Trait\TraitsView.xaml' at directory 'D:\'.
...
  Generated BAML file: 'D:\obj\Release\Themes\luna.normalcolor.baml'.
  Generated BAML file: 'D:\obj\Release\Trait\TraitsView.baml'.
  Markup compilation is done.
Done executing task "MarkupCompilePass2" -- FAILED.
Done building target "MarkupCompilePass2" in project "ServiceMangementApplication.csproj" -- FAILED.

In the XAML we are depending on the UsageQuantityPresenter to implement INotifyPropertyChanged, which it does. However it also implements other interfaces and does inheritance of a base-class.

This is what it looked for for us:

/// <summary>
/// UsageQuantityPresenter
/// </summary>
public class UsageQuantityPresenter : EntityViewPresenterBase, IUsageQuantity
{
...

The EntityViewPresenterBase (I hate that class name by the way) implements INotifyPropertyChanged.

The solution was found in the following forum tread: http://social.msdn.microsoft.com/Forums/en/wpf/thread/00907c94-c6b2-4bf1-98f9-113c5c4392d8

It states that you should add:

<AlwaysCompileMarkupFilesInSeparateDomain>true</AlwaysCompileMarkupFilesInSeparateDomain>

to the .csproj file with the XAML files. Doing this makes MSBuild happy again.

WPF Command execution and click event execution on buttons

August 18th, 2009 2 comments

I had to fix a bug today that was caused by the order of which a command and a click event was executed on a WPF button. I found no documentation of it so I thought I’d make a note of it.

If you have  a WPF button like so:

<Button Command="{Binding Clear}" Click="ClearClicked" "></Button>

Then the click event gets executed first and the command is executed after.

Thus you can have the command as a separation layer between the ui and the presentation model. Having the command make the changes on the presentation layer, and have the click event handler to handle how the UI reacts to pressing the button.

If you for some reason need to have the UI execute after the command is executed, then I suggest a command class like the following:

using System;

 /// <summary>
 /// An ObservableCommand is a command where the execution of the command can be monitoried and thus acted upon;
 /// </summary>
 public abstract class ObservableCommand : ICommand
 {

 ///<summary>
 /// The event fired when a command is executed;
 ///</summary>
 public event EventHandler CommandExecuted = delegate { };

 /// <summary>
 /// Defines the method to be called when the command is invoked.
 /// </summary>
 /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.
 ///                 </param>
 public void Execute( object parameter )
 {
 ExecuteCommand( parameter );
 CommandExecuted(this, EventArgs.Empty);
 }

 protected abstract void ExecuteCommand( object parameter );

 /// <summary>
 /// Defines the method that determines whether the command can execute in its current state.
 /// </summary>
 /// <returns>
 /// true if this command can be executed; otherwise, false.
 /// </returns>
 /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.
 ///                 </param>
 public virtual bool CanExecute( object parameter )
 {
 return true;
 }

 protected void RaiseCanExecuteChanged()
 {
 CanExecuteChanged( this, EventArgs.Empty );
 }

 /// <summary>
 /// Occurs when changes occur that affect whether or not the command should execute.
 /// </summary>
 public event EventHandler CanExecuteChanged = delegate { };
 }

 

So how to use this class?

First of, make sure your command class inherits from it like so:

	///

	/// The save command
	/// 

	internal class SaveCommand : ObservableCommand

Then hook on the CommandExecuted event to do your UI action accordingly:

Save.CommandExecuted += Save;
...
void Save( object sender, EventArgs e1 )
{
	_textBox.AddText(string.Empty);
	_textBox.Focus();
}

To use this command in WPF is just like using any other command.

<button command="{Binding Save}" ...>
Tags:

Left aligning wpf grid headers

March 25th, 2009 No comments

Applying this style to the gridviewcolumns they will left align their header content.

<Style x:Key=”LeftAlignedGridHeader” TargetType=”{x:Type GridViewColumnHeader}”>
<Setter Property=”HorizontalContentAlignment” Value=”Left” />
</Style>

Tags: ,