Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Custom RoutedUICommand in xaml throws exception

Tags:

c#

wpf

xaml

I have followed this article and some others to create a Custom RoutedUICommand. I am using Infragistics Ribbon, but I dont think the problem comes from there.

<igRibbon:XamRibbonWindow x:Class="MyRibbonWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:igRibbon="http://infragistics.com/Ribbon"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:MyNamespaceTag="clr-namespace:MyNamespace"
xmlns:igWPF="http://schemas.infragistics.com/xaml/wpf" mc:Ignorable="d" 
WindowState="Maximized">    
<igRibbon:XamRibbonWindow.Resources>
<!-- Some DataTempaltes here -->
</igRibbon:XamRibbonWindow.Resources>
<igRibbon:RibbonWindowContentHost x:Name="ribbonWindowContentHost" >
  <igRibbon:RibbonWindowContentHost.Ribbon>
    <igRibbon:XamRibbon x:Name="xamRibbon" Theme="[current]">
      <igRibbon:XamRibbon.ApplicationMenu>
        <igRibbon:ApplicationMenu>
          <igRibbon:ApplicationMenu.FooterToolbar>
            <igRibbon:ApplicationMenuFooterToolbar>
              <igRibbon:ButtonTool Name="appMenuOptions" Caption="Opt_ions"/>
            </igRibbon:ApplicationMenuFooterToolbar>
           </igRibbon:ApplicationMenu.FooterToolbar>
      </igRibbon:ApplicationMenu>
    </igRibbon:XamRibbon.ApplicationMenu>
   </igRibbon:XamRibbon>
  </igRibbon:RibbonWindowContentHost.Ribbon>       
  <ContentControl Content="{Binding MyContent}"
      ContentTemplateSelector="{StaticResource myContentTemplateSelector}" />       
</igRibbon:RibbonWindowContentHost>    
</igRibbon:XamRibbonWindow>

We needed to change the look and feel of the Infragistics Ribbon, and a team member from Infragistics comunity suggested an Style to be added. The problem is that when we added it to the Resources section of the Ribbon, it didn't work. So we were forced to do the following:

var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri(
    @"/MyNamespace;component/Resources/RibbonResources.xaml", 
    UriKind.RelativeOrAbsolute);
xamRibbon.Resources.MergedDictionaries.Add(resourceDictionary);

This applies correctly the styles in the ribbon, but in that RibbonResources.xaml we have a button:

<Button Name="myButton" 
    Command="MyNamespaceTag:MyCommands.MyCommand" 
    Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    <Image Source="MyImage.png" Width="16" Height="16" />
</Button>

The MyCommand and MyCommands class, have been created exactly as the article i mentioned. But when I run the application I get the following error:

XamlParseException: Failed to create a 'Command' from the text MyNamespace:MyCommands.MyCommand' InnerException: Type reference cannot find type named '{clr-namespace:MyNamespace}MyCommands'.

If I use the command like this instead:

Command="{x:Static MyNamespaceTag:MyCommands.MyCommand}"

I get the following exception:

XamlParseException: Provide value on 'System.Windows.Markup.StaticExtension' threw an exception. InnerException: Type reference cannot find type named '{clr-namespace:MyNamespaces}MyCommands'.

In the code-behind we are binding the command like this:

CommandBindings.Add(
    new CommandBinding(
        MyCommands.MyCommand, 
        Show, 
        CanShow));

If we put the button directly where we have the XamRibbonWindow, it works fine.

I am still pretty new with WPF, and I can't figure out what I am doing wrong.

like image 490
Dzyann Avatar asked Mar 15 '13 19:03

Dzyann


1 Answers

You're on the right path with MyNamespace:MyCommands.MyCommand. It looks like you are including MyNamespace in your xaml file as "Shell." At the top, in the igRibbon:XamRibbonWindow tag, you should include an xmlns:MyNamespace=[whatever] attribute, or alternately, change the command from

MyNamespace:MyCommands.MyCommand

to

Shell:MyCommands.MyCommand.

like image 173
Steve Westbrook Avatar answered Nov 03 '22 13:11

Steve Westbrook