我目前正在开发 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/