使用资源字典合并 xaml 个文件
Merge xaml files using resource dictionaries
关于合并两个 XAML 文件的信息有点令人困惑,有人可以指出正确的方向吗? (W10, VS2017, UAP, Midi)
我有 app.xaml
、MainPage.xaml
和 A.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 又名 (XAMLWrapper.xaml
)
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
提前感谢您的帮助。也许 app.xaml 需要引用资源字典 and/or 将 "merged" 标签放入 MainPage 和 A。或者 MainPage 和 A 也必须 be/act 喜欢字典?
我已经更新了我的代码,但我仍然没有看到列表框
我不确定您尝试使用资源字典是否可行。资源字典就是这样,是您的 XAML 资源的列表,因此您可以在其中定义样式或模板等。然后,使用该资源的 XAML 会引用这些内容。我从来没有尝试过在其中定义元素,但我认为您不能用它来初始化网格(我认为您正在尝试这样做?)。
编辑:
结合评论重新阅读您的原文 post 我认为您根本不需要资源词典。你想要的是一个单独的用户控件。您可以找到有关此 here 的信息,但作为一个简短示例,我创建了一个 WPF 项目并添加了 2 个用户控件,1 个包含列表,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 where you can see the content is being changed based on which template is applicable. You could define the templates in your resource dictionary and reference them that way. I think this 也值得参考,因为虽然与您的问题没有直接关系,但它概述了您可以在应用程序中分割内容的各种方式并进行了一些相关讨论。
关于合并两个 XAML 文件的信息有点令人困惑,有人可以指出正确的方向吗? (W10, VS2017, UAP, Midi)
我有 app.xaml
、MainPage.xaml
和 A.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 又名 (XAMLWrapper.xaml
)
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
提前感谢您的帮助。也许 app.xaml 需要引用资源字典 and/or 将 "merged" 标签放入 MainPage 和 A。或者 MainPage 和 A 也必须 be/act 喜欢字典? 我已经更新了我的代码,但我仍然没有看到列表框
我不确定您尝试使用资源字典是否可行。资源字典就是这样,是您的 XAML 资源的列表,因此您可以在其中定义样式或模板等。然后,使用该资源的 XAML 会引用这些内容。我从来没有尝试过在其中定义元素,但我认为您不能用它来初始化网格(我认为您正在尝试这样做?)。
编辑:
结合评论重新阅读您的原文 post 我认为您根本不需要资源词典。你想要的是一个单独的用户控件。您可以找到有关此 here 的信息,但作为一个简短示例,我创建了一个 WPF 项目并添加了 2 个用户控件,1 个包含列表,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 where you can see the content is being changed based on which template is applicable. You could define the templates in your resource dictionary and reference them that way. I think this 也值得参考,因为虽然与您的问题没有直接关系,但它概述了您可以在应用程序中分割内容的各种方式并进行了一些相关讨论。