【问题标题】:Merge xaml files using resource dictionaries使用资源字典合并 xaml 文件
【发布时间】:2019-06-21 00:42:20
【问题描述】:

关于合并两个 XAML 文件的信息有点令人困惑,有人能指出我正确的方向吗? (W10, VS2017, UAP, Midi)

我有app.xamlMainPage.xamlA.xaml。我也有一个 xaml 资源字典。我想将A.xaml 插入MainPage.xaml。 (A其实叫MidiWrapper.xaml

app.xaml

<Application
x:Class="Kawai_ES110_Midi.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

How can i combine multiple XAML files using C# in WPF? 之后,当我将第一个解决方案放入 app.xaml 时出现访问冲突

MainPage.xaml

<Page
x:Class="Kawai_ES110_Midi.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kawai_ES110_Midi"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">


<Grid>
<Grid.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="XAMLWrapper.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Grid.Resources>
</Grid>
</Page>

A.xaml(又名MidiWrapper.xaml

<ResourceDictionary
x:Class="Kawai_ES110_Midi.MidiWrapper"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kawai_ES110_Midi"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <ListBox x:Name="midiInPortListBox"/>
        <ListBox x:Name="midiOutPortListBox"/>
</ResourceDictionary>

资源字典 xaml aka (XAMLWrapper.xaml)

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

提前感谢您的帮助。也许 app.xaml 需要引用资源字典和/或将“合并”标签放入 MainPage 和 A。或者 MainPage 和 A 也必须像字典一样/行为? 我已经更新了我的代码,但我没有看到列表框仍然出现

【问题讨论】:

    标签: c# xaml dictionary resources


    【解决方案1】:

    我不确定您尝试做的事情是否可以使用资源字典。资源字典就是这样,它是您的 XAML 资源的列表,因此您可以在其中定义样式或模板等。然后,使用该资源的 XAML 会引用这些内容。我从来没有尝试过在一个元素中定义元素,但我认为你不能用它初始化一个网格(我认为你正在尝试这样做吗?)。

    编辑:

    结合评论重新阅读您的原始帖子,我认为您根本不需要资源字典。你想要的是一个单独的用户控件。您可以找到有关此here 的信息,但作为一个简短的示例,我创建了一个 WPF 项目并添加了 2 个用户控件,1 个包含列表,一个包含按钮。

    Mainwindow.xaml

    <Window x:Class="test.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:test"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <local:ListUserControl Grid.Column="0" Grid.Row="0"/>
            <local:ButtonUserControl Grid.Column="0" Grid.Row="1"/>
        </Grid>
    </Window>
    

    ListUserControl.xaml

    <UserControl x:Class="test.ListUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:test"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800" Background="White">
        <StackPanel>
            <ListBox>
                <ListBoxItem>ListBox Item #1</ListBoxItem>
                <ListBoxItem>ListBox Item #2</ListBoxItem>
                <ListBoxItem>ListBox Item #3</ListBoxItem>
            </ListBox>
        </StackPanel>
    </UserControl>
    
    

    ButtonUserControl.xaml

    <UserControl x:Class="test.ButtonUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:test"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800" Background="DarkRed">
        <Grid>
            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,10,10" VerticalAlignment="Top" Width="75"/>
        </Grid>
    </UserControl>
    

    这实现了我认为您正在尝试实现的元素之间的分离。因此,这应该允许您组成多个用户控件的视图,其中每个控件都是控件的集合(可能是其他用户控件,但这可能会变得混乱)。这也可以使用here 所述的数据模板来实现,您可以在其中看到内容正在根据适用的模板进行更改。您可以在资源字典中定义模板并以这种方式引用它们。我认为this 也值得参考,虽然它与您的问题没有直接关系,但它概述了您可以在应用程序中分割内容的各种方式,并进行了一些相关的讨论。

    【讨论】:

    • 我要做的与 HTML 的 CSS 相同,您可以在其中拥有多个样式表。如果您有一个大型项目,您不能将所有元素都放在同一个 XAML 工作表中吗? &lt;HEAD&gt; &lt;LINK href="special.css" rel="stylesheet" type="text/css"&gt; &lt;/HEAD&gt; 否则你的所有代码都在同一个文件中,这会使事情变得混乱和无组织。如果 XAML 不允许多个 C# 类轻松地与一张 XAML 表进行通信,那么面向对象编程的意义何在?直通功能是我唯一的选择吗?
    • 我不想将一堆代码粘贴在与 MainPage 相同的代码文件中。此代码添加到 ListBox。我想我只需要将 ListBox 传递给需要它的类。
    • 我已经更新了我的答案,我认为这更接近你的要求,但如果我误解了你的问题,我深表歉意。在我最近编写的一个应用程序中,我有一个由 1 个网格和 3 个内容呈现器组成的主视图,每个呈现器显示不同的用户控件。我有一些合并的资源字典,但它们只包含控件的数据模板和样式,它们允许我更改应用程序的主题和标准控件的显示方式。然后每个用户控件使用这些字典中的模板和样式,我希望这有助于澄清事情。
    • 非常感谢您的帮助! (如果您支持我的问题,我将有足够的积分来支持您的回答)
    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多