页面只能有 Frame 作为父级,不能有 Window [英] Page can have only Frame as parent and not Window
问题描述
继续讨论这个问题:App 中的空引用错误.xaml
MVVM light,我正在制作一个 WPF 应用程序,我将在我的 Window
上添加一个 Page
元素.为此,我使用下一个代码:
Continuing on this question: Null reference error in App.xaml
MVVM light, I'm making a WPF application and I'll add a Page
-element on my Window
. For this I'm using next code:
<Window x:Class="Porject.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:Project"
xmlns:pages="clr-namespace:Project.Pages"
mc:Ignorable="d"
DataContext="{StaticResource appvm}"
Title="Project" Height="450" Width="800">
<Page Content="{Binding CurrentPage, Mode=TwoWay}" />
</Window>
然而,这给了我这个例外:
However, this gives my this exception:
InvalidOperationException
:Page
只能有 Window
或 Frame
作为父级.
InvalidOperationException
:Page
can have onlyWindow
orFrame
as parent.
因此您可以在第一个代码块中看到 Page
的父级是一个 Window
.如果我将 Page
元素放在 Frame
(如下面的代码) 中,则不会抛出异常.
So you can see in the first code block the parent of the Page
is a Window
. If I place the Page
-element in a Frame
(like code below), the exception doesn't throw.
<!-- Opening Window tag with all attributes -->
<Frame>
<Frame.Content>
<Page Content="{Binding CurrentPage, Mode=TwoWay}" />
</Frame.Content>
</Frame>
<!-- Closing Window tag -->
所以你可以看到,Page
元素只能有一个 Frame
作为父元素.但是为什么说异常…只有一个Window
或 Frame
作为父级"?一定是这样的:
So you can see, the Page
-element can only have a Frame
as parent. But why says the exception "… only a Window
or Frame
as parent"? Must it be this:
InvalidOperationException
:Page
只能有 Window
或 Frame
作为父级.
InvalidOperationException
:Page
can have onlyWindow
orFrame
as parent.
推荐答案
找到了,MainWindow
上的代码一定是这样的:
Found it, the code on the MainWindow
must be this:
<!-- Opening Window tag with all attributes -->
<Frame Content="{Binding CurrentPage, Mode=TwoWay}" />
<!-- Closing Window tag -->
这也会在窗口上显示 StartScreenPage
.
This will also show the StartScreenPage
on the window.
这篇关于页面只能有 Frame 作为父级,不能有 Window的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!