【问题标题】:DataTemplate not loading in pivot windows phone 8.1DataTemplate 未在枢轴 Windows Phone 8.1 中加载
【发布时间】: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&lt;space&gt;" 混在一起时显示的事实会让您认为代码和 xaml 是正确的,但它绝对不能在运行时工作.

知道为什么我的 DataTemplates 没有显示在数据透视项中是怎么回事吗?

谢谢。

【问题讨论】:

  • 您能否编辑您的帖子并澄清您现在的问题/问题到底是什么?
  • 刚刚澄清了帖子。对不起!

标签: c# windows-phone-8 mvvm windows-phone-8.1


【解决方案1】:

这是部分答案。我的意思是我确实找到了解决问题的方法,但我没有解决问题本身。

我的 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);
    }
}

由于某些奇怪的原因,Windows Phone 8.1 (WinRT) 不喜欢在 WP8/WP8.1 Silverlight 中加入数据模板。

如前所述,这在 IDE 中是不稳定的,它有时会显示 DataTemplate,有时它并不取决于我是否在 Content="{Binding}" 的 Binding 关键字后添加空格。可以肯定的一件事是它永远不会在运行时工作,至少不能在上面的代码中工作。

VisualTreeHelper.GetParent(current) 无论如何都会返回null。我在调试时检查了我是否可以访问资源,但无济于事。

我是如何解决的?好吧,我将我的数据模板移到了资源字典中

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

我这样做的第二个部分,我的 FindResources 的第二部分开始了,因为 Current 对象始终为空,无论如何

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

它会找到相关的 DataTemplate 并根据相关的 PivotItem ViewModel 在我的数据透视控件中相应地显示它。

现在,我还没有走出困境,因为我不知道绑定到相关视图模型是否会起作用,但那是另一回事了!

如果有人知道为什么在 Pages.Resources 或 Grid.Resources 中定义时找不到 DataTemplate,请更新帖子,我很想知道原因。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多