将页面加载到ContentControl中 [英] Load a Page into a ContentControl
问题描述
我有一个ContentControl,我想在其中加载页面 myPage2 。此页面上的XAML代码如下:
I have a ContentControl where I want to load the page myPage2. My XAML Codefrom this page looks like this:
<Page x:Class="ExampleApp.myPage2">
<Grid x:Name="Content" Height="651" Width="941" Background="White">
...
...
</Grid>
</Page>
我知道我可以使用以下代码从页面加载资源:
I know that I can load a resource from a page with this Code:
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
var contentControl = (ContentControl)container;
return (DataTemplate) contentControl.Resources[templateKey];
}
我现在的问题是我无法像上面这样加载页面码。我必须这样写:
My problem now is that I can't load a page like above with this code. I have to write this:
<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从上面加载具有相同代码的页面=测试
。但是主要的问题是我想使用页面的第一个声明,并且不想使用< Page.Resources> < DataTemplate x:Key = Test>
等。我想从第一个声明(本文中的第一个代码)直接加载网站。如何直接从页面创建DataTemplate?还是有其他方法可以将页面加载到ContentControl中?
And then I can load the page with the same Code from above with templateKey="Test"
. But the main problem is that I want to use the first declaration of the page and do not want to use <Page.Resources> <DataTemplate x:Key="Test">
and so on. I want to load the site direcly from the first declaration (first code in this post). How can I create a DataTemplate directly from a page? Or is there an other way to load a page into a ContentControl?
推荐答案
没有理由使用
。 ContentControl
中的页面页面
是 UserControl
类的子类,它增加了对在 Frame中使用的支持。
控件以支持导航,后退堆栈/历史记录等。您可能应该将 Page
替换为 UserControl
在XAML中以及后面的代码,因此您最终会得到这样的东西:
There is no reason to use a Page
within a ContentControl
. A Page
is a subclass of the UserControl
class that adds support for being used within a Frame
control to support navigation, back stack/history, etc. You should probably replace Page
with UserControl
in XAML and code behind, so you would end up with something like this:
<UserControl x:Class="ExampleApp.myControl2">
<Grid x:Name="Content" Height="651" Width="941" Background="White">
...
...
</Grid>
</UserControl>
您可以将 UserControl
本身放在 DataTemplate
如果要在 ContentControl $ c $中用作
DataTemplate
c>:
You can put the UserControl
itself in a DataTemplate
if you want to use it as a DataTemplate
in a ContentControl
:
<ContentControl
xmlns:controls="using:ExampleApp">
<ContentControl.Resources>
<DataTemplate
x:Key="Test">
<controls:myControl2 />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
这篇关于将页面加载到ContentControl中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!