无法使用单独的文件解析资源

时间:2022-09-04 11:15:44

I feel like this is a common sense and trivial, but i don't understand what i'm doing to begin with. I don't have any other resource I can use for help either. Sometimes i wonder if I'm even googling the question right.

我觉得这是一个常识和琐碎,但我不明白我在做什么开始。我没有任何其他资源可以用来帮忙。有时我想知道我是否正确地搜索了这个问题。

I have some custom styles & templates I've made, but now the file is rather large and difficult to work with. I want to put each style or template in there own XAML files (sorta like headers/implementation files) so that a friend could work on one and then we add it to the project. (Such as Dictionary1.xaml ... ). I started a blank project to keep it simple.

我有一些自定义样式和模板,但现在文件相当大,难以使用。我想将每个样式或模板放在自己的XAML文件中(类似于标题/实现文件),以便朋友可以处理一个,然后我们将它添加到项目中。 (例如Dictionary1.xaml ...)。我开始了一个空白的项目,以保持简单。

Dictionary1.XAML

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary x:Key="SomeKey">
    <Color x:Key="detailMark">Black</Color>
    <SolidColorBrush x:Key="detailMarkBrush" Color="{StaticResource ResourceKey=detailMark}" />
    <Style x:Key="flatTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Rectangle  Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="1"/>
                        <TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

App.XAML

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <local:MainWindow x:Key="SomeKey"/>
    </Application.Resources>

</Application>

And MainWindow.XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" 
        Height="350" Width="525">


    <Grid>
        <TextBox Style="{DynamicResource flatTextBox}"> <!-- doesn't autocomplete/work -->

        </TextBox>

    </Grid>
</Window>

Edit:

<TextBox Style="{Binding Mode=OneWay, Source={StaticResource SomeKey}}">
        <!-- Throws System.Windows.Markup.XamlParseException  -->

    </TextBox>

1 个解决方案

#1


As Alex mentioned, the right way to do this is using Merged Dictionaries.
Therefore, you should structure your project correctly, otherwise it will end up in a mess.

正如Alex所说,正确的方法是使用Merged Dictionaries。因此,您应该正确地构建项目,否则它将最终陷入混乱。

Keeping your "blank project", it should look like this:

保持你的“空白项目”,它应该是这样的:

  • YourProject
    • App.xaml (.cs)
    • MainWindow.xaml (.cs)
    • SomeOtherWindow.xaml (.cs)
    • Resources folder
      • Dictionary1.xaml
      • Dictionary2.xaml
      • ...
    • 资源文件夹Dictionary1.xaml Dictionary2.xaml ...

  • YourProject App.xaml(.cs)MainWindow.xaml(.cs)SomeOtherWindow.xaml(.cs)资源文件夹Dictionary1.xaml Dictionary2.xaml ...

Then you have to decide:

然后你必须决定:

  1. Do you want the resources to be available application wide?
  2. 您是否希望应用程序范围内的资源可用?

  3. Or do you want the resources to vary between certain windows / user controls?
  4. 或者您希望某些窗口/用户控件之间的资源有所不同吗?


If you want #1, you have to merge the dictionaries in the App.xaml file:

如果您想要#1,则必须合并App.xaml文件中的词典:

<Application x:Class=...
             ...>
    <Application.Resources>        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Dictionary1.xaml"/>
                <ResourceDictionary Source="Resources/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

If you want #2, you have to merge the dictionaries in the specific window / user control file:

如果你想要#2,你必须合并特定窗口/用户控制文件中的字典:

<Window x:Class=...
        ...>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Dictionary1.xaml"/>
                <ResourceDictionary>
                    <!-- Window specific resources -->
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <!-- Content -->
</Window>

#1


As Alex mentioned, the right way to do this is using Merged Dictionaries.
Therefore, you should structure your project correctly, otherwise it will end up in a mess.

正如Alex所说,正确的方法是使用Merged Dictionaries。因此,您应该正确地构建项目,否则它将最终陷入混乱。

Keeping your "blank project", it should look like this:

保持你的“空白项目”,它应该是这样的:

  • YourProject
    • App.xaml (.cs)
    • MainWindow.xaml (.cs)
    • SomeOtherWindow.xaml (.cs)
    • Resources folder
      • Dictionary1.xaml
      • Dictionary2.xaml
      • ...
    • 资源文件夹Dictionary1.xaml Dictionary2.xaml ...

  • YourProject App.xaml(.cs)MainWindow.xaml(.cs)SomeOtherWindow.xaml(.cs)资源文件夹Dictionary1.xaml Dictionary2.xaml ...

Then you have to decide:

然后你必须决定:

  1. Do you want the resources to be available application wide?
  2. 您是否希望应用程序范围内的资源可用?

  3. Or do you want the resources to vary between certain windows / user controls?
  4. 或者您希望某些窗口/用户控件之间的资源有所不同吗?


If you want #1, you have to merge the dictionaries in the App.xaml file:

如果您想要#1,则必须合并App.xaml文件中的词典:

<Application x:Class=...
             ...>
    <Application.Resources>        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Dictionary1.xaml"/>
                <ResourceDictionary Source="Resources/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

If you want #2, you have to merge the dictionaries in the specific window / user control file:

如果你想要#2,你必须合并特定窗口/用户控制文件中的字典:

<Window x:Class=...
        ...>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Dictionary1.xaml"/>
                <ResourceDictionary>
                    <!-- Window specific resources -->
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <!-- Content -->
</Window>