在当前选择的 PanoramaItem 中获取图像 [英] Get Image in currently selected PanoramaItem
问题描述
我有一个全景图,它根据数据绑定获取动态数量的项目.在创建的 PanoramaItem 中,我有一个我想随时间修改的图像(随着时间的推移淡入/淡出不同的图像).
I have a Panorama which gets a dynamic amount of items based on data binding. In the created PanoramaItem, I have an Image which I want to modify over time (fade in/out different images over time).
我看到了类似的问题:如何在运行时检索 Panorama-Item 的名称?但这对我不起作用,因为我在使用 Panorama.Items[index]
时从未获得 PanoramaItem.我只得到我使用的数据绑定的一个实例.
I saw similar questions like this: How to retrieve the name of a Panorama-Item at runtime?
but that does not work for me as I never get a PanoramaItem when I use Panorama.Items[index]
. I only get an instance of the data binding I use.
我来自 Android 世界,在那里我可以简单地使用诸如 findViewById()
之类的东西来获取特定视图.WP8 有没有类似的东西?我确实知道 FindName()
但在我的全景图中使用的只是返回 null...
I come from the Android world where I could simple use something like findViewById()
to get a specific view. Is there anything similar in WP8? I do know FindName()
but that used on my Panorama simply returns null...
所以我的问题是:如何在代码隐藏文件中获得名为 GalleryPreview
的图像的引用?
So my question is: How can I get a reference of Image named GalleryPreview
in the code behind file?
我的页面 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
推荐答案
@wankr 引导我走向正确的方向,尽管他的链接并没有完全帮助我.
@wankr guided me in the right direction though his link didn't fully helped me.
我想访问图像以更改基于计时器的显示源.
I wanted to access an Image to change the displayed source on a timer base.
这是我的 OnTick 包含的用于获取图像引用的内容:
This is what my OnTick contains to get the Image reference:
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
.
"GalleryPreview" is the name of the Image I want to access. As I have multiple Images in my layout, I needed to extend the method FindVisualChild
I found on the link from @wankr.
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;
}
这篇关于在当前选择的 PanoramaItem 中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!