c# - 如何在UWP中的类库项目中使用Resources.resw?

标签 c# windows-10-universal

我目前正在开发 Windows 10 UWP。起初,我只有一个项目,我在其中添加了 Constants.resw 文件来使用我保存在本地存储中并稍后访问它们的资源。现在我已经创建了一个 Timer 类型的后台任务,并且我想访问 Timer 后台任务项目中的数据库,因此我创建了一个类库项目并在该项目中添加了所有 POJOS 和 Utils 类。有人可以建议我是否可以在类库项目中添加 Constants.resw 以及如何添加?

另外,我想使用下面的代码来访问它,这是我在有一个项目(Windows 10 UWP 项目)时使用的代码

ApplicationDataContainer userSettings = ApplicationData.Current.LocalSettings;
if (!userSettings.Containers.ContainsKey(Constants.DB_AVAILABLE)) //if (!checkIfDBExists().Result)
{
     //some operations
}

当我尝试在后台任务中使用 Constants.resw 时,出现以下错误

Exception thrown: 'System.TypeInitializationException' in Common.dll
System.TypeInitializationException: The type initializer for 'Common.CommonResources.Constants' threw an exception. ---> System.Exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
at Windows.UI.Xaml.Application.get_Current()
at Common.CommonResources.Constants..cctor()
--- End of inner exception stack trace ---
at Common.CommonResources.Constants.get_DB_AVAILABLE()
at Common.DatabaseManager.getSQLiteConnection()
at Common.DatabaseManager.getLocationDetails()The type initializer for 'Common.CommonResources.Constants' threw an exception.

最佳答案

您无需将 Resources.resw 文件添加到类库或 Windows 运行时组件中,因为资源在应用级别而不是项目级别工作。 这意味着您的主应用程序项目中的资源可以在应用程序中的所有项目中“查看”和使用。这是如何从外部加载资源的示例:

ResourceContext resourceContext = ResourceContext.GetForViewIndependentUse();
ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");

// Here you load the resource you need
var resourceValue = resourceMap.GetValue("resourceName", resourceContext);

关于c# - 如何在UWP中的类库项目中使用Resources.resw?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36840231/

相关文章:

c# - 字节数组末尾有大量 0 值

c# - 如何从引用的 nuget 包加载 azure 事件函数

c# - Asp.net MVC 4 Ajax.BeginForm 提交 + mvc.grid - 页面重新加载

uwp - 如何选择 Windows 10 SDK 的最低版本和目标版本

azure - 如何将 Windows 开发人员中心仪表板/使用页面中的检测 key 添加到 Azure 门户?

sqlite - 如何在 UWP 项目中使用预先设计好的 SQLite?

c# - 使用简单进样器根据需要加载装配体

windows-store-apps - 用于商店提交的 .appxupload UWP 包不包含 .appxsym 文件

c# - 为什么BitmapImage源只能改一次?

c# - 动态将Func转换为对应的Action