2014-01-16 21 views
0

我正在創建一個Windows Phone 8應用程序。在應用程序中我有一個XAML文件中定義的資源字典爲:如何在Windows Phone 8中使用ResourceDictionary作爲StaticResource(元素不是fount)

 <Style TargetType="TextBlock"> 
     <Setter Property="Foreground" Value="YellowGreen" /> 
     <Setter Property="FontSize" Value="35" /> 
     <Setter Property="TextWrapping" Value="Wrap"/> 
    </Style> 

    <Style TargetType="TextBox" x:Key="CommonSetters"> 
     <Setter Property="Width" Value="450"/> 
     <Setter Property="FontSize" Value="35" /> 
     <Setter Property="Foreground" Value="YellowGreen" /> 
     <Setter Property="Height" Value="100"/> 
     <Setter Property="Background" Value="Red"> 
      <!--<Setter.Value> 
       <ImageBrush ImageSource="logo.png" Opacity="0.1"/> 
      </Setter.Value>--> 
     </Setter> 
    </Style> 

</ResourceDictionary> 

這ResourceDictionary的是在App.xaml中引用爲:

的xmlns:SHELL =「CLR的命名空間:微軟.Phone.Shell;裝配= Microsoft.Phone「>

<!--Application Resources--> 
    <Application.Resources> 
     <local:LocalizedStrings xmlns:local="clr-namespace:Work_Force" x:Key="LocalizedStrings"/> 
     <ResourceDictionary x:Key="myDict"> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Resource.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 

     </ResourceDictionary> 
     <Style TargetType="TextBlock" x:Key="NN"> 
      <Setter Property="Width" Value="450"/> 
      <Setter Property="FontSize" Value="35" /> 
      <Setter Property="Foreground" Value="YellowGreen" /> 
      <Setter Property="Height" Value="100"/> 
     </Style> 
    </Application.Resources> 

    <Application.ApplicationLifetimeObjects> 
     <!--Required object that handles lifetime events for the application--> 
     <shell:PhoneApplicationService 
      Launching="Application_Launching" Closing="Application_Closing" 
      Activated="Application_Activated" Deactivated="Application_Deactivated"/> 
    </Application.ApplicationLifetimeObjects> 

</Application> 

,然後做的簡單的部分:

靜態資源NN做工精細,但commonSetters沒有工作,那麼在resourse.xaml聲明。

回答

1

您在App.xaml中定義資源的方式,您的'myDict'資源字典是Application.Resources默認字典中的嵌套字典。我不知道任何語法如何引用XAML中的嵌套字典中的資源(因爲我以前不需要這樣的代碼;我認爲來自後臺代碼的FindResource(...)可以實現這一點)。解決您的問題的一種方法是,您直接修改Application.Resources的默認詞典以合併您的「資源」字典。您可以通過修改Application.Resources節這樣做:

<Application.Resources> 
    <ResourceDictionary> 
     <local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Resources.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

需要注意的是,現在你沒有一個叫「myDict」了ReosurceDictionary。您的「資源」字典將直接合併到默認字典中,該字典沒有密鑰。然後資源將被正確解析。

+0

感謝,這也適用 –

0

終於我知道了類似像ASPX或HTML頁面,在這裏我們需要的頁面

在徘徊無論你添加的樣式的引用,它需要在頁面中添加頁面的引用,你的解決方案有需要

 <phone:PhoneApplicationPage.Resources> 
      <ResourceDictionary Source="yourstylepage.xaml"/>  
     </phone:PhoneApplicationPage.Resources> 

把上面的代碼<phone:PhoneApplicationPage>

相關問題