none
LongListSelector中的图片控件问题 RRS feed

  • 问题

  • 我希望用LongListSelector来实现选择游戏关数,可是出现了很奇怪的现象。
    在LongListSelector的"ItemTemplate"中我为每个item创建了两个控件,
    <TextBlock Text="{Binding GS}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="Portable User Interface" Tap="TextBlock_Tap"  />
     <Image Tag="{Binding GS}" Source="/Assets/Tiles/co.png" Visibility="Visible" HorizontalAlignment="Right" Loaded="Image_Loaded_1"/>
    Textblock用于显示关数,图片显示是否通关
    并且在image的Tag绑定关数,便于判断是否通关,Loaded事件用于在进入界面时检测是否通关
     private void Image_Loaded_1(object sender, RoutedEventArgs e)
            {
                Image image = sender as Image;
                string temp = "chuangguan" + image.Tag.ToString().Substring(1, image.Tag.ToString().Length - 2);
                if (Convert.ToInt32(Sto.save_inf[temp]) == 1)
                {
                    image.Visibility = Visibility.Visible;
                }
            }
    刚进入界面时,一切正常,只有第二关通关,因此只有它旁边的图片显示。

    但是在点击索引(就是通讯录的A,B,C,D....的那些键)后,第二关旁边的图片隐藏了,但是变成其它的某个图片显示了,请问这个是怎么回事,或者有没有其它的解决方法
    2014年7月7日 14:38

答案

  • 你好,

    请参考:

    <!--Pivot item one-->
    <phone:PivotItem Header="Browse">
    	<Grid>
    		<phone:LongListSelector Name="llsGameList" ItemsSource="{Binding}" Tap="llsGameList_Tap" Margin="0,90,0,0">
    			<phone:LongListSelector.ItemTemplate>
    				<DataTemplate>
    					<Grid>
    						<my:SafePicture>
    							<Image Name="imgGameList" Margin="0,10,0,10" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="150">
    								<Image.Source>
    									<BitmapImage UriSource="{Binding BoxArtFrontThumb}"
    						 CreateOptions="BackgroundCreation" DecodePixelHeight="200" DecodePixelWidth="150" />
    								</Image.Source>
    							</Image>
    						</my:SafePicture>
    					</Grid>
    				</DataTemplate>
    			</phone:LongListSelector.ItemTemplate>
    		</phone:LongListSelector>
    	</Grid>
    </phone:PivotItem> 


    public class SafePicture : System.Windows.Controls.ContentControl
    {
        public SafePicture()
        {
            this.Unloaded += this.SafePictureUnloaded;
        }
    
        private void SafePictureUnloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var image = this.Content as System.Windows.Controls.Image;
    
            if (image != null)
            {
                image.Source = null;
            }
        }
    }

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年7月8日 8:23
    版主