2011-10-13 17 views
5

Tôi đang cố gắng tải kiểu WPF từ tệp khác thực sự từ Thư viện kiểm soát tùy chỉnh WPF nhưng tôi không tải được ở đây là giải pháp của tôi.Đang tải kiểu WPF từ Tệp tài nguyên

Các giải pháp bao gồm hai dự án

  1. WpfTestControls của Loại WPF Thư viện Custom Control

  2. WpfTestApp loại Thư viện ứng dụng WPF trong đó có tham chiếu đến WpfTestControls

MainWindow.xaml từ Thư viện ứng dụng WPF

<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> 

Generic.xaml từ WPF Custom Control Library Thư viện

<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> 

TextBoxStyle.xaml từ WPF Custom Control

<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> 

tập tin AssemblyInfo.cs My chứa sau

[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))] 

Nhưng vẫn tôi đang thất bại trong việc tải các Style. Nếu tôi đang sử dụng không sử dụng Generic.xaml tất cả mọi thứ làm việc tốt ví dụ đoạn mã sau việc như mong đợi

<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> 

am i làm gì sai? Cảm ơn trước

Trả lời

5

Hãy trả lời vài điều cho tôi ...

  1. là "Thư viện Custom Control WPF" lắp ráp giống như "WpfTestControls" lắp ráp?
  2. Nếu không, thì "Thư viện kiểm soát tùy chỉnh WPF" có tham chiếu đến cụm "WpfTestControls" không?
  3. Có phải WpfTestApp của bạn có tham chiếu đến cả cụm "Thư viện điều khiển tùy chỉnh WPF" và "WpfTestControls" không?

Nếu bạn thêm (các) tham chiếu đó, tài nguyên sẽ tải chính xác.

Các bước của tôi ...

  1. Thêm một "Thư viện Custom Control WPF" nói "ThemesLibray"
  2. Trong tiện ích này hai bộ từ điển tài nguyên dưới "Chủ đề" thư mục

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. Tôi có dự án starup chính "MyWPFTestApp" có tài liệu tham khảo lắp ráp để ThemesLibray. Trong đó các cửa sổ có ThemesLibrary nguồn lực sáp nhập theo cách này ....

    <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> 
    

Khi tôi khởi động MyWPFTestApp, tôi thấy các cửa sổ với TextBox màu xanh lá cây.

+0

Xin chào Có lẽ tôi đã viết sai nhưng Solution chứa hai dự án chỉ WpfTestControls của Loại WPF Custom Control Library và WpfTestApp của ứng dụng Wpf loại có tham chiếu hai WpfTestControls. – Robob

+0

Đúng vậy, hãy xem bản chỉnh sửa của tôi ở trên ... –

+0

Bạn nói đúng nhưng tôi hiểu nó phải làm việc với giải pháp trước đó. Đó là cách giải quyết mà tôi sẽ áp dụng cho bây giờ – Robob

1

Điều chính là: Đảm bảo bạn đã đặt Từ điển hoạt động xây dựng từ điển tài nguyên thành Tài nguyên.