cverdon

Hi,

I'm trying to have commands execute automatically on a background thread by implementing an AsyncCommandBinding which inherits from CommandBinding.

The problem that I have is that the xaml parser seems does not behave the same when parsing my AsyncCommandBinding than it does for CommandBindings.

Normal code you can find on msdn:
<Window.CommandBindings>
<CommandBinding Command="{x: Static local:Window1.Command1}" Executed="Command1_Executed" CanExecute="Command1_CanExecute"/>
</Window.CommandBindings>


My code:
<Window.CommandBindings>
<local:AsyncCommandBinding Command="{x: Static local:Window1.Command1}" Executed="Command1_Executed" CanExecute="Command1_CanExecute"/>
</Window.CommandBindings>

The project builds, but this results in a XamlParseException:

'Command1_Executed' value cannot be assigned to property 'Executed' of object 'CommandTest.AsyncCommandBinding'. Error binding to target method. Error at object 'CommandTest.AsyncCommandBinding' in markup file...

Of course, if I add the binding with code there is no problem and everything works fine:
CommandBindings.Add(new AsyncCommandBinding(Command1, Command1_Executed, Command1_CanExecute));

Is there any way to tell the parser to behave the same way for my custom class than for CommandBinding or is it this behavior hard-coded

Thanks for your help,
Charles


Re: Windows Presentation Foundation (WPF) Custom CommandBinding & XamlParseException?

Yi-Lun Luo - MSFT

Hello, does your AsyncCommandBinding derive from CommandBinding I¡¯ve made a simple test. I created an empty class derives from CommandBinding, and used your syntax in XAML. There¡¯s no error. Can you post some code of the AsyncCommandBinding






Re: Windows Presentation Foundation (WPF) Custom CommandBinding & XamlParseException?

cverdon

Hi, thanks for your help.
Sorry for the delay, I just got the msdn alert right now!

I'm using the VPC of Orcas beta 1.

Here's the code: (btw if there is a better way to do this let me know)

public class AsyncCommandBinding : CommandBinding

{

#region Private Fields

ExecutedRoutedEventHandler m_executed;

BackgroundWorker m_worker;

object m_currentRequestSender;

ExecutedRoutedEventArgs m_currentRequestArgs;

#endregion

#region Constructor

public AsyncCommandBinding()

{

base.Executed += InterceptedExecute;

}

public AsyncCommandBinding(ICommand command) : base(command)

{

base.Executed += InterceptedExecute;

}

public AsyncCommandBinding(ICommand command, ExecutedRoutedEventHandler executed)

: base(command)

{

m_executed = executed;

base.Executed += InterceptedExecute;

}

public AsyncCommandBinding(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute)

: base(command)

{

m_executed = executed;

base.CanExecute += canExecute;

base.Executed += InterceptedExecute;

}

#endregion

private void InterceptedExecute(object sender, ExecutedRoutedEventArgs args)

{

if (m_worker == null) {

m_worker = new BackgroundWorker();

m_worker.DoWork += new DoWorkEventHandler(m_worker_DoWork);

m_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_worker_RunWorkerCompleted);

}

if (m_worker.IsBusy) {

//todo: add to queue

}

else {

m_currentRequestSender = sender;

m_currentRequestArgs = args;

m_worker.RunWorkerAsync();

}

}

void m_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

//todo: verify queue

}

void m_worker_DoWork(object sender, DoWorkEventArgs args)

{

m_executed(m_currentRequestSender, m_currentRequestArgs);

}

}


Charles




Re: Windows Presentation Foundation (WPF) Custom CommandBinding & XamlParseException?

Majulae

I experienced something very similar to this today.

I have a UserControl that I register a RoutedEvent to and then after dropping the user control onto another Window, I attach an eventhandler to this routed event. It has always worked fine in every situation I have used it in until today when after attempting to attach an eventhandler I got the exact same error message that you have described in your post.

After googling and reading here on these forums I heard that simply recreating the project and running the same code would work, which I tried and it did. My thought was that it was definitely something in the project file, and so after doing a side by side comparison noticed that the only real difference worth noting is that somehow the project that started having the bug had picked up an additional reference to a different .dll called PresentationDeveloper...something or other, I wish I had saved the text before deleting it and running it to see that it worked but unfortunately I didn't. Anyway, after deleting that reference the old project worked without the bug.

I hope that helps someone figure out what was wrong. I don't have a way of recovering what the project file used to look like to actually pin down which .dll was being referenced.

~Bob Pace





Re: Windows Presentation Foundation (WPF) Custom CommandBinding & XamlParseException?

cverdon

Hi,

I didn't work for me...

I created a new blank project (.net 3.0 instead of 3.5 like the previous one) and added just the code necessary to make it compile and I have the same message. I then removed all references, but it turns out that they were all needed. (There wasn't any PresentatonDeveloper...)

Regards,

Charles