2011-02-18 100 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