xamarin - 从 ContentView 访问 App.xaml 中的全局资源
问题描述
我在 App.xaml 资源字典中定义了一种颜色。当我尝试在 ContentView 中引用它时,出现运行时异常:
未找到键 NavbarBackground 的 StaticResource
我需要在 App.xaml 中合并资源字典吗?
应用程序.xaml<Application.Resources>
<ResourceDictionary>
<!--Global Styles-->
...
<Color x:Key="NavbarBackground">Black</Color>
...
</ResourceDictionary>
</Application.Resources>
CustomTitleView.xaml
<ContentView>
...
<ContentView.Content>
...
<ImageButton Source="search.png" HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" BackgroundColor="{StaticResource NavbarBackground}" />
...
</ContentView.Content>
</ContentView>
解决方案
如果您使用单独的 xaml 文件来定义资源,请使用 ResourceDictionary。如果您是 App.xaml 中的设置值,请将它们设置在 Application.Resources 中。请参阅此处的文档
推荐阅读
- gitlab - GitLab:如何在降价项目符号/列表上添加(删除)额外的行空间?
- ruby-on-rails - 使用 wicked_pdf 生成的 PDF 文件不超过 200 页,带有页脚
- selenium-webdriver - 需要 Xpath 或 CSSSelector 定位器用于复选框元素的以下 HTML 代码
- typescript - 下面的代码段在打字稿中是什么意思?
- strapi - 如何调试有关记录创建的 Strapi 错误?
- javascript - 如何使用 JavaScript 获取 CheckBoxlist 值
- angular - 使用 AWS SDK Amplify 下载 S3 中的文件夹
- python - 以统一的方式缩放值?
- node.js - 如何在节点js中将urlencoded数组转换为json
- python - 你能确定这个程序关于线性回归的正规方程实现有什么问题吗