【发布时间】:2015-03-21 15:38:39
【问题描述】:
我正在尝试在 wp8.1 应用程序 (UNIAPP) 的数据透视项目中显示不同的布局。理想情况下,我想加载不同的页面,但由于我可以弄清楚这一点,我想我会先尝试基础知识,因为我以前会使用它,但由于某种原因我无法让它工作。
我的数据透视项是根据提供的 ViewModel 动态加载的
<Pivot.ItemTemplate>
<DataTemplate>
<controls:DataTemplateSelector Content="{Binding}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
</controls:DataTemplateSelector>
/DataTemplate>
</Pivot.ItemTemplate>
我的资源在同一个xaml页面中定义如下
<Page.Resources>
<DataTemplate x:Key="MyApp.ViewModel.PIDetailsVM">
<Button Content="test" Foreground="White"></Button>
</DataTemplate>
<DataTemplate x:Key="MyApp.ViewModel.PIListVM">
<Button Content="test" Foreground="White"></Button>
</DataTemplate>
</Page.Resources>
我的 DataTemplateSelector 定义如下:
public class DataTemplateSelector : ContentControl
{
protected override void OnContentChanged(object oldContent,
object newContent)
{
ContentTemplate = this.FindResource<DataTemplate>(newContent.GetType
().FullName);
}
}
每当我转到新的数据透视项目时都会触发它,但 ContentTemplate 始终为空。
newContent.GetType().FullName 返回相关的视图模型名称,我可以看到它显示在相关的枢轴中。
我注意到的一件事是,当我通过 this.Resources.count() 检查 DataTemplateSelector 类 (this) 时,它没有资源,所以显然没有找到它们,但我该如何解决这个问题?
更新:
我的数据模板没有加载到我的数据透视项中。 .NET IDE 显然存在问题,因为每当我从 Content="{Binding}" 添加或删除 a 时,它都会在数据透视项中显示按钮,但它在 IDE 中。不幸的是,在运行时,它只显示我的视图模型的名称。
认为 IDE 中的行为不稳定,我的 DataTemplate 中的按钮在与Content="{Binding<space>" 混在一起时显示的事实会让您认为代码和 xaml 是正确的,但它绝对不能在运行时工作.
知道为什么我的 DataTemplates 没有显示在数据透视项中是怎么回事吗?
谢谢。
【问题讨论】:
-
您能否编辑您的帖子并澄清您现在的问题/问题到底是什么?
-
刚刚澄清了帖子。对不起!
标签: c# windows-phone-8 mvvm windows-phone-8.1