【问题标题】:Get Image in currently selected PanoramaItem在当前选中的 PanoramaItem 中获取图像
【发布时间】:2013-06-24 19:40:40
【问题描述】:

我有一个全景图,它根据数据绑定获取动态数量的项目。在创建的 PanoramaItem 中,我有一个想要随时间修改的图像(随着时间的推移淡入/淡出不同的图像)。

我看到类似这样的问题:How to retrieve the name of a Panorama-Item at runtime? 但这对我不起作用,因为当我使用 Panorama.Items[index] 时,我从来没有得到 PanoramaItem。我只得到了我使用的数据绑定的一个实例。

我来自 Android 世界,我可以简单地使用 findViewById() 之类的东西来获取特定视图。 WP8有类似的吗?我知道FindName(),但在我的全景图中使用的只是返回 null...

所以我的问题是:如何在文件后面的代码中获取名为 GalleryPreview 的图像的引用?

我的页面 xaml

<phone:PhoneApplicationPage
    x:Class="App.View.AppDetailsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="False" >

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="PanoramaHeaderTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="72"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding AppStoreIcon}"
                       Width="64" Height="64"
                       Grid.Column="0"
                       HorizontalAlignment="Left"/>
                <TextBlock x:Name="ItemTitle"
                           Text="{Binding os}"
                           Grid.Column="1"/>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="PanormaItemTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding storeName}" FontSize="24"
                           Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>

                <TextBlock Text="Rating" FontSize="24"
                           Grid.Row="1" Grid.Column="0"/>
                <TextBlock Text="{Binding rating}" FontSize="24"
                           Grid.Row="1" Grid.Column="1"/>

                <TextBlock Text="5 Stars: " FontSize="24"
                           Grid.Row="2" Grid.Column="0"/>
                <TextBlock Text="{Binding starsFive}" FontSize="24"
                           Grid.Row="2" Grid.Column="1"/>
                <TextBlock Text="4 Stars: " FontSize="24"
                           Grid.Row="3" Grid.Column="0"/>
                <TextBlock Text="{Binding starsFour}" FontSize="24"
                           Grid.Row="3" Grid.Column="1"/>
                <TextBlock Text="3 Stars: " FontSize="24"
                           Grid.Row="4" Grid.Column="0"/>
                <TextBlock Text="{Binding starsThree}" FontSize="24"
                           Grid.Row="4" Grid.Column="1"/>
                <TextBlock Text="2 Stars: " FontSize="24"
                           Grid.Row="5" Grid.Column="0"/>
                <TextBlock Text="{Binding starsTwo}" FontSize="24"
                           Grid.Row="5" Grid.Column="1"/>
                <TextBlock Text="1 Stars: " FontSize="24"
                           Grid.Row="6" Grid.Column="0"/>
                <TextBlock Text="{Binding starsOne}" FontSize="24"
                           Grid.Row="6" Grid.Column="1"/>

                <StackPanel Grid.ColumnSpan="2" Grid.Row="7"
                            ManipulationStarted="AppItemManipulationStarted"
                            ManipulationCompleted="AppItemManipulationCompleted">
                    <Image x:Name="GalleryPreview" Source="/Assets/images/blub.png"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot">
        <phone:Panorama x:Name="AppPanorama" Title="Title"
                        ItemsSource="{Binding}"
                        SelectionChanged="PanoramaSelectionChanged"
                        HeaderTemplate="{StaticResource PanoramaHeaderTemplate}"
                        ItemTemplate="{StaticResource PanormaItemTemplate}">
        </phone:Panorama>
    </Grid>

</phone:PhoneApplicationPage>

当我尝试使用FindName("GalleryPreview")

LogUtil.Log(AppPanorama.FindName("GalleryPreview") == null ? "null" : AppPanorama.FindName("GalleryPreview"));
LogUtil.Log(LayoutRoot.FindName("GalleryPreview") == null ? "null" : LayoutRoot.FindName("GalleryPreview"));
LogUtil.Log(AppPanorama.SelectedItem);

// output:
// null
// null
// App.Model.Store

【问题讨论】:

    标签: c# xaml windows-phone-8 findname


    【解决方案1】:

    Control.GetTemplateChild 可能就是您要找的。​​p>

    模板具有独立的名称范围。这是因为模板被重复使用,并且一旦控件的多个实例分别实例化它们的模板,模板中定义的任何名称都不能保持唯一。调用 GetTemplateChild 以便在实例化模板后返回对来自模板的对象的引用。您不能使用 FindName 从模板中查找项目,因为 FindName 的作用范围更广,而且 ControlTemplate 类本身与应用后的实例化模板之间没有任何联系。

    最后一次尝试 How to: Find DataTemplate-Generated Elements

    【讨论】:

    • 多重答案并不是这个问答的真正运作方式......无论如何我在使用这种方法时遇到了麻烦。这是一个内部的,我不能简单地在我的全景对象上使用它。我对 c# 和 wp8 还是很陌生,有什么技巧可以使它工作吗?
    • 据我所知,这种方法只有在我创建自己的控件时才有效。我不想那样做。我只想使用普通的 DataTemplate 作为 ItemTemplate 并在那里访问一个图像......
    • 你能链接你返回 null 的 FindName() 部分吗?
    • 添加了另一个可能对您有帮助的链接
    • 链接中的代码无法正常工作,因为我在 Windows Phone 8 的 DataTemplate 中没有 FindName 方法,但它向我发送了正确的方向。我将发布我的工作代码。
    【解决方案2】:

    试试这个,看看是否有帮助:

    Image newImg = new Image();
    newImg = LayoutRoot.FindName("GalleryPreview") as Image;
    

    【讨论】:

      【解决方案3】:

      @wankr 引导我朝着正确的方向前进,尽管他的链接并没有完全帮助我。

      我想访问一个图像来更改计时器基础上显示的源。

      这是我的 OnTick 包含的用于获取图像参考的内容:

      ImageSourceConverter converter = new ImageSourceConverter();
      PanoramaItem myPanoramaItem = (PanoramaItem)(AppPanorama.ItemContainerGenerator.ContainerFromItem(AppPanorama.Items[_selectedIndex]));
      Image galleryItem = (Image)FindVisualChild<Image>("GalleryPreview", myPanoramaItem);
      galleryItem.Source = (ImageSource)converter.ConvertFromString(gallery[_galleryIndex]);
      

      “GalleryPreview”是我要访问的图像的名称。由于我的布局中有多个图像,我需要扩展我在@wankr 的链接上找到的方法FindVisualChild。

      public static ChildItem FindVisualChild<ChildItem>(String name, DependencyObject obj) where ChildItem : DependencyObject
      {
          for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
          {
              DependencyObject child = VisualTreeHelper.GetChild(obj, i);
              FrameworkElement uiChild = (FrameworkElement)child;
              if (child != null && child is ChildItem && uiChild.Name.Equals(name))
              {
                  return (ChildItem)child;
              }
              else
              {
                  ChildItem childOfChild = FindVisualChild<ChildItem>(name, child);
                  if (childOfChild != null)
                  {
                      return childOfChild;
                  }
              }
          }
          return null;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-12
        • 2017-11-08
        • 2014-02-04
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        相关资源
        最近更新 更多