如何防止 UserControl 的内容被覆盖?
How to prevent the Content of a UserControl from being overridden?
我是 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=""/>
</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 的回答已被接受,因为它提供了对正在发生的事情的更好解释。
UserControl
是 Control
的子 class(类似于 WPF 的 ContentControl
)。它只能有一个 child。所以你自己明确地覆盖了内容。通过在 Page
XAML 中为 AppView
指定 child 元素,您可以设置控件的内容。这优先于 UserControl
自己的 XAML.
中指定的任何内容
遗憾的是,您并不清楚您希望发生什么。也许您想为其他内容提供 属性,AppView
class 可以使用它来添加自己的内容。或者,也许您应该允许客户端代码提供 DataTemplate
和某种数据项 object(例如模型 class),用于 ContentPresenter
或类似的在 AppView
XAML.
但是无论你想做什么,你都必须在不使用和覆盖 UserControl
的隐式 Content
属性 的当前值的情况下进行在 Page
XAML.
我是 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=""/>
</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 的回答已被接受,因为它提供了对正在发生的事情的更好解释。
UserControl
是 Control
的子 class(类似于 WPF 的 ContentControl
)。它只能有一个 child。所以你自己明确地覆盖了内容。通过在 Page
XAML 中为 AppView
指定 child 元素,您可以设置控件的内容。这优先于 UserControl
自己的 XAML.
遗憾的是,您并不清楚您希望发生什么。也许您想为其他内容提供 属性,AppView
class 可以使用它来添加自己的内容。或者,也许您应该允许客户端代码提供 DataTemplate
和某种数据项 object(例如模型 class),用于 ContentPresenter
或类似的在 AppView
XAML.
但是无论你想做什么,你都必须在不使用和覆盖 UserControl
的隐式 Content
属性 的当前值的情况下进行在 Page
XAML.