3

最近、Silverlight アプリケーションをいくつかの小さなプロジェクトに分割しました。

スタイルを含むすべてのリソース ディクショナリを別のプロジェクト ("Application.Themes") に移動し、メイン プロジェクト内の App.xaml ファイルからこれらを参照します。

これはメイン プロジェクトでは正常に機能しますが、これらのリソース ディクショナリ内のスタイルを参照する他のすべてのプロジェクトでは、デザイナー内で「オブジェクト参照がオブジェクトのインスタンスに設定されていません」という例外がスローされますが、問題なく正しいスタイルでコンパイルおよび実行されます。 .

メインの App.xaml ファイルと同じ辞書を参照する個々のプロジェクトのそれぞれに App.xaml ファイルを追加しましたが、違いはありません。

デザイナーを使用できる別のプロジェクトからリソース ディクショナリを参照する正しい方法はありますか?

編集:

ここに、私が抱えている問題を示すための詳細情報とコードスニペットがあります

このプロジェクト内に「テーマ」と呼ばれるスタイル プロジェクトがあり、プロジェクトのすべてのスタイルを定義する辞書がいくつかあります。

メインの App.xaml 内には次のものがあります

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
            <ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>

メイン プロジェクト内でスタイルを参照すると、それらは正しく機能します。ただし、それらのプロジェクトが Themes プロジェクトを参照している場合でも、他のプロジェクトには適用されません。

デザイン時にスタイルを解決するために、各 UserControl の先頭に次のものを配置しようとしましたが、それでもプロジェクト内のスタイルを解決できません。

<UserControl>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
                <ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>

    <!-- Additional Control Specific resources -->
    </ResourceDictionary>
</UserControl.Resources>


<!-- The following resources are defined in Styles.XAML and don't resolve at design time  and throw errors -->
<TextBlock Text="Header Test"
           FontFamily="{StaticResource HeaderFontFamily}"
           Foreground="{StaticResource StrongBrush}">

</UserControl>

私の styles.xaml はこれに似ています。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:behaviors="clr-namespace:Minerva.Presentation.Behavior;assembly=Minerva.Presentation"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">

<SolidColorBrush x:Key="StrongBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.StrongColor}" />
<FontFamily x:Key="HeaderFontFamily">Segoe UI Light, Lucida Sans Unicode, Verdana</FontFamily>

</ResourceDictionary>
4

2 に答える 2

2

スタイル/テーマプロジェクトのアセンブリを作成して、他のプロジェクトから参照できるようにします。MergedDictionariesを使用してapp.xaml/page.xamlでこれらのスタイルをアプリケーションにマージするには、

<Application.Resources>
 <ResourceDictionary>  
   <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="/Assembly;component/Stylesorthemes.xaml"/>             
     </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources> 

これがお役に立てば幸いです。

于 2012-10-18T08:36:59.720 に答える
0

リソース ディクショナリ Theme.xaml を含むアセンブリ テストを作成しました

Theme.xaml コード

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Thickness x:Key="GeneralThickness">10</Thickness>
</ResourceDictionary>

別の Silverlight プロジェクトのテストリターンを作成しました

case1.In App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         x:Class="testreturns.App"
         >
<Application.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/test;component/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
</Application>

case2.ユーザーコントロールレベル

<UserControl.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/test;component/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources> 

そしてそれを使ってボタンのボーダーの太さを設定します

<Button Height="50" Width="150" BorderThickness="{StaticResource GeneralThickness}"/>

どちらの場合も、それは私のために働いています。これはあなたが意図したものですか?アセンブリの作成中に、theme.xaml ファイルのプロパティ BuildAction を Resource に設定しましたか?

于 2012-10-18T13:32:21.847 に答える