问题
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
can have onlyWindow
orFrame
as parent.
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 -->
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
can have onlyWindow
orFrame
as parent.
回答1:
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 -->
This will also show the StartScreenPage
on the window.
来源:https://stackoverflow.com/questions/54018287/page-can-have-only-frame-as-parent-and-not-window