加载一个页面到ContentControl中(Load a Page into a ContentC

2019-09-01 05:58发布

我有一个ContentControl中,我要加载的页面myPage2。 我的XAML Codefrom这个页面看起来就像这样:

<Page x:Class="ExampleApp.myPage2">
    <Grid x:Name="Content" Height="651" Width="941" Background="White">
        ...
        ...
    </Grid>
</Page>

我知道,我可以从这个代码的页面加载资源:

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
    var contentControl = (ContentControl)container;
    return (DataTemplate) contentControl.Resources[templateKey];
}

我现在的问题是,我无法加载一个页面像上面这段代码。 我写这篇文章:

<Page x:Class="ExampleApp.myPage2">
    <Page.Resources> 
        <DataTemplate x:Key="Test">       
            <Grid x:Name="Content" Height="651" Width="941" Background="White">
                ...
                ...
            </Grid>
        </DataTemplate>
    </Page.Resources>
</Page>

然后,我可以从上面的代码相同的加载页面templateKey="Test" 。 但主要的问题是,我想使用的页面的第一个声明,不希望使用<Page.Resources> <DataTemplate x:Key="Test">等。 我想从第一次申报direcly加载网站(第一个代码在这个岗位)。 我怎么能直接从页面创建一个DataTemplate? 或者说还有什么其他的方式来一个页面加载到ContentControl中?

Answer 1:

没有理由使用Page一个内ContentControl 。 一个Page是子类UserControl类,增加了一个内使用支持Frame控制,支持导航,后退堆栈/历史等你或许应该替换PageUserControl在XAML和代码背后,所以你会的东西最终会像这样:

<UserControl x:Class="ExampleApp.myControl2">
    <Grid x:Name="Content" Height="651" Width="941" Background="White">
        ...
        ...
    </Grid>
</UserControl>

你可以把UserControl本身在DataTemplate ,如果你想使用它作为一个DataTemplateContentControl

<ContentControl
    xmlns:controls="using:ExampleApp">
    <ContentControl.Resources>
        <DataTemplate
            x:Key="Test">
            <controls:myControl2 />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>


文章来源: Load a Page into a ContentControl