2017-06-01 19 views
0

我已經創建了兩個自定義控制,無論是從System.Windows.Controls.Button繼承。一個被稱爲XLButton,另一個是XLBox。他們有相同的款式XAML /在兩個獨立的ResourceDictionary物體在兩個獨立的.xaml文件模板,和相同的代碼隱藏文件,除了「XLButton」出現在XLButton文件,其中「XLBox」出現在XLBox文件,反之亦然移動合併XAML ResourceDictionaries到Generic.xaml導致錯誤

我創建了一個帶有兩排Grid一個簡單的測試窗口。我的ResourceDictionary文件合併成測試窗口的的Window.Resources,並創建每個自定義控制,一個頂行中,一個在底部的一個實例。這工作正常。這裏是測試窗口的XAML:

<Window x:Class="ScratchPadWindow" 
     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:ExLuminaControls" 
     mc:Ignorable="d" 
     Title="ScratchPadWindow" Height="118" Width="145"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="/Styles/XLBox.xaml" /> 
       <ResourceDictionary Source="/Styles/XLButton.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <local:XLBox Content="Hi!"/> 
     <local:XLButton Grid.Row="1" Content="Yeah!"/> 
    </Grid> 
</Window> 

這工作得很好。但是,當我註釋掉ResourceDictionary.MergedDictionaries部分並在其原始形式Themes\Generic.xaml複製,所以它看起來是這樣的:

<ResourceDictionary 
     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:ExLuminaControls" 
     mc:Ignorable="d"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/Styles/XLButton.xaml" /> 
     <ResourceDictionary Source="/Styles/XLBox.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

我得到一個「無法找到資源‘風格/ xlbutton.xaml’錯誤,與相關這條線:

<local:XLBox Content="Hi!"/> 

這沒有什麼意義,我,但更令人困惑的是,問題消失,如果我在設計單擊「禁用項目代碼」

我。使用混合2 017社區。

任何人都可以幫助我理解這一點嗎?

謝謝!

回答

0

它的路徑問題,在一般情況下,使用這樣的

<ResourceDictionary Source="/AssemblyName;Component/Styles/XLButton.xaml" /> 

或有 「../../」

  1. 主題和通用小寫
  2. 上去組裝信息
  3. app.xaml如果沒有任何工作

    [assembly:Th emeInfo( ResourceDictionaryLocation.None,//主題特定資源字典所在的位置 //(用於在頁面中未找到資源, //或應用程序資源字典) ResourceDictionaryLocation。SourceAssembly //其中通用資源字典位於 //(如果使用的資源沒有在頁面, //應用程序,或任何主題特定資源詞典中找到的) )

    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
    
         <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" /> 
         ... 
        </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
    

定義和風格

#region --------------------CONSTRUCTORS-------------------- 

    static WaitSpin() 
    { 
     FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(WaitSpin), 
      new FrameworkPropertyMetadata(typeof(WaitSpin))); 
    } 

    /// <summary> 
    /// LoadingAnimation constructor. 
    /// </summary> 
    public WaitSpin() 
    { 
     this.DefaultStyleKey = typeof(WaitSpin); 
    } 

    #endregion 


<Style x:Key="{x:Type local:WaitSpin}" TargetType="{x:Type local:WaitSpin}"> 
+0

恐怕沒有什麼區別。當我將'ResourceDictionary'引用放入'Window'的.xaml文件中時,它仍然有效,但當我將它放在generic中時(不管是否有'/ ExLuminaControls; Component'部分)。 xaml'文件。 –

+0

好的,另外3人檢查,見上面 – GCamel

+0

謝謝。當我從Blend移動到VS時,你的路徑確實修復了ref。所有其他的東西檢查出好。顯然,問題比我想象的要微妙。自定義樣式在generic.xaml中定義時仍然沒有效果。如果我直接將Style定義移動到generic.xaml文件中,它仍然沒有效果。但是,如果我製作第二個項目,除名稱外與第一個項目相同,則第二個項目可以從第一個項目實例化自定義控件,並應用generic.xaml中的樣式。不知道爲什麼generic.xaml不適用於自己的項目。標記你的答案是正確的。 –