标签:uwp windows-10 win-universal-app xaml c
我是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的子类(类似于WPF的ContentControl).它只能有一个孩子.因此,您将自己明确覆盖内容.通过在Page XAML中为AppView指定子元素,可以设置控件的内容.这优先于UserControl自己的XAML中指定的任何内容.
不幸的是,还不清楚您期望发生什么.也许您想提供其他内容的属性,AppView类可以使用该属性将其添加到自己的内容中.或者,也许您应该允许客户端代码提供DataTemplate和某种数据项对象(例如,模型类),这些内容可用于ContentPresenter或AppView XAML中的类似对象.
但是,无论您想做什么,都必须在不使用并覆盖Page XAML中UserControl的隐式Content属性的当前值的情况下进行.
标签:uwp,windows-10,win-universal-app,xaml,c 来源: https://codeday.me/bug/20191119/2033517.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。