wpf 引用另一个 xaml 文件中定义的自定义资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15775111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:45:19  来源:igfitidea点击:

Reference custom resource defined in another xaml file

wpfxamlresourcesstaticresource

提问by user1400716

I am trying to create a new resource in one xaml file and reference it in another xaml file. i.e I define

我正在尝试在一个 xaml 文件中创建一个新资源并在另一个 xaml 文件中引用它。即我定义

<Window.Resources>
    <ImageBrush x:Key="TileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" ImageSource="MyImageButton.png" Opacity="0.3">
    </ImageBrush>
</Window.Resources>

And attempt to use it in another xaml file by

并尝试在另一个 xaml 文件中使用它

<Grid>
    <Button Background="{StaticResource TileBrush}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">
        A Tiled Button
    </Button>
</Grid>

However I get the error "StaticResource reference 'TileBrush' was not found."I can reference the resource from the same xaml file but don't know how to do so from another file.

但是我收到错误“未找到静态资源引用‘TileBrush’。” 我可以从同一个 xaml 文件中引用资源,但不知道如何从另一个文件中引用。

回答by Raúl Ota?o

In WPF, the resource references works as a tree. Each control have resource, and children control can access parent's resources. The global application resource dictionary is in the App.xaml file. In this file you can include several resource dictionaries as a Merged Dictionary. See this code sample:

在 WPF 中,资源引用作为一棵树工作。每个控件都有资源,子控件可以访问父控件的资源。全局应用程序资源字典位于 App.xaml 文件中。在此文件中,您可以包含多个资源词典作为合并词典。请参阅此代码示例:

<?xml version="1.0" encoding="utf-8"?>
<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="View\SomeFileDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

The SomeFileDictionary.xamlis located in the Viewfolder of my project structure. And has looks like this:

SomeFileDictionary.xaml位于在View我的项目结构的文件夹中。并且看起来像这样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:ViewModel="clr-namespace:Cepha.ViewModel"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                ... >

<DataTemplate DataType="{x:Type ViewModel:SomeType}">
    <TextBox .../>
</DataTemplate>...

And each dictionary key or data template defined in this file (or App.xaml), can be referenced in any place of your project. Hope this helps...

并且在此文件(或 App.xaml)中定义的每个字典键或数据模板,都可以在项目的任何位置引用。希望这可以帮助...

回答by Kenneth

You should define this in the app.xaml file. These resources are shared throughout the entire project

您应该在 app.xaml 文件中定义它。这些资源在整个项目中共享