VS csproj设置

WPF 手动关联xaml与cs文件

复用其他控件,添加现有项时无法关联

    <Compile Include="Controls\UC_Loading.xaml.cs">
      <DependentUpon>UC_Loading.xaml</DependentUpon>
    </Compile>

Winform 手动关联Designer与cs文件

<Compile Include="Controls\Dir\Loading.cs">
  <SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\Dir\Loading.Designer.cs">
  <DependentUpon>Loading.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Controls\Dir\Loading.resx">
  <DependentUpon>Loading.cs</DependentUpon>
</EmbeddedResource>

启用可空引用

启用 C# 8.0 的语法支持
在项目文件中开启可空引用类型的支持

定义 说明 备注
string aa 不可为空 aa就是不可为空的引用类型
string? aa 可为空 aa就是可为空的引用类型
method(string aa) 未知 对于类型参数来说,可能不能确定是否是可空引用类型
static void Main(string[] args)
{
    Test(null);;
}

#nullable enable
readonly static string? _name = null;
private static void Test(string name)
{
    int len = name.Length;
    int len2 = _name.Length + len;
}
#nullable disable

len2 计算时会提示 _name可能为null

null!

public Demo Demo { get; set; } = null!;
一元后缀 ! 运算符是 null 包容运算符(null-forgiving operator),可以取消表达式的所有可为 null 警告。
null! 表示不可以为 null 的类属性。
!的作用

显示引用次数

当前上下文中不存在名称“InitializeComponent”

xaml(提示错误的cs文件对应的xaml)文件重新保存一下。

warning NETSDK1049

Microsoft.NET.Build.Extensions.NETFramework.targets(*,*): warning NETSDK1049: 解析的文件包含错误图像、无元数据或不可访问。未找到索引。 (异常来自 HRESULT:0x80131124) ...XXX.dll

dll经过.NET Reactor 等工具加密混淆过,实际这个警告对程序的运行没有任何的影响。
用文本编辑器打开出现问题的项目文件 xxx.csproj ,
在 PropertyGroup 节点中追加<DependsOnNETStandard>false</DependsOnNETStandard>
比如:

VS启动 未知错误 ActivityLog.xml

删除 Microsoft.VisualStudio.Default.cache 文件。比如,我的VS 2019,文件目录为:

C:\Users...\AppData\Local\Microsoft\VisualStudio\16.0_4a05ccaa\ComponentModelCache

程序集重定向

在配置文件中指定程序集绑定,将一系列程序集版本重定向到另一个版本。

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
    </assemblyBinding>
</runtime>

指定运行时应使用版本 4.0.3.0 而不是 0.0.0.0 和 4.0.3.0 之间的程序集版本。

posted @ 2020-12-24 21:02  wesson2019  阅读(629)  评论(0编辑  收藏  举报