我正在构建一个 prism 应用程序,我已经使用 MahApps 的 hamburgermenu 设置了 shell。在那个 Hamburgermenu 的内容中,我想要一个区域。
但是当我尝试从“Hamburgermenu.Content”中的 ContentControl 中创建一个区域时,该区域没有被添加。 但是,如果我尝试在 Hamburgermenu 控件之外的 ContentControl 中创建一个区域,它会完美运行。
<Controls:MetroWindow x:Class="SystemCreator.ClientApplication.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:SystemCreator.ClientApplication"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:converters="http://metro.mahapps.com/winfx/xaml/shared"
xmlns:prism="http://prismlibrary.com/"
xmlns:inf="clr-namespace:SystemCreator.ClientApplication.Infrastructure;assembly=SystemCreator.ClientApplication.Infrastructure"
xmlns:cdviews="clr-namespace:SystemCreator.CreateDatabase;assembly=SystemCreator.CreateDatabase"
xmlns:test="clr-namespace:TestModule;assembly=TestModule"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="{Binding Title}" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/HamburgerMenuTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MahApps.Metro.Styles.HamburgerMenu" TargetType="{x:Type Controls:HamburgerMenu}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="HamburgerMenuTemplate">
<Setter.Value>
<DataTemplate>
<!-- PackIconMaterial - Menu -->
<ContentControl Width="22"
Height="22"
Content="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z"
Style="{DynamicResource PathIconContentControlStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="ItemContainerStyle" Value="{StaticResource HamburgerMenuItemStyle}" />
<Setter Property="KeyboardNavigation.ControlTabNavigation" Value="Local" />
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Local" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="Local" />
<Setter Property="OptionsItemContainerStyle" Value="{StaticResource HamburgerMenuItemStyle}" />
<Setter Property="PaneBackground" Value="{DynamicResource MahApps.Metro.HamburgerMenu.PaneBackgroundBrush}" />
<Setter Property="PaneForeground" Value="{DynamicResource MahApps.Metro.HamburgerMenu.PaneForegroundBrush}" />
<Setter Property="Template" Value="{StaticResource HamburgerMenuTemplate}" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type
Controls:HamburgerMenuIconItem}">
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{Binding Icon}"
Focusable="False"
IsTabStop="False" />
<TextBlock Grid.Column="1"
VerticalAlignment="Center"
FontSize="16"
Text="{Binding Label}" />
</Grid>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Controls:HamburgerMenu x:Name="HamburgerMenuControl"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
HamburgerWidth="48"
DisplayMode="CompactInline"
VerticalScrollBarOnLeftSide="False"
ItemTemplate="{StaticResource MenuItemTemplate}"
OptionsItemTemplate="{StaticResource MenuItemTemplate}"
Style="{StaticResource MahApps.Metro.Styles.HamburgerMenu}"
Width="Auto"
>
<Controls:HamburgerMenu.HamburgerMenuHeaderTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="16"
Foreground="White"
Text="Menu" />
</DataTemplate>
</Controls:HamburgerMenu.HamburgerMenuHeaderTemplate>
<!--Content-->
<Controls:HamburgerMenu.Content>
<ContentControl prism:RegionManager.RegionName="{x:Static inf:ApplicationConstants.MainContent}" />
</Controls:HamburgerMenu.Content>
</Controls:HamburgerMenu>
</Grid>
</Controls:MetroWindow>
由于在 Hamburgermenu 内未添加该区域,因此导航无法正常工作。有人知道我可能做错了什么吗?
最佳答案
附加属性 ( RegionManager.RegionName
) 仅适用于立即创建的控件。延迟创建的控件将不会被检测到,因为区域管理器已经完成了对区域的查找。
您需要手动添加区域,在菜单的后面的代码(构造函数)中,如下所示:
RegionManager.SetRegionName( theNameOfTheContentControlInsideTheMenu, WellKnownRegionNames.MenuRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideTheMenu, theRegionManager );
您必须为托管该区域的内容控件分配一个名称,并以某种方式获取区域管理器 ( ServiceLocator.Current.GetInstance<IRegionManager>()
)。
关于c# - 我无法使用 Prism 将 ContentControl 设为一个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53968851/