简体   繁体   English

DataTemplate未在数据透视Windows Phone 8.1中加载

[英]DataTemplate not loading in pivot windows phone 8.1

I'm trying to display different layouts in pivot items in a wp8.1 app (UNIAPP ). 我正在尝试在wp8.1应用程序(UNIAPP)的枢轴项目中显示不同的布局。 Ideally I would like to load different pages but since I could figure this out, I thought I'd try with the basics first as I'd use this before but for some reason I can't get this to work. 理想情况下,我想加载不同的页面,但是由于我可以弄清楚这一点,因此我认为我会首先尝试基础知识,因为之前我会使用它,但是由于某些原因,我无法使它起作用。

My pivot items are loaded dynamically based on the provided ViewModel 我的数据透视项基于提供的ViewModel动态加载

 <Pivot.ItemTemplate>
   <DataTemplate>
     <controls:DataTemplateSelector Content="{Binding}"
      HorizontalContentAlignment="Stretch"
      VerticalContentAlignment="Stretch">
    </controls:DataTemplateSelector>
   /DataTemplate>
 </Pivot.ItemTemplate>

My resources are defined as follows within the same xaml page 我的资源在同一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 is defined as follows: 我的DataTemplateSelector定义如下:

public class DataTemplateSelector : ContentControl
{
  protected override void OnContentChanged(object oldContent, 
  object newContent)
  {
    ContentTemplate = this.FindResource<DataTemplate>(newContent.GetType
    ().FullName);
  }
}

It is being triggered whenever I go to a new pivot item, but the ContentTemplate is always null. 每当我转到新的数据透视项目时,都会触发该事件,但是ContentTemplate始终为null。

The newContent.GetType().FullName returns the relevant viewmodel name which I can see being displayed in the relevant pivot. newContent.GetType().FullName返回相关的视图模型名称,我可以看到该名称显示在相关的数据透视图中。

One thing I noticed is that the DataTemplateSelector class (this) has no resources when I check it via this.Resources.count(), so it's obviously not finding them but how do I fix this? 我注意到的一件事是,当我通过this.Resources.count()检查它时,DataTemplateSelector类(this)没有资源,因此显然找不到它们,但是我该如何解决呢?

UPDATE: 更新:

My DataTemplates are not getting loaded in my Pivot Items. 我的DataTemplates没有加载到我的数据透视项目中。 There is obviously a problem with the .NET IDE as whenever I add or remove a from Content="{Binding}" it displays the button within the pivot item but that's within the IDE. .NET IDE显然存在问题,因为每当我在Content =“ {Binding}”中添加或从中删除时,它都会在透视表项中显示按钮,但这是在IDE中。 Unfortunately, at run-time, it just displays the name of my viewmodel. 不幸的是,在运行时,它仅显示我的视图模型的名称。

Thought the behaviour is erratic in the IDE, the fact that the button from my DataTemplate is displaying when messing around with the Content="{Binding<space>" would make you think that the code and xaml are correct but it's definitely not working at run-time. 认为该行为在IDE中是不稳定的,当与Content="{Binding<space>"混在一起时,我的DataTemplate的按钮正在显示的事实会让您认为代码和xaml是正确的,但绝对不能在运行。

Any idea what's wrong why my DataTemplates are not displaying in pivot item? 知道为什么我的DataTemplates不显示在数据透视表项中怎么了?

Thanks. 谢谢。

This is a partial answer. 这是部分答案。 By this I mean that I did find a work-around to my problem but I did not resolve the issue itself. 我的意思是,我确实找到了解决该问题的方法,但没有解决问题本身。

My DataTemplateSelector which gets triggered whenever the pivot changes call a extension function called FindResource: 我的DataTemplateSelector每当枢轴更改时都会触发,它调用名为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);
    }
}

For some strange reason, Windows Phone 8.1 (WinRT) does not like having the data templates in while it is not a problem in WP8/WP8.1 Silverlight. 由于某些奇怪的原因,Windows Phone 8.1(WinRT)不喜欢将数据模板放入其中,而这在WP8 / WP8.1 Silverlight中不是问题。

As mentioned, this is unstable in the IDE where it sometimes displays the DataTemplate, and sometimes it doesn't depending on whether or not I add a space after the Binding keyword to the Content="{Binding}" . 如前所述,这在IDE中是不稳定的,在IDE中有时会显示DataTemplate,有时并不取决于是否在Binding关键字后的Content="{Binding}"添加空格。 One thing for sure is that it never works at run-time, well not at least not with the above code. 可以肯定的一件事是,它永远不会在运行时运行,至少在上面的代码中也是如此。

VisualTreeHelper.GetParent(current) always returns null no matter what. 无论如何, VisualTreeHelper.GetParent(current)始终返回null I've checked at debug time if I somehow could access the resources, but to no avail. 我在调试时检查了我是否可以访问资源,但无济于事。

How did I fix it? 我该如何解决? Well, I moved my data templates to a resource dictionary 好吧,我将数据模板移到了资源字典中

<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>

The second I did this, the second part of my FindResources kicks in since the Current object is always null, no matter what 我第二次这样做了,因为无论如何,Current对象始终为null,因此我的FindResources的第二部分开始了

if (Application.Current.Resources.ContainsKey(key))
  {
    return (T)Application.Current.Resources[key];
  }

and it finds the relevant DataTemplate and displays it accordingly in my pivot control based on the relevant PivotItem ViewModel. 并找到相关的DataTemplate并根据相关的PivotItem ViewModel将其显示在我的数据透视控件中。

Now, I'm not out of the woods yet as I have no idea if binding to the relevant viewmodel will work but that's a whole other story! 现在,我还没有走出困境,因为我不知道绑定到相关的视图模型是否会起作用,但这是另外一回事了!

If anyone knows why DataTemplate cannot be found when defined in Pages.Resources or Grid.Resources, please update the post as I'd love to know why. 如果有人知道为什么DataTemplate在Pages.Resources或Grid.Resources定义时无法找到,请更新帖子,因为我很想知道这是为什么。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM