locked
Using StaticResource in this situation cause Blend raise error RRS feed

  • Question

  • Guys,
    I don't know if it's a known-issue or it just happens on my machine. please advise.

    Re-produce situation:

    1. Create Style1 in ResourceDictionary1.xaml. Style can be anything, just simple like change Background color of button.

    2. Merge ResourceDictionary1.xaml into App.xaml.

    3. Create a usercontrol that contains a button and apply Style1 to that button. Use Style={StaticResource Style1}

    4. Add this UserControl to Window1.xaml.

    Run the project, everything is OK, button get styled as applied.

    Open UserControl in DesignView: OK.

    Open project in Blend,  error raised and could not render windows1 because Style1 not found.

    Change reference StaticResource to DynamicResource, everything is solved.

     I trapped into this situation when trying to change every DynamicResource to StaticResource in my project to speed up performance of my WPF app but this caused my pages cannot open in Design View.



    Duy T. Doan
    Thursday, May 29, 2008 9:07 AM

All replies

  • I am experiencing this same issue.  Blend uses dynamicresources by default, and when I convert them to static resources, they elements will no longer render in blend.  However, everywhere I'm reading says that one should use staticResources whenever possible because dynamic resources will turn your application into a dog...  did you find an answer to your question?
    Purkiss
    Friday, November 27, 2009 9:19 PM
  • Hi,
          This is a known issue and unfortuantely we have no work around for this but to suggest using Dynamic Resources, please check Unni's blog for more information. Link: http://blogs.msdn.com/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx.

    We will defintiely try to make the design time experience of using static resources better in future releases of Blend.

    Thanks for your feedback.

    Regards,
    Navit Saxena
    • Proposed as answer by Purkiss Monday, November 30, 2009 5:09 PM
    Sunday, November 29, 2009 10:33 AM
  • Could you not create a extension say {OptimizedResource ..}  that uses StaticResource when !InDesignMode and DynamicResource when InDesignMode ??

    e.g.

        public class OptimizedResourceExtension : MarkupExtension

        {

            private MarkupExtension _Target = null

     

            // Summary:

            //     Initializes a new instance of the OptimizedResourceExtension

            //     class.

            public OptimizedResourceExtension()

            {

                MakeExtension();

            }

            //

            // Summary:

            //     Initializes a new instance of the OptimizedResourceExtension

            //     class, with the provided initial key.

            //

            // Parameters:

            //   resourceKey:

            //     The key of the resource that this markup extension references.

            public OptimizedResourceExtension(object resourceKey)

            {

                MakeExtension();

                ResourceKey = resourceKey;

            }

     

            protected void MakeExtension()

            {

                if (IsInDesignMode)

                {

                    _Target = new DynamicResourceExtension();

                }

                else

                {

                    _Target = new StaticResourceExtension();

                }

            }

     

            #region IsInDesignMode

     

            private static bool? _isInDesignMode;

            public static bool IsInDesignMode

            {

                get

                {

                    if (!_isInDesignMode.HasValue)

                    {

    #if SILVERLIGHT 

                        _isInDesignMode = DesignerProperties.IsInDesignTool; 

    #else

     

                        var prop = DesignerProperties.IsInDesignModeProperty;

     

                        _isInDesignMode = (bool)DependencyPropertyDescriptor

                            .FromProperty(prop, typeof(FrameworkElement))

                                .Metadata.DefaultValue;

    #endif

                    }

                    return _isInDesignMode.Value;

                }

            }

     

            #endregion // IsInDesignMode

     

            // Summary:

            //     Gets or sets the key value passed by this static resource reference. They

            //     key is used to return the object matching that key in resource dictionaries.

            //

            // Returns:

            //     The resource key for a resource.

            [ConstructorArgument("resourceKey")]

            public object ResourceKey

            {

                get

                {

                    object retKey = null;

                    if (_Target != null)

                    {

                        if (_Target is StaticResourceExtension)

                        {

                            retKey = (_Target as StaticResourceExtension).ResourceKey;

                        }

                        if(_Target is DynamicResourceExtension)

                        {

                            retKey = (_Target as DynamicResourceExtension).ResourceKey;

                        }

                    }

     

                    return retKey;

                }

                set

                {

                    if (_Target != null)

                    {

                        if (_Target is StaticResourceExtension)

                        {

                            (_Target as StaticResourceExtension).ResourceKey = value;

                        }

                        if (_Target is DynamicResourceExtension)

                        {

                            (_Target as DynamicResourceExtension).ResourceKey = value;

                        }

                    }

                }

            }

     

            public override object ProvideValue(IServiceProvider serviceProvider)

            {

                object oRet = null;

     

                if (_Target != null)

                {

                    oRet = _Target.ProvideValue(serviceProvider);

                }

     

                return oRet;

            }

        }

     

    • Edited by anagram4wander Thursday, December 17, 2009 12:30 AM Added code
    Wednesday, December 16, 2009 11:53 PM
  • Hi,
          This is a known issue and unfortuantely we have no work around for this but to suggest using Dynamic Resources, please check Unni's blog for more information. Link: http://blogs.msdn.com/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx.

    We will defintiely try to make the design time experience of using static resources better in future releases of Blend.

    Thanks for your feedback.

    Regards,
    Navit Saxena

    This is still an issue ... 3 years later.
    Friday, December 7, 2012 1:11 PM