xaml - 无法从 app.xaml 访问 Xamarin Forms 中的 StaticResource

标签 xaml xamarin

我无法访问在 app.xaml 中创建的静态资源。 这是我的 app.xaml 代码

<Application.Resources>
    <ResourceDictionary>
      <Color x:Key="AppDefaultColor">#07bab7</Color>
      <Color x:Key="PageDefaultColor">#f0f0f0</Color>
    </ResourceDictionary>
  </Application.Resources>

当我尝试在我的页面中将其用作静态资源时,它会出错。 这是我用来从 app.xaml 访问静态资源的页面代码

<Label Text="Test"  TextColor="{StaticResource AppDefaultColor}"  />

最佳答案

请检查您的 app.xaml.cs 类是否调用了 InitializeComponent() 方法。在创建 App.xaml 和后面的关联代码时需要的 App 构造函数中。

请看图片

app.xaml.cs

关于xaml - 无法从 app.xaml 访问 Xamarin Forms 中的 StaticResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709965/

相关文章:

c# - 在可见性更改时激活 Storyboard

c# - 在将 ItemsSource 与数据网格 wpf 中的复选框控件一起使用之前,项目集合必须为空

wpf - 使用 WPF 的 MVVM 模式是否不需要 Dispatcher?

c# - 如何在 UWP 应用程序中保留 TextBlock 的空白

facebook - Xamarin.Android 可以使用哪个 facebook SDK?

c# - 如何得到 x :name of element in code behind/XAML

c# - WPF 多重绑定(bind) .Net Framework 4.0

android - 启动画面大小调整不正确

c# - Xamarin-Studio Android 窗体设计器 : Android Version not installed

xamarin - 如何在 Xamarin.Forms 中重定向