3

WP7 Mango の ResourceDictionary に問題があります。

インターネットで見つけたもののほとんどは、次のように簡単です。

1) 本文を含む Xaml ファイル:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <Style x:Key="TextBlockStyle1" TargetType="TextBlock">
 <Setter Property="Foreground" Value="Orange"/>
 <Setter  Property="FontSize" Value="24"/>
 <Setter  Property="VerticalAlignment" Value="Bottom"/>
</Style>
</ResourceDictionary>

2) App.xaml に以下を追加します。

 <Application.Resources>
    <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="MyResources.xaml"/>
       </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
 </Application.Resources>

なぜ機能しないのかわかりません。このようにすると、例外が発生します。

タイプ 'ResourceDictionary' は ResourceDictionary 内にあり、キーを持っていません。

ステップ 2 で xaml の 2 行目に ked を追加すると実行されますが、不明なエラーでクラッシュします。MyResources.xaml ファイルからリソースを追加していないようです。

誰かがここで解決策を指摘できますか?

4

2 に答える 2

1

実際にそれを理解しました。

キーを入力せずに動作させようとしていたところ、App.xaml に残したスタイルが問題を引き起こしていることがわかりました。そのため、App.xaml に残っている残りのすべてのスタイルは、それらが一意であるにもかかわらず、内部に移動する必要がありました。

<Application.Resources>
<ResourceDictionary>

   my remaining styles with key & target type are here now

   <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="MyResources.xaml"/>
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

編集:

誰かの時間を節約し、それらを理解するのに長い時間がかかったいくつかの重要な詳細: 1) MSDN も推奨しているように、Key を ResourceDictionary 内に配置しないでください

2) 参照される Xaml 内のスタイルには、すべて Key (または Name ) が含まれている必要があります。

3) 残りのスタイルは、上で説明したように配置する必要があります

4) 次のコードでは、他のいくつかのスタイルが基づいている基本スタイルを再定義する場合、MyResources2.xaml で継承されたスタイルも再定義するまで、変更は反映されません (または、MyResources.xaml の基本スタイルを MyResources2.xaml のスタイルに置き換えます)。 )

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="MyResources.xaml"/>
  <ResourceDictionary Source="MyResources2.xaml"/>             
</ResourceDictionary.MergedDictionaries> 

5) MergedDictionaries の ResourceDictionaries は LIFO として機能します

于 2012-04-03T20:54:04.890 に答える
1

App.xaml で ResourceDictionary のキーを設定する必要があります。

<Application.Resources>
    <ResourceDictionary x:Key="keyname">
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="MyResources.xaml"/>
       </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
于 2012-04-03T10:28:11.983 に答える