C# 获取当前选定全景项目中的图像

C# 获取当前选定全景项目中的图像,c#,xaml,windows-phone-8,findname,C#,Xaml,Windows Phone 8,Findname,我有一个全景图,它根据数据绑定获取动态数量的项目。在创建的全景项目中,我有一个图像,我想随时间修改它(随时间淡入/淡出不同的图像) 我看到类似的问题如下: 但这对我不起作用,因为当我使用Panorama.Items[index]时,我从来没有得到过全景项目。我只得到我使用的数据绑定的一个实例 我来自Android世界,在那里我可以简单地使用findViewById()之类的东西来获得特定的视图。WP8中有类似的内容吗?我确实知道FindName(),但在我的全景图上使用的方法只会返回null 所

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

我看到类似的问题如下: 但这对我不起作用,因为当我使用
Panorama.Items[index]
时,我从来没有得到过全景项目。我只得到我使用的数据绑定的一个实例

我来自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>

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

Image newImg = new Image();
newImg = LayoutRoot.FindName("GalleryPreview") as Image;
可能就是你要找的

模板有一个自包含的名称范围。这是因为模板被重复使用,并且一旦控件的多个实例各自实例化了模板,模板中定义的任何名称都不能保持唯一。调用GetTemplateChild,以便在实例化模板后返回对来自模板的对象的引用。您不能使用FindName从模板中查找项,因为FindName在更一般的范围内工作,并且一旦应用了ControlTemplate类,ControlTemplate类本身与实例化模板之间就没有连接

最后一次尝试
@wankr引导我朝着正确的方向前进,尽管他的链接并没有完全帮助我

我想访问一个图像以更改计时器上显示的源

这是my 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]);

多重回答并不是这个问答的工作方式。。。不管怎样,我用那种方法有困难。这是一个内部的,我不能简单地在我的全景对象上使用它。我对c#和wp8还是相当陌生的,有什么建议可以让它工作吗?据我所知,只有在我创建自己的控件时,这种方法才会工作。我不想那样做。我只想使用一个普通的DataTemplate作为ItemTemplate并访问其中的一个图像…您可以链接返回null的FindName()部分吗?添加了另一个链接,可能会对您有所帮助链接中的代码无法正常工作,因为我在Windows Phone 8上的DataTemplate中没有FindName方法,但它会将我发送到正确的方向。我将发布我的工作代码。
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]);
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;
}