如何防止覆盖UserControl的内容?

时间:2016-01-31 04:24:33

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

我是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设计器中呈现时,就好像我的用户控件的内容已完全被删除并被主页中指定的内容替换。为了给你一个想法,这是设计师的截图:

enter image description here

正如您所看到的,没有菜单或SplitView或我在自定义控件中添加的内容;唯一出现的是我在{1}}的XAML中指定的TextBlock

无论如何,为什么会发生这种情况,我该怎么做才能解决它?

编辑:找到了我正在寻找的解决方案here,但Peter Duniho的答案已被接受,因为它可以更好地解释发生了什么

1 个答案:

答案 0 :(得分:3)

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

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

但无论你想做什么,你都必须在不使用和覆盖Content的隐含UserControl属性的当前值的情况下执行此操作。 Page XAML。

相关问题