SOLUTION FOUND: SHOW STOPPER: Windows application cannot call the RibbonWindow - Error

Jul 7, 2010 at 10:31 PM

Windows applications should be able to load the Fluent RibbonWindow.  For example, create a simple Windows Form Application, and create a button.  When you click the button the following error appears:

         private void button1_Click(object sender, EventArgs e)
        {
            Fluent.Sample.KeyTips.Window w = new Fluent.Sample.KeyTips.Window();
            w.ShowDialog();
        }

Cannot create instance of 'RibbonTabItem' defined in assembly 'Fluent, Version=1.3.0.0, Culture=neutral, PublicKeyToken=3e436e32a8c5546f'. Exception has been thrown by the target of an invocation.  Error at object 'Fluent.Ribbon' in markup file 'Fluent.Sample.KeyTips;component/window.xaml' Line 31 Position 12.
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

A sample application based on the KeyTips application is attached.  On Debug, this error occurs within the Fluent.dll.  This is a real show stopper for deploying our application.  Is there a workaround or can this be fixed.  A common System.Windows.Window has this capability.  Without this feature we cannot deploy.  Thank you.

Coordinator
Jul 9, 2010 at 7:31 AM

The styles of Fluent.Sample.KeyTips.Window are attached in Application.xaml. In such case you have to modify Fluent.Sample.KeyTips.Window to attach styles in xaml of the window.

Is it helpful?
Daniel

Jul 9, 2010 at 5:02 PM

No this does not resolve the issue.  We added the following to the window.xaml (blue, black or silver does not matter):

<Fluent:Ribbon.Resources>
                <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Black.xaml" />
</Fluent:Ribbon.Resources>

 

The error occurs in both the 3.5 and 4.0 versions of Fluent.dll 51427 when calling OnCoerceStyle from RibbonTabItem.cs.  The basevalue is null and the attempt to access resource style throws the error.  This is the error:

 

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Cannot create instance of 'RibbonTabItem' defined in assembly 'Fluent, Version=1.3.0.0, Culture=neutral, PublicKeyToken=3e436e32a8c5546f'. Exception has been thrown by the target of an invocation.  Error at object 'Fluent.Ribbon' in markup file 'Fluent.Sample.KeyTips;component/window.xaml' Line 34 Position 12.

 

 

 

 

 static object OnCoerceStyle(DependencyObject d, object basevalue)
        {
            if (basevalue == null)
            {

                basevalue = ((FrameworkElement)d).Resources["RibbonTabItemStyle"] as Style ??
                              Application.Current.Resources["RibbonTabItemStyle"] as Style;
            }

            return basevalue;
        }
Jul 9, 2010 at 5:28 PM

Additional information.  This error also occurs in RibbonWindow.cs in OnCoerceStyle function.  If you bypass the code, the window loads.  However, the ResourceDictionary is not attached to the window and therefore does not display the ribbon or other components.  Any help to get this working would be appreciated.  Thanks Kristi

Jul 9, 2010 at 8:32 PM

We found a workaround.  When loading the WPF Fluent Ribbon application using the declaration:

Fluent.Sample.KeyTips.Window w = new Fluent.Sample.KeyTips.Window()

w.ShowDialog()

The application-level resources do not get created.  If by default the implementation of the ResourceDictionary/Theme is in the application level.  By calling w.ShowDialog() the application resource is not created and not available during the InitializeComponent process of the window.  This causes the error. 

Alternatively, when the ResourceDictionary/Theme is loaded as a part of the xaml window, the ResourceDictionary/Theme is also un-initialized/unavailable when during the InitializeComponent process and again the error occurs.

A solution, based on an article from Dr. WPF - Managing Application Resources when WPF is Hosted, helped to resolve this issue.  While the article is not really on-point, it does have good tips and an explaination of why ResourceDictionary elements are not available.  (http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/).

Following the article, to ensure that the Application level Resources are available, we modified the Window.cs to include the EnsureApplicationResources() function and called the function before InitializeComponent().  Please note:  LoadComponent cannot accept the pack attribute "pack://application:,,,"  This was removed to allow the component to load.


    public partial class Window : RibbonWindow
    {
        /// <summary>        /// Default constructor
        /// </summary>
        public Window()
        {
            EnsureApplicationResources();
            InitializeComponent();
        }

        public static void EnsureApplicationResources()
        {
            if (Application.Current == null)
            {
                new Application();
                Application.Current.Resources.MergedDictionaries.Add (Application.LoadComponent (new Uri ("/Fluent;Component/Themes/Office2010/Black.xaml", UriKind.RelativeOrAbsolute   )) as System.Windows.ResourceDictionary );
            }
        }


Coordinator
Jul 10, 2010 at 3:41 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.