使用资源字典合并XAML文件

3

关于合并两个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>

在遵循如何在WPF中使用C#组合多个XAML文件的方法后,当我将第一个解决方案放入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,又称(XAMLWrapper.xaml

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

非常感谢您的帮助。也许app.xaml需要引用资源字典和/或将“merged”标记放入MainPage和A中。或者MainPage和A也必须像字典一样工作。

我已经更新了我的代码,但仍然看不到列表框。

1个回答

1
我不确定您想要做的事情是否可以使用资源字典完成。资源字典就是一个包含XAML资源的列表,您可以在其中定义样式、模板等内容。这些资源会被您的XAML所引用。我从未尝试在其中定义元素,但我认为您无法使用它来初始化一个网格(这可能是您想要做的事情?)。
编辑:
重新阅读您的原始帖子以及评论,我不认为您真的需要资源字典。您需要的是一个单独的用户控件。您可以在此处找到相关信息,但简短的示例是我创建了一个WPF项目并添加了两个用户控件:一个包含列表,另一个包含按钮。

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>

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

我想做的与HTML的CSS相同,您可以拥有多个样式表。如果您有一个大项目,您不能将所有元素放在同一个XAML表中吗? 否则,您的所有代码都在同一个文件中,这会使事情变得混乱和不整洁。如果XAML不允许多个C#类轻松地与一个XAML表通信,那么面向对象编程的意义何在? 在这里,穿透函数是我的唯一选择吗? - MegaMech
我不想把一堆代码粘贴到与 MainPage 相同的代码文件中。这段代码会向 ListBox 添加内容。我认为我只需要将 ListBox 传递给需要它的类即可。 - MegaMech
我已更新我的回答,我认为这更接近你要问的问题,但如果我误解了你的问题,请原谅。在我最近编写的一个应用程序中,我有一个主视图,包括1个网格和3个内容呈现器,每个呈现器都显示不同的用户控件。我有一些合并的资源字典,但这些仅包含数据模板和控件样式,可让我更改应用程序的主题和标准控件的显示方式。然后,每个用户控件都使用来自这些字典的模板和样式,希望这有助于澄清事情。 - DaeDaLuS_015
非常感谢您的帮助!(如果您给我的问题点赞,我就有足够的积分来点赞您的答案) - MegaMech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接