Question

I have a xaml page. xaml page wants to show two TextBlocks and one LonglistSelector.

the two TextBlocks datasource come from an object(SpecifiedArticle);the LonglistSelctor itemsSource comes from Collection(ImageUriCollection).

when the page launch, the images cannot be displayed.

  1. two TextBlocks data show well

  2. LonglistSelctor does not show images; but i am sure ImageUriCollection's data can be get from the xaml because i tested in a image control and it works

            <Image  Source="{Binding ImageUriCollection[0].ImageSource}" Width="108" Height="108" Stretch="UniformToFill">
    
    
            </Image>
    

i think the issue is in the LonglistSelctor itemsSource binding. anyone can help?

all code below(without the httpclient wrapper):

DetailsPage.cs is below:

public partial class DetailsPage : PhoneApplicationPage
    {
        DetailsPageViewModel viewModel = new DetailsPageViewModel();

        public DetailsPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(DetailsPage_Loaded);
        }

        private void DetailsPage_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = viewModel;
            //imageList.ItemsSource = viewModel.ImageUriCollection;
            //imageList.ScrollTo(imageList.ItemsSource[imageList.ItemsSource.Count - 1]);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string ArticleId = "";
            try
            {
                if (NavigationContext.QueryString.TryGetValue("ArticleId", out ArticleId))
                {
                    Debug.WriteLine(ArticleId);
                    viewModel.LoadPage(Int32.Parse(ArticleId));
                    //Debug.WriteLine(viewModel.SpecifiedArticle.Words.ToString());
                    //LayoutRoot.DataContext = new CollectionViewSource { Source = viewModel.SpecifiedArticle };
                }
                else
                {
                    MessageBox.Show("No ArticleId passed in.");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error in DetailsPage.OnNavigatedTo"); 
            }
        }
    }

ViewModel is below:

public class DetailsPageViewModel : INotifyPropertyChanged
    {
        private bool _isLoading = false;

        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }
            set
            {
                _isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        public DetailsPageViewModel()
        {
            this.SpecifiedArticle = new Article();
            this.ImageUriCollection = new ObservableCollection<Photo>();
            this.IsLoading = false;
        }

        private Article specifiedArticle;
        public Article SpecifiedArticle
        {
            get
            {
                return specifiedArticle;
            }
            set
            {
                specifiedArticle = value;
                NotifyPropertyChanged("SpecifiedArticle");
            }
        }

        public ObservableCollection<Photo> ImageUriCollection
        {
            get;
            private set;
        }

        public void LoadPage(int articleId)
        {            
            IsLoading = true;

            ReadArticle(articleId);

        }

        private async void ReadArticle(int articleId)
        {
            try
            {
                Article articleDetails = new Article();
                articleDetails = await CollectionHttpClient.GetAsyncByArticleId(articleId);
                SpecifiedArticle = articleDetails;
                //articleDetails.FirstImage = new Uri(articleDetails.ImagePathList[0]);

                if (articleDetails.ImagePathList != null)
                {
                    foreach (var item in articleDetails.ImagePathList)
                    {
                        Photo p = new Photo();
                        p.ImageSource = new Uri(item);
                        this.ImageUriCollection.Add(p);
                    }
                    //var image = await CollectionHttpClient.GetImageByImageName(articleDetails.ImagePath);
                    //Bytelist.Add(image);
                }
                else
                {
                    this.ImageUriCollection = null;
                }

                IsLoading = false;


            }
            catch(Exception ex)
            {
                MessageBox.Show("sorry, no data.");
                IsLoading = false;
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

xaml is:

<phone:PhoneApplicationPage.Resources>

        <vm:DetailsPageViewModel x:Key="viewModel"/>
        <phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter"/>
        <phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/>

        <Style x:Key="JumpListStyle" TargetType="phone:LongListSelector">
            <Setter Property="LayoutMode" Value="List" />
            <Setter Property="Margin" Value="12,12,0,0"/>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border Background="{Binding Converter={StaticResource BackgroundConverter}}" 
                                Width="470" 
                                Height="70" 
                                Margin="6">
                            <TextBlock Text="{Binding Key}"
                                       Foreground="{Binding Converter={StaticResource ForegroundConverter}}"                                       
                                       FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                       FontSize="28"  
                                       Padding="2"
                                       VerticalAlignment="Bottom"/>
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="GroupHeader">
            <Border Background="Transparent">
                <Border Background="Transparent" BorderBrush="Transparent" BorderThickness="1"  
                        Width="400" Height="90"                  
                        HorizontalAlignment="Left">
                    <TextBlock Text="Pictures:" 
                               Foreground="{StaticResource PhoneAccentBrush}" 
                               FontSize="28"
                               Padding="2"                                
                               FontFamily="{StaticResource PhoneFontFamilySemiLight}"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"/>
                </Border>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="ItemTemplate">
            <StackPanel Height="108" Width="108" Margin="6,6">
                <Image Width="108" Height="108" Stretch="UniformToFill">
                    <Image.Source>
                        <BitmapImage UriSource="{Binding ImageSource}" CreateOptions="BackgroundCreation"/>
                    </Image.Source>
                </Image>
            </StackPanel>
        </DataTemplate>


    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="{Binding Path=SpecifiedArticle.Subject }" TextWrapping="Wrap" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        </StackPanel>

        <!--ContentPanel - place images here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Background="Transparent">

            <!--
            <Image  Source="{Binding ImageUriCollection[0].ImageSource}" Width="108" Height="108" Stretch="UniformToFill">

                    <Image.Source>
                        <BitmapImage UriSource="{Binding ImageSource}" CreateOptions="BackgroundCreation"/>
                    </Image.Source>

            </Image>
 -->

            <phone:LongListSelector Name="imageList" Margin="13,-30,0,0"
                                        ItemsSource="{Binding ImageUriCollection}"
                                        ItemTemplate="{StaticResource ItemTemplate}"                     

                                        JumpListStyle="{StaticResource JumpListStyle}" 
                                        IsGroupingEnabled="True"
                                        LayoutMode="Grid" 
                                        GridCellSize="108,108"/>

        </Grid>

        <!--ContentPanel - place article words here-->
        <StackPanel Grid.Row="2" Margin="12,17,0,28">

            <TextBlock Text="{Binding Path=SpecifiedArticle.Words}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>



    </Grid>

DetailsPage.cs is below:

public partial class DetailsPage : PhoneApplicationPage
    {
        DetailsPageViewModel viewModel = new DetailsPageViewModel();

        public DetailsPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(DetailsPage_Loaded);
        }

        private void DetailsPage_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = viewModel;
            //imageList.ItemsSource = viewModel.ImageUriCollection;
            //imageList.ScrollTo(imageList.ItemsSource[imageList.ItemsSource.Count - 1]);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string ArticleId = "";
            try
            {
                if (NavigationContext.QueryString.TryGetValue("ArticleId", out ArticleId))
                {
                    Debug.WriteLine(ArticleId);
                    viewModel.LoadPage(Int32.Parse(ArticleId));
                    //Debug.WriteLine(viewModel.SpecifiedArticle.Words.ToString());
                    //LayoutRoot.DataContext = new CollectionViewSource { Source = viewModel.SpecifiedArticle };
                }
                else
                {
                    MessageBox.Show("No ArticleId passed in.");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error in DetailsPage.OnNavigatedTo"); 
            }
        }
    }

ViewModel is below:

public class DetailsPageViewModel : INotifyPropertyChanged
    {
        private bool _isLoading = false;

        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }
            set
            {
                _isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        public DetailsPageViewModel()
        {
            this.SpecifiedArticle = new Article();
            this.ImageUriCollection = new ObservableCollection<Photo>();
            this.IsLoading = false;
        }

        private Article specifiedArticle;
        public Article SpecifiedArticle
        {
            get
            {
                return specifiedArticle;
            }
            set
            {
                specifiedArticle = value;
                NotifyPropertyChanged("SpecifiedArticle");
            }
        }

        public ObservableCollection<Photo> ImageUriCollection
        {
            get;
            private set;
        }

        public void LoadPage(int articleId)
        {            
            IsLoading = true;

            ReadArticle(articleId);

        }

        private async void ReadArticle(int articleId)
        {
            try
            {
                Article articleDetails = new Article();
                articleDetails = await CollectionHttpClient.GetAsyncByArticleId(articleId);
                SpecifiedArticle = articleDetails;
                //articleDetails.FirstImage = new Uri(articleDetails.ImagePathList[0]);

                if (articleDetails.ImagePathList != null)
                {
                    foreach (var item in articleDetails.ImagePathList)
                    {
                        Photo p = new Photo();
                        p.ImageSource = new Uri(item);
                        this.ImageUriCollection.Add(p);
                    }
                    //var image = await CollectionHttpClient.GetImageByImageName(articleDetails.ImagePath);
                    //Bytelist.Add(image);
                }
                else
                {
                    this.ImageUriCollection = null;
                }

                IsLoading = false;


            }
            catch(Exception ex)
            {
                MessageBox.Show("sorry, no data.");
                IsLoading = false;
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
Was it helpful?

Solution 2

it is the style problem.

delete the style, try again, images show well

<phone:LongListSelector Name="imageList" Margin="13,-30,0,0" 
                                    ItemsSource="{Binding ImageUriCollection}"
                                    ItemTemplate="{StaticResource ItemTemplate}"
                                    LayoutMode="Grid"
                                    GridCellSize="108,108">

            </phone:LongListSelector>

in the JumpListStyle, it contains the textblock which is not belong to the xaml, that is why the LonglistSelector does not display anything after binding collectly.

OTHER TIPS

Try this-

just change your ItemTemplate DataTemplate to a simpler one

If it works, add one change at a time.

<DataTemplate x:Key="ItemTemplate">
    <Image Source="{Binding ImageSource}" Stretch="UniformToFill"/>
</DataTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top