C# 未在pivot windows phone 8.1中加载数据模板
我试图在wp8.1应用程序(UNIAPP)中的透视项中显示不同的布局。理想情况下,我希望加载不同的页面,但既然我能弄明白这一点,我想我应该先尝试一下基础知识,因为我以前会使用它,但由于某种原因,我无法让它工作 My pivot项是基于提供的ViewModel动态加载的C# 未在pivot windows phone 8.1中加载数据模板,c#,windows-phone-8,mvvm,windows-phone-8.1,C#,Windows Phone 8,Mvvm,Windows Phone 8.1,我试图在wp8.1应用程序(UNIAPP)中的透视项中显示不同的布局。理想情况下,我希望加载不同的页面,但既然我能弄明白这一点,我想我应该先尝试一下基础知识,因为我以前会使用它,但由于某种原因,我无法让它工作 My pivot项是基于提供的ViewModel动态加载的 <Pivot.ItemTemplate> <DataTemplate> <controls:DataTemplateSelector Content="{Binding}"
<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>
My DataTemplateSelector的定义如下:
public class DataTemplateSelector : ContentControl
{
protected override void OnContentChanged(object oldContent,
object newContent)
{
ContentTemplate = this.FindResource<DataTemplate>(newContent.GetType
().FullName);
}
}
公共类DataTemplateSelector:ContentControl
{
受保护的覆盖无效OnContentChanged(对象oldContent,
对象(新内容)
{
ContentTemplate=this.FindResource(newContent.GetType
()全名);
}
}
每当我转到一个新的透视项时,它都会被触发,但ContentTemplate总是空的
newContent.GetType().FullName
返回相关的viewmodel名称,我可以看到该名称显示在相关的透视图中
我注意到的一点是,当我通过this.resources.count()检查DataTemplateSelector类(this)时,它没有任何资源,因此它显然找不到这些资源,但如何解决这个问题
更新:
数据透视项中未加载我的数据模板。.NET IDE显然存在问题,因为每当我从Content=“{Binding}”添加或删除一个时,它都会在pivot项中显示按钮,但该按钮在IDE中。不幸的是,在运行时,它只显示我的viewmodel的名称
虽然IDE中的行为不稳定,但在处理Content=“{Binding”
时,我的DataTemplate中的按钮会显示出来,这会让您认为代码和xaml是正确的,但在运行时它肯定不起作用
知道为什么我的数据模板没有显示在透视项中有什么问题吗
谢谢。这是一个部分答案。我的意思是我确实找到了解决问题的方法,但我没有解决问题本身 My DataTemplateSelector,每当pivot更改时会触发它调用名为FindResource的扩展函数:
public static class ControlExtensions
{
public static T FindResource<T>(this DependencyObject initial,
string key) where T : DependencyObject
{
DependencyObject current = initial;
while (current != null)
{
if (current is FrameworkElement)
{
if ((current as FrameworkElement).Resources.
ContainsKey(key))
{
return (T)(current as FrameworkElement).Resources[key];
}
}
current = VisualTreeHelper.GetParent(current);
}
if (Application.Current.Resources.ContainsKey(key))
{
return (T)Application.Current.Resources[key];
}
return default(T);
}
}
我第二次这样做时,FindResources的第二部分就开始了,因为不管发生什么,当前对象总是空的
if (Application.Current.Resources.ContainsKey(key))
{
return (T)Application.Current.Resources[key];
}
它会找到相关的数据模板,并根据相关的PivotItem ViewModel在我的pivot控件中相应地显示它
现在,我还没有脱离困境,因为我不知道绑定到相关viewmodel是否会起作用,但这完全是另一回事
如果有人知道为什么在Pages.Resources或Grid.Resources中定义时找不到DataTemplate
,请更新帖子,因为我想知道原因
谢谢。你能编辑你的帖子并澄清你的问题吗?只是澄清了帖子。对不起!
if (Application.Current.Resources.ContainsKey(key))
{
return (T)Application.Current.Resources[key];
}