簡體   English   中英

我無法使用 Prism 將 ContentControl 設為區域

[英]I am unable to make a ContentControl a region using Prism

我正在構建一個棱鏡應用程序,並且我已經使用 MahApps 的 hamburgermenu 設置了外殼。 在漢堡菜單的內容中,我想要一個區域。

但是,當我嘗試從“Hamburgermenu.Content”中的 ContentControl 中創建一個區域時,不會添加該區域。 但是,如果我嘗試從漢堡菜單控件之外的 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>

由於在漢堡菜單內未添加該區域,因此導航不起作用。 有沒有人知道我可能做錯了什么?

附加屬性 ( RegionManager.RegionName ) 僅適用於立即創建的控件。 不會檢測到延遲創建的控件,因為區域管理器已完成對區域的查找。

您需要在菜單后面的代碼(構造函數)中手動添加區域,如下所示:

RegionManager.SetRegionName( theNameOfTheContentControlInsideTheMenu, WellKnownRegionNames.MenuRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideTheMenu, theRegionManager );

您必須為托管區域的內容控件分配一個名稱,並以某種方式獲取區域管理器( ServiceLocator.Current.GetInstance<IRegionManager>() )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM