Avoiding Visual Studio designer errors when WPF resource is defined in separate project

梦想的初衷 提交于 2019-11-28 06:45:58

You could create your own ResourceDictionary class, inheriting from ResourceDictionary. Then you can arrange that at design-time this custom ResourceDictionary loads some explicitly defined styles (i.e. those loaded from the app at runtime), whereas at runtime it does nothing at all. The IsInDesignMode-Property could be evaluated for this.

Say you have such a class, called 'DesignTimeResourceDictionary', then you just use s.th. like

 <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <Util:DesignTimeResourceDictionary Source="SomeUriToYourResources"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
 </UserControl.Resources>

to load your resources at design-time and make the designer work. At Runtime you can then make your DesignTimeResourceDictionary skip the loading of resources and achieve the desired behavior.

If you need, you could really create a copy of the real resources for this, or you can just create a dummy dictionary containing all the keys you need to keep the designer working.

M. Dudley

One possible solution is to use DynamicResource rather than StaticResource. The Visual Studio 2008 designer simply displays the controls without any styling, like VS2010 beta 1 does when it cannot resolve a StaticResource.

Using DynamicResource is appropriate in situations where a particular style may change at runtime, like when skinning.

I found some related questions supporting this:

I also found someone who states that DynamicResource should be used whenever a resource is not local:

I just want to extend Simon D. answer. What he is proposing is the solution that i am using right now. I just wanted to share complete source code. It is from this Trick To Use A ResourceDictionary Only When In Design Mode source.

public class DesignTimeResourceDictionary : ResourceDictionary
{
    /// <summary>
    /// Local field storing info about designtime source.
    /// </summary>
    private string designTimeSource;

    /// <summary>
    /// Gets or sets the design time source.
    /// </summary>
    /// <value>
    /// The design time source.
    /// </value>
    public string DesignTimeSource
    {
        get
        {
            return this.designTimeSource;
        }

        set
        {
            this.designTimeSource = value;
            if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
            {
                base.Source = new Uri(designTimeSource);
            }
        }
    }

    /// <summary>
    /// Gets or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    /// <returns>The source location of an external resource dictionary. </returns>
    public new Uri Source
    {
        get
        {
            throw new Exception("Use DesignTimeSource instead Source!");
        }

        set
        {
            throw new Exception("Use DesignTimeSource instead Source!");
        }
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation Jump "
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml Jump "
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:WpfApplication1">

  <Window.Resources>
    <local:DesignTimeResourceDictionary DesignTimeSource="pack://application:,,,/BlueColors.xaml"/>
  </Window.Resources>

    <Grid>
      <Button Background="{DynamicResource defaultBackground}"
      HorizontalAlignment="Center" VerticalAlignment="Center">click me</Button>
    </Grid>
</Window>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!