【问题标题】:How to prevent the Content of a UserControl from being overridden?如何防止 UserControl 的内容被覆盖?
【发布时间】:2016-01-31 04:24:32
【问题描述】:

我是 Windows 10 应用开发的新手。我正在尝试将我的自定义 UserControl 嵌入到我的应用程序的页面中。出于某种原因,内容完全被我在 XAML 页面中放入的任何内容所取代。为了给出更好的解释,下面是代码:

AppView.xaml(控件)

<UserControl
    x:Class="Sirloin.AppView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sirloin"
    xmlns:h="using:Sirloin.Helpers">

    <SplitView x:Name="splitView" DisplayMode="CompactOverlay">
        <SplitView.Pane>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <!--The hamburger-->
                <Button Click="OnHamburgerClicked" Grid.Row="0" Style="{StaticResource MenuButtonStyle}">
                    <Button.DataContext>
                        <local:MenuItem Symbol="&#xE700;"/>
                    </Button.DataContext>
                </Button>

                <!--Buttons just below the hamburger-->
                <ListView x:Name="topView" 
                          Grid.Row="1"
                          ItemTemplate="{StaticResource ListViewItemTemplate}"/>

                <!--Buttons toward the bottom of the menu-->
                <ListView x:Name="bottomView" 
                          Grid.Row="3"
                          ItemTemplate="{StaticResource ListViewItemTemplate}"/>
            </Grid>
        </SplitView.Pane>

        <SplitView.Content>
            <!--Where I'd like the content specified in MainPage to appear-->
            <Frame x:Name="frame" Background="White"/>
        </SplitView.Content>
    </SplitView>
</UserControl>

MainPage.xaml

<Page
    x:Class="Sirloin.Example.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="using:Sirloin"
    xmlns:local="using:Sirloin.Example">

    <s:AppView>
        <Grid Background="White">
            <TextBlock HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Text="Hello, world!"/>
        </Grid>
    </s:AppView>
</Page>

MainPage 在 VS 设计器中呈现时,就好像我的用户控件的内容已被完全删除并替换为主页中指定的任何内容。为了给你一个想法,这里是设计师的截图:

如您所见,没有菜单或SplitView 或我在自定义控件中添加的任何内容;唯一存在的是我在 XAML 中为 MainPage 指定的 TextBlock

无论如何,为什么会发生这种情况,我可以做些什么来解决它?


编辑:找到了我一直在寻找的解决方案 here,但 Peter Duniho 的回答已被接受,因为它更好地解释了正在发生的事情。

【问题讨论】:

    标签: c# xaml windows-10 win-universal-app uwp


    【解决方案1】:

    UserControlControl 的子类(类似于 WPF 的ContentControl)。它只能有一个孩子。因此,您自己明确地覆盖了内容。通过在Page XAML 中为AppView 指定子元素,您可以设置控件的内容。这优先于 UserControl 自己的 XAML 中指定的任何内容。

    很遗憾,您预计会发生什么并不是很清楚。也许您想为其他内容提供一个属性,AppView 类可以使用它来添加到它自己的内容中。或者,也许您应该允许客户端代码提供 DataTemplate 和某种数据项对象(例如模型类),这些对象在 ContentPresenterAppView XAML 中的类似内容中使用。

    但无论您尝试做什么,都必须在不使用并覆盖 UserControl XAML 中 UserControl 的隐式 Content 属性的情况下执行它。

    【讨论】:

    • UserControlControl 的子类,而不是 ContentControl。它确实有一个Content dp。
    • @JustinXL:是的,谢谢。我忘记了 Winrt API 将内容控件和 Panel 控件与 WPF 分开。尽管如此,同样的基本问题仍然适用。
    • @Peter Duniho:我认为 James 对用户控制的意思是 MainPage。我认为他希望在打开 MainPage.xaml 的设计器时看到 MainPage 周围的 Splitview 控件。
    猜你喜欢
    • 2017-12-19
    • 2021-02-08
    • 2020-07-23
    • 2018-06-27
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多