【解決方法】外部 (DLL) リソース ディクショナリの WINDOW スタイル プロパティがデザイン モードで適用されない

プログラミングQA


私は持っている 2つの異なる プロジェクト…

WPF コントロール ライブラリ 他のファイルの中でも、私がいくつか持っているプロジェクト リソース ディクショナリ ファイル…

そして、 WPF アプリケーション 私が追加した参照のプロジェクト WPF コントロール ライブラリ.

私の問題は、 スタイル 上に Window そこからの要素 外部リソース ディクショナリ ファイル。

スタイルズ 変化 好き、 Background なる 見える だけで ランニングモード!!!

私が試したこと:

これが私のものだとしましょう Window.xaml 私の中にあるスタイルファイル WPF コントロール ライブラリ 計画:

XML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="Window_Style" TargetType="Window">
        <Setter Property="Background" Value="#FF272727"/>
    </Style>

</ResourceDictionary>

まず私は 追加 それは 参照 私の中に WPF アプリケーション プロジェクトに追加します App.xaml 私のファイル WPF アプリケーション 計画:

XML
<Application x:Class="MyLibMaster.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyLibMaster"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyLib;component/Resources/Styles/Window.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

それから私はそれを私のものに適用します Window このような:

XML
<Window x:Class="MyLibMaster.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyLibMaster"
        mc:Ignorable="d"
        Title="MainWindow" Height="480" Width="840"
        
        Style="{DynamicResource Window_Style}">

    <Grid >
        
    </Grid>
    
</Window>

解決策 1

カスタムを追加する必要があります DesignTimeResourceDictionary. Google 検索を介していくつかの例があります。 クラスの元のソースと思われるものは次のとおりです。 デザイン モードでのみ ResourceDictionary を使用するトリック – TechNet Articles – United States (English) – TechNet Wiki[^]

次のようになります。

C#
public class DesignTimeResourceDictionary : ResourceDictionary
{
    /// <summary>
    /// Local field storing info about designtime source.
    /// </summary>
    private string? _designTimeSource;

    /// <summary>
    /// Gets or sets the design time source.
    /// </summary>
    /// <value>
    /// The design time source.
    /// </value>
    public string? DesignTimeSource
    {
        get => _designTimeSource;

        set
        {
            _designTimeSource = value;
            if ((bool)DesignerProperties
                    .IsInDesignModeProperty
                    .GetMetadata(typeof(DependencyObject))
                    .DefaultValue)
                base.Source = new Uri(_designTimeSource!);
        }
    }

    /// <summary>
    /// Gets or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    /// <returns>The source location of an external resource dictionary. </returns>
    public new Uri Source
    {
        get => base.Source;
        set => throw new Exception("Use DesignTimeSource instead set Source!");
    }
}

クラスが外部 Lib にある場合は、 App.Xaml:

XML
<Application x:Class="MyLibMaster.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyLibMaster"
             xmlns:MyLib="clr-namespace:MyLib;assembly=MyLib"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                    Source="/MyLib;component/Resources/Styles/Window.xaml" />
                <MyLib:DesignTimeResourceDictionary
                    DesignTimeSource="/MyLib;component/Resources/Styles/Window.xaml" />

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

解決策をコンパイルし、デザインタイムの xaml ウィンドウを閉じて再度開くと、スタイルが適用されるはずです。

コメント

タイトルとURLをコピーしました