如何在每个XAML文件中没有合并的ResourceDictionary的Blend/VS中获得WYSIWYG设计?

and*_*mar 8 wpf expression-blend

我刚刚为我删除了一个大内存问题,我曾经在每个xaml文件中合并我们的"Themes"资源字典,而不是仅仅在app.cs.xaml中.

但是,在删除除App.cs.xaml之外的每个文件中的合并之后,我丢失了设计时样式/模板.

请注意:这仅适用于合并到我们的Themes.xaml中的样式(例如Color.xaml,Brushes.xaml - 我们为每种类型的样式都有一个).直接在Themes.xaml(我们没有..)中定义的东西有效.

我看到两个解决方案,

1)在XAML中注释掉合并,当我想使用设计时,只需取消注释.

2)在每个控件的默认控制器中都有这个:(也许只适用于Blend)

#if DEBUG
Resources.MergedDictionaries.Add(
                new ResourceDictionary()
                {
                    Source = new System.Uri(@"RD.xml")
                }
                );
#endif
Run Code Online (Sandbox Code Playgroud)

任何人都知道,必须有更好的方法来设计页面和控件的设计时间?

谢谢!

sco*_*obi 10

Blend 4支持Visual Studio 2010也支持的"设计时资源".见http://adamkinney.wordpress.com/2010/05/04/design-time-resources-in-expression-blend-4-rc/.

它几乎只是一个包含你喜欢的MergedDictionaries的ResourceDictionary,并且在项目文件中显示如下(由Blend自动添加):

<Page Include="Properties\DesignTimeResources.xaml" Condition="'$(DesignTime)'=='true' OR ('$(SolutionPath)'!='' AND Exists('$(SolutionPath)') AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true')">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
  <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>
Run Code Online (Sandbox Code Playgroud)

效果很好.

  • 这是本页面上最好的答案.谢谢,一个巨大的帮助. (3认同)

Bra*_*ham 8

我所做的是添加一个继承自ResourceDictionary的类并覆盖source属性以检查IsInDesignMode是否为true.

如果是我设置源,否则我将源保留为空(这有效阻止字典在运行时合并)

public class BlendMergedDictionary : ResourceDictionary
{
    public bool IsInDesignMode
    {
        get
        {
            return (bool)DependencyPropertyDescriptor.FromProperty(
                            DesignerProperties.IsInDesignModeProperty,
                            typeof(DependencyObject)
                            ).Metadata.DefaultValue;
        }
    }

    public new Uri Source
    {
        get { return base.Source; }
        set
        {
            if (!IsInDesignMode)
                return;

            Debug.WriteLine("Setting Source = " + value);
            base.Source = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我需要在Blend中引用字典时,我会像这样合并字典

<ResourceDictionary.MergedDictionaries>
            <BlendHelpers:BlendMergedDictionary Source="Foo.xaml" />
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)

您仍然必须在每个文件的字典中"合并",但您不需要支付在运行时实际加载字典的代价.合并仅用于支持设计时行为.