this.Resources抛出NullReferenceException?

时间:2015-02-13 13:43:54

标签: c# xaml windows-phone-8 windows-phone

我正在尝试从我的资源中访问应用资源,如下所示:

LinearGradientBrush lb = Application.Current.Resources["FFG"] as LinearGradientBrush;

但是,这会抛出NullReferenceException。我尝试使用此代码来查看null:

Debug.WriteLine(this.Resources.Count());

还返回了NullReferenceException。

更奇怪的是,这是有效的:

(Storyboard)this.Resources["LayersOut"]).Begin();

这个.Resources可能是null,即使我在XAML的资源区域拥有所有这些吗?

请注意,上面的代码是在InitializeComponent();

之后运行的

(为了便于阅读,删除了所有的故事板)

<phone:PhoneApplicationPage.Resources>
<LinearGradientBrush x:Key="PrecipBrush" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF490000" Offset="0"/>
        <GradientStop Color="#FFE8E8E8" Offset="1"/>
        <GradientStop Color="#FF2044E4" Offset="0.838"/>
        <GradientStop Color="#FF17A598" Offset="0.653"/>
        <GradientStop Color="#FF22BB00" Offset="0.445"/>
        <GradientStop Color="#FFFFE800" Offset="0.253"/>
        <GradientStop Color="#FFFF0202" Offset="0.125"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="MRMS" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="Red" Offset="0"/>
        <GradientStop Color="#FFAB59FD" Offset="1"/>
        <GradientStop Color="#FF2044E4" Offset="0.838"/>
        <GradientStop Color="#FF17A598" Offset="0.653"/>
        <GradientStop Color="#FF22BB00" Offset="0.445"/>
        <GradientStop Color="#FF005D00" Offset="0.253"/>
        <GradientStop Color="Yellow" Offset="0.125"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="FFG" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF460055" Offset="0"/>
        <GradientStop Color="#FF23FF00" Offset="1"/>
        <GradientStop Color="Yellow" Offset="0.815"/>
        <GradientStop Color="Magenta" Offset="0.253"/>
        <GradientStop Color="#FFB400A3" Offset="0.125"/>
        <GradientStop Color="#FFB69D00" Offset="0.642"/>
        <GradientStop Color="#FFB60000" Offset="0.43"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="DroughtKey" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF490000" Offset="0"/>
        <GradientStop Color="#FFFFD100" Offset="1"/>
        <GradientStop Color="#FFB67C00" Offset="0.653"/>
        <GradientStop Color="#FF746400" Offset="0.445"/>
        <GradientStop Color="Red" Offset="0.253"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="ForestFire" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF490000" Offset="0"/>
        <GradientStop Color="#FFFFD100" Offset="1"/>
        <GradientStop Color="Red" Offset="0.253"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="QPF" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="Red" Offset="0"/>
        <GradientStop Color="#FF68FF00" Offset="1"/>
        <GradientStop Color="#FF28B000" Offset="0.838"/>
        <GradientStop Color="#FF0800BB" Offset="0.445"/>
        <GradientStop Color="#FF00FFD1" Offset="0.253"/>
        <GradientStop Color="#FFDC00FF" Offset="0.125"/>
        <GradientStop Color="#FF126300" Offset="0.626"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="Temp" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="Red" Offset="0"/>
        <GradientStop Color="#FF070097" Offset="1"/>
        <GradientStop Color="#FF035FFF" Offset="0.838"/>
        <GradientStop Color="#FF23FF00" Offset="0.445"/>
        <GradientStop Color="Yellow" Offset="0.253"/>
        <GradientStop Color="#FFFF5D00" Offset="0.125"/>
        <GradientStop Color="#FF00FFF3" Offset="0.626"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="PrecipAvg" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF661C00" Offset="0"/>
        <GradientStop Color="#FF17A600" Offset="1"/>
        <GradientStop Color="#FF26EC4A" Offset="0.838"/>
        <GradientStop Color="#FFDCFF00" Offset="0.445"/>
        <GradientStop Color="#FFFF8000" Offset="0.253"/>
        <GradientStop Color="#FF9B3800" Offset="0.125"/>
        <GradientStop Color="#FF65E040" Offset="0.626"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="PrecipHour" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF661C00" Offset="0"/>
        <GradientStop Color="#FF0011BF" Offset="1"/>
        <GradientStop Color="#FF265CEC" Offset="0.838"/>
        <GradientStop Color="#FF2EFF00" Offset="0.445"/>
        <GradientStop Color="#FFF3FF00" Offset="0.253"/>
        <GradientStop Color="Red" Offset="0.125"/>
        <GradientStop Color="#FF65E040" Offset="0.626"/>
    </LinearGradientBrush>
</phone:PhoneApplicationPage.Resources>

2 个答案:

答案 0 :(得分:0)

Application.Current.Resources是指App.xaml中定义的资源,而不是当前页面。尝试在后面的代码中以这种方式访问​​:

    LinearGradientBrush lb = this.Resources["FFG"] as LinearGradientBrush;

或在App.xaml上移动画笔定义,然后您的代码也应该起作用:

   LinearGradientBrush lb = Application.Current.Resources["FFG"] as LinearGradientBrush;

答案 1 :(得分:0)

您需要在App.xaml中编写以下代码:

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Path to your resource file" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后你可以这样得到它:

LinearGradientBrush lb = Application.Current.Resources["FFG"] as LinearGradientBrush;