如何创建单独的资源文件进行调试和发布



是否可以创建两个文件,例如Text.Debug.resxText.Release.resx,在程序调试和发布期间自动加载相应的资源文件?

我会包装ResourceManager:

public class Resources
{
private readonly ResourceManager _resourceManager;
public Resources()
{
#if DEBUG
const string configuration = "Debug";
#else
const string configuration = "Release";
#endif
_resourceManager = new ResourceManager($"StackOverflow.Text.{configuration}", typeof(Resources).Assembly);
}
public string GetString(string resourceKey)
{
return _resourceManager.GetString(resourceKey);
}
}

显然,在新建管理器时要适当地修改名称空间。

编辑

您也可以将其实现为一个静态类,以避免必须新建包装器的实例:

public static class Resources
{
private static ResourceManager _resourceManager;
public static string GetString(string resourceKey)
{
if (_resourceManager != null)
{
return _resourceManager.GetString(resourceKey);
}
#if DEBUG
const string configuration = "Debug";
#else
const string configuration = "Release";
#endif
_resourceManager = new ResourceManager($"StackOverflow.Text.{configuration}", typeof(Resources).Assembly);
return _resourceManager.GetString(resourceKey);
}
}

在Properties下创建两个子目录:Debug和Release。复制Resources.resx和Resources。Designer.cs文件添加到每个目录中。它将重新生成资源。命名空间为ProjectName的Designer.cs文件。属性。Debug或ProjectName。属性。释放编辑.csproj文件,对这些文件设置适当的条件,如下所示:

<Compile Include="PropertiesDebugResources.Designer.cs" Condition="$(Configuration.StartsWith('Debug')) ">
...
<EmbeddedResource Include="PropertiesDebugResources.resx" Condition="$Configuration.StartsWith('Debug'))">
...

然后将Resources.cs文件添加到Properties目录,并使用#if DEBUG来确定它是否继承自Properties。调试。资源或财产。释放资源:

namespace ProjectName.Properties
{
class Resources
#if DEBUG
: ProjectName.Properties.Debug.Resources
#else
: ProjectName.Properties.Release.Resources
#endif
{
}
}

最新更新