【问题标题】:Load resource dictionary at design time when using SDK format in project在项目中使用 SDK 格式时在设计时加载资源字典
【发布时间】:2019-12-19 13:11:12
【问题描述】:

我目前正在将我的项目转换为 Visual Studio 的新 SDK 格式。不幸的是,我无法让设计师工作。过去,我在每个项目中添加了一个 app.xaml,其中包含所有与设计时相关的资源。这会导致一些警告,但 Visual Studio 给了我我需要的东西:XAML 设计器中的语法完成

切换到 SDK 格式后出现错误:\Microsoft.WinFX.targets(225,9): error MC1002: Library project file cannot specify ApplicationDefinition element. 因此我删除了 app.xaml 文件,但现在我没有语法完成功能,并且设计器不再显示我的控件。

我尝试的是添加资源文件并设置

<Page Include="Properties\DesignTimeResources.xaml">
    <SubType>Designer</SubType>
    <Generator>MSBuild:Compile</Generator>
    <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>

在 csproj 文件中。但是这个技巧不再起作用了,因为页面不能再在 csproj 文件中定义了..

Microsoft 认为这应该如何工作?有什么方法可以在不编辑我的 1000 个 xaml 文件的情况下将设计时资源添加到我的项目中?

我真的想避免将此添加到我的每个 xaml 文件中:

 <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MyAssembly;component/Resources/ResourceBundle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

【问题讨论】:

  • 有一个 GitHub thread 有同样的问题
  • 设计师本身不是问题。问题是设计时资源..

标签: c# .net wpf .net-core


【解决方案1】:

这就是诀窍:

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

将此添加到项目文件中,我的用户控件再次在设计时加载。不幸的是智能感知仍然无法正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多