簡體   English   中英

從資源文件加載WPF樣式

[英]Loading WPF Style from Resource File

我試圖從其他文件加載WPF樣式實際上從WPF自定義控件庫,但我無法加載這里是我的解決方案。

該解決方案包含兩個項目

  1. WPF自定義控件庫類型的WpfTestControls

  2. WPF應用程序庫類型的WpfTestApp,它引用了WpfTestControls

來自WPF應用程序庫的MainWindow.xaml

<Window.Resources>
    <Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green"/>
    </Style>
</Window.Resources>
<Grid>
    <TextBox Height="50px" Width="100px" Style="{DynamicResource TempStyle}"/>
</Grid>

來自WPF自定義控件庫的Generic.xaml

<ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/WpfTestControls;component/TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

來自WPF自定義控件庫的TextBoxStyle.xaml

<ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Green"/>
</Style>

我的AssemblyInfo.cs文件包含以下內容

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries))]

但我仍然沒有加載風格。 如果我使用不使用Generic.xaml一切正常,例如下面的代碼按預期工作

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Green"/>
    </Style>
</Window.Resources>
<Grid>
    <TextBox Height="50px" Width="100px" Style="{StaticResource TempStyle}"/>
</Grid>

我究竟做錯了什么 ? 提前致謝

請為我回答幾件事......

  1. “WPF自定義控件庫”程序集與“WpfTestControls”程序集相同嗎?
  2. 如果沒有,那么“WPF自定義控件庫”是否引用了“WpfTestControls”程序集?
  3. 您的WpfTestApp是否同時引用“WPF自定義控件庫”和“WpfTestControls”程序集?

如果添加該引用,則應正確加載資源。

我的步驟......

  1. 添加“WPF自定義控件庫”說“ThemesLibray”
  2. 在此添加“Themes”文件夾下的兩個資源字典

TextBoxStyle.xaml

 <ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="GreenTextBoxStyle" TargetType="{x:Type TextBox}">
       <Setter Property="Background" Value="Green"/>
    </Style>
 </ResourceDictionary>

Generic.xaml

  <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="TextBoxStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
  1. 我有主要的starup項目“MyWPFTestApp”,它有對ThemesLibray程序集引用。 在那個窗口中ThemesLibrary這種方式合並了ThemesLibrary資源....

     <Window x:Class="MyWPFTestApp.Window7" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window7" Height="300" Width="300"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/ThemseLibrary;component/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <TextBox Style="{StaticResource GreenTextBoxStyle}"/> </Grid> </Window> 

當我啟動MyWPFTestApp時,我看到帶有綠色TextBox的Window。

主要是:確保將資源字典的構建操作設置為資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM