2012-06-11 28 views
6

Tôi có phong cách sau đây bổ sung vào Windows.Resources tôitải từ tập tin riêng biệt trong WPF

<Window.Resources> 
... 
<!--A Style that extends the previous TextBlock Style--> 
<!--This is a "named style" with an x:Key of TitleText--> 
<Style BasedOn="{StaticResource {x:Type TextBlock}}" 
    TargetType="TextBlock" 
    x:Key="TitleText"> 
<Setter Property="FontSize" Value="26"/> 
<Setter Property="Foreground"> 
<Setter.Value> 
    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 
    <LinearGradientBrush.GradientStops> 
     <GradientStop Offset="0.0" Color="#90DDDD" /> 
     <GradientStop Offset="1.0" Color="#5BFFFF" /> 
    </LinearGradientBrush.GradientStops> 
    </LinearGradientBrush> 
    </Setter.Value> 
</Setter> 
</Style> 
... 
</Window.Resources> 

Tôi có rất nhiều những phong cách trong mã XAML của tôi và tôi muốn lưu mỗi kiểu thành phần để tệp phụ (không phải tệp bên ngoài) .. ví dụ: tất cả các kiểu có liên quan đến TextBlocks phải nằm trong một tệp có tên là TextBlockStyles.xaml

Làm cách nào để làm điều này trong wpf?

Làm cách nào để liên kết kiểu trong dự án của tôi?

Cảm ơn trước

Trả lời

17

Bạn sử dụng từ điển tài nguyên sáp nhập

Trong bạn App.xaml bạn sẽ sử dụng

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary 
       Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/> 
      ... other dictionaries here 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

hoặc trực tiếp vào một usercontrol sẽ

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary 
       Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/> 
      ... other dictionaries here 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 

Bạn có thể rút ngắn Source="..." thành chỉ Source="TextBlockStyles.xaml" nếu tệp nằm trong cùng một cụm và trong thư mục gốc của dự án, hoặc cách khác Source="Styles\TextBlockStyles.xaml" nếu bạn đặt từ điển tài nguyên vào thư mục Styles.

+0

Vậy tệp có tên là 'TextBlockStyles.xaml' trông như thế nào? –

+0

@TheMuffinMan: Ví dụ [có thể tìm thấy tại đây] (https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/resourcedictionary-and-xaml-resource-references#merged-resource từ điển). (Nó được gọi là 'Dictionary1.xaml' trong ví dụ này.) – kmote

0

bạn đang tìm kiếm tài nguyên động. cách tốt nhất là tải và dọn lớn từ điển trong tài nguyên. ứng dụng hoặc trên trang điều khiển. đây là một mẫu tốt cho nó.

http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-resources-in-a-custom-control-library.aspx

<ResourceDictionary> 

    <ResourceDictionary.MergedDictionaries> 

    <ResourceDictionary Source="Dictionary1.xaml"/> 

    </ResourceDictionary.MergedDictionaries> 

</ResourceDictionary> 

this.Resources.MergedDictionaries.Add (Smyresourcedictionary);

Các vấn đề liên quan