开发者

Silverlight: App.Current.Resources is empty

What I have doen has been based on this article: http://msdn.microsoft.com/en-us/library/aa348547.aspx

I am trying to get a string from a merged dictionary that is loaded in app.xaml. I am trying to do this from a class that is not a code behind file. I know that the resource file can load in principle because page elements are being styled from the xaml markup from styles contained in the Styles.xaml file.

The app.xaml file

<Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  x:Class="theApp.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

the styles.xaml file (edited down for simplicity)

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
     xmlns:clr="clr-namespace:System;assembly=mscorlib">

    <clr:String x:Key=开发者_运维知识库"applicationName">TheKraken</clr:String>
</ResourceDictionary>

The line of code i am using to try and access the resource

string appName = (string)App.Current.Resources["applicationName"];

Any idea what i am missing?


the real problem with this code is not the code itself.

The variable I am assigning the resource to is then consumed as part of a linq where clause.

You won't believe this as i had 3 people scratching their heads today. the situation was this: I have a set of contract statements that ensure the precondition that app.Current.Resources is not empty - these all pass

For the sake of demonstrating the oddity I assign the resource to 2 seperate strings. one of which is used in the linq where clause the other is there to demonstrate that pulling the string from the resource does actually work. The string that is in the where clause is null and cannot be set to anything other than null. The other string contains data as expected. Both strings are assigned to using precisley the same code. Even stranger yet, if i initialise a string to say "john" and then asign the resource data to it and then consume this in the linq then the assignment of "john" to the string also results in the string being null. the linq was working upon an XElement at the time.

string test = "John";
test =  (string)App.Current.Resources["applicationName"];
var results = from ........
              where x == test
              select something

this caused test to not get set to john - IE the linq after the code being questioned is having an effect on the assignment.

The fix to the problem was to move the linq out into another rmethod and then everything behaved normally. I do not have the slightest clue why this occured.

I would love to know though. Am very tempted to recreate it in a new project and send it to microsoft tbh. But am hoping there is a obvious and rational explanation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜