2011-02-18 101 views
0

我理想情況下希望爲資源字典中的窗口保留資源,但是在聲明window.resources部分之前,最好的辦法是讓它們知道。所以我最終做了類似下面的代碼。wpf窗口啓動圖像

有沒有辦法靜態地引用背景圖片?我該如何做得更好?

乾杯,
Berryl

<Window x:Class="Smack.ConstructionAdmin.Presentation.Wpf.Views.ProjectPicker.ProjectPickerView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
... 
    Background="{DynamicResource waveBrush}" 
    Icon="pack://application:,,,/MyAssembly;component/Images/Project_32.png" 
... 
    > 
<Window.Resources> 
    <ImageBrush 
     x:Key="waveBrush" Stretch="Fill" ImageSource="pack://application:,,,/MyAssembly;component\Images\Wave.jpg" 
     /> 
</Window.Resources> 

回答

1

在你的項目中Application.Resources科Application.xaml文件。

您也可以使用獨立的資源文件並將其包含在您的Windows xaml文件或Application.xaml文件中。

<Window x:Class="WpfApplication1.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" Background="{DynamicResource MyBackColor}"> 
    <Window.Resources> 
     <ResourceDictionary Source="Resources\MyResourceDictionary.xaml" /> 
    </Window.Resources> 
    <Grid> 

    </Grid> 
</Window> 

或者

<Application x:Class="WpfApplication1.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary Source="Resources\MyResourceDictionary.xaml" /> 
</Application.Resources> 

+0

嘿,我想我沒有問這個問題非常好。你的第一個片段就是我現在正在做的事情,以及當不同的窗口使用不同資源時我通常想要的內容。我*真的*意味着問我是否可以靜態引用資源而不是動態引用。我可以嗎? – Berryl 2011-02-18 21:20:19