Page can have only Frame as parent and not Window

吃可爱长大的小学妹 提交于 2019-12-13 04:53:41

问题


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 only Window or Frame 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 only Window or Frame 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!