System.OutOfMemoryException when images are displayed in a pivot control - windows-phone-8

I have an application with a pivot control.i am getting out of memory exception when binding image url source dynamically.Please help me out.Thanx in advance
Sample code:
XAML
<phone:Pivot x:Name="PivotProductImages" Grid.Row="1" Margin="0,0,0,77" ItemsSource="{Binding ProductItems}" SelectionChanged="PivotProductImages_SelectionChanged" >
<phone:Pivot.ItemTemplate>
<DataTemplate >
<Image Source="{Binding ProductUrl}"></Image>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
c#
private ObservableCollection<string> objProductimg = new ObservableCollection<string>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
objProductimg.Add("http://media.testing.com/v2/images_content_split/12594/products_1853164_image1_original.jpg.ashx?quality=90');
objProductimg.Add("http://media.testing.com/v2/images_content_split/12594/products_1853164_image2_original.jpg.ashx?quality=90');
PivotProductImages.ItemsSource = ProductItems}

If the images you download are too large, Windows Phone platform gives you the oportunity to decode them at a specific size. This way, even if your image is 1800 x 1000 for example, you will keep in memory the same image, but at a lower resolution.
<phone:Pivot x:Name="PivotProductImages" Width="100" Grid.Row="1" Margin="0,0,0,77" ItemsSource="{Binding ProductItems}" SelectionChanged="PivotProductImages_SelectionChanged" >
<phone:Pivot.ItemTemplate>
<DataTemplate >
<Image>
<BitmapImage DecodePixelWidth="100" UriSource="{Binding ProductUrl}" \>
</Image>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
If you set only one of the DecodePixelWidth or DecodePixelHeight, the image will be decoded at the desired size and same aspect ratio. By setting both of them to a value, you will decode the all images at the same ratio. This will be problematic if you have many pictures with different sizes.
I hope this will help you.

Related

Populate ScrollView at runtime in WIndows Phone 8

I have an app for windows phone 8 that displays data loaded from my website.
At the moment, I have setup 4 'holders' for the data, that contain a few TextBlocks and Images. When the app is loaded, these 4 holders display the data for the first 4 'records'. To display the next 4 'records', the user has to click a button, 'Next'.
I want to change this so that all 'records' are displayed in a ScrollView so the user simply has to scroll down to view records rather than click the 'Next' button.
I have also written the app for Android using Eclipse and Java. To do the above, I created a layout of the 'holder' in xml and then this is used as a template for the data. I only have to define the layout once and it is repeated at runtime, populated with the data from each record.
How do I achieve the same in Windows Phone, using vb.net and xaml?
I have googled and possibly DataTemplate is what I need however I'm not sure and have no idea how to implement it.
If you could point me in the right direction I'm sure I can figure it out!
Thanks in advance.
EDIT:
Ok, I've tried the following but the ListBox is empty:
Basically I have a List populated at runtime from my website (I know this bit works):
Public WebData As New System.Collections.Generic.List(Of WebInfo)
WebInfo Class:
Public Class WebInfo
Public ID As Integer
Public H1 As String
Public A1 As String
Public C1 As String
Public C2 As String
Public K1 As Date
End Class
xaml:
<ListBox x:Name="MainList" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="3" Grid.RowSpan="6" Grid.Column="0" Grid.ColumnSpan="3">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="H1" Text="{Binding H1}" FontSize="15" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Right" TextAlignment="Right" FontWeight="Bold" Foreground="Black"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I try to set the ItemsSource using:
MainList.ItemsSource = WebData
The ListBox does not populate.
Any Thoughts?
I think LongListSelector works for you, but you should edit DataTemplate for your needs.
<phone:LongListSelector ItemsSource="{Binding ArticleList}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Title:" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
The problem was, I was using variables in my class rather than properties:
Public Class WebInfo
Public Property ID As Integer
Public Property H1 As String
Public Property A1 As String
Public Property C1 As String
Public Property C2 As String
Public Property K1 As Date
End Class
Thanks for your help.

Fullscreen an image placed in a button in windows phone

Now i'm creating a quiz which has answers are images. I put each of them in an button to trigger my own event. The problem i having now is when I hold the button,the image in it will be fullscreen and when I don't hold it,the image return back. I have tried stretch property of image but it doesn't take effect.Please help me to find out the solution. Thanks. Here is my code:
<Button Name="Answer0Button" Hold="Answer0Button_OnHold" Style="{StaticResource ButtonStyle}" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderThickness="3" BorderBrush="{Binding Markers, Converter={StaticResource markersConverter}, ConverterParameter=0}">
<Image Source="{Binding Answers[0],Converter={StaticResource quizImagePathConverter}}"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<command:EventToCommand Command="{Binding AssertCommand}" CommandParameter="{Binding Answers[0]}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
When I debug, this button doesn't get the event on hold.
private void Answer0Button_OnHold(object sender, GestureEventArgs e)
{
//Make the image fullscreen here
}

How to refresh LongListSelector after delete an item

I have a LongListSelector like that
<phone:LongListSelector Name="ListRecentFiles"
LayoutMode="Grid"
ItemsSource="{Binding}"
GridCellSize="140,140"
SelectionChanged="ListRecentFiles_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid Background="Red" Margin="0,0,5,5">
<TextBlock Text="{Binding NoteTitle}" Style="{Binding PhoneTextNormalStyle}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="ContextMenu">
<toolkit:MenuItem x:Name="Delete" Header="Delete" Click="DeleteNote_Click" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
this is DataContext: public static ObservableCollection<Note> NoteItems;
And i try to delete an item from LongListSelector
private void DeleteNote_Click(object sender, RoutedEventArgs e)
{
Note selectedNote = (sender as MenuItem).DataContext as Note;
ListRecentFiles.ItemsSource.Remove(item);
NoteItems.Remove(selectedNote);
}
It's not work except i navigate to a other XAML page and return
I have visited this page but can't fix link
Without seeing more of the code, it's hard to be sure what's going wrong. But if you are setting
ListRecentFiles.DataContext = NoteItems;
that is incorrect. You want to set
ListRecentFiles.ItemsSource = NoteItems;
The XAML declaration:
ItemSource="{Binding}"
Could do that (depending on the rest of the code). Once .ItemsSource is set correctly, then the line:
NoteItems.Remove(selectedNote);
Should succeed in removing the visual item from the LongListSelector. In any case, you should not do the line:
ListRecentFiles.ItemsSource.Remove(item);
That would do the wrong thing when the list gets so big that it doesn't all fit in memory at once.

Change background as well as foreground in webBrowser Control ( windows phone 8)?

I'm a newbie, and I want to make a book app. I read file html in the Webbrowser (Windows Phone 8), but I want to change the background color to black and font color to white, but it is not working. I've tried following css,xaml.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,10,14,-10" Background="Black">
<phone:WebBrowser x:Name="web" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="446" Loaded="web_Loaded" Height="215" Background="Black" />
</Grid>
It isn't possible as webbrowser engine renders a background color for HTML page. What you can use is a trick. As it is posted here, you can set default Opacity to 0, and when the load is completed, change the opacity to 1:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,10,14,-10" Background="Black">
<phone:WebBrowser x:Name="web" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"
Loaded="web_Loaded" Width="446" Height="215" Opacity="0" LoadCompleted="web_LoadCompleted"/>
</Grid>
private void web_LoadCompleted(object sender, NavigationEventArgs e)
{
web.Opacity = 1;
}
Or as it is posted here you can cover webbrowser with another element which you collapse when content rendering is finished.

ContextMenu's MenuItem DataContext returns old items

I am using a ContextMenu within a LongListSelector so that I can delete some items in the list bounded to the LLS.
I am following a recent guide here in order to implement the LLS (though I am not using the JumpList). The only thing I've changed is to have the base group class extend ObservableCollection instead of List.
The issue I am having is that once I've implemented the ContextMenu and delete from there, I can delete from the same "location" in teh visible list twice and then it would crash.
Debugging shows that after the second delete, the Datacontext of the MenuItem returns the previous item that was deleted. So when I search for it in the list, the index I get is -1. I can catch this but I don't know how to then find out what was really selected as the item.
My XAML section for the contextMenu is as below:
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid toolkit:TiltEffect.IsTiltEnabled="True">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="conmen" Loaded="ContextMenu_Loaded">
<toolkit:MenuItem Header="Delete" Click="DeleteItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
<TextBlock Text="{Binding Usr, StringFormat='x{0}'}" FontSize="32" HorizontalAlignment="Left" Width="48"/>
</Border>
<Border Grid.Column="1" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
<TextBlock Text="{Binding Name}" FontSize="32" HorizontalAlignment="Left" />
</Border>
<Border Grid.Column="2" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
<TextBlock Text="{Binding Type, StringFormat=\{0:C\}}" FontSize="32" HorizontalAlignment="Right" />
</Border>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
And this is the start of my delete_click function to remove the item:
private void DeleteItem_Click(object sender, RoutedEventArgs e)
{
var menItem = (MenuItem)sender;
editCartItem = (Model.Cartitem)menItem.DataContext;
cartIndex = editCartItem.Id;
deleteIndex = this.cartList.FindIndex(FindItem);
After two deletes, the (Model.Cartitem)menItem.DataContext returns the previously deleted item.
I have been searching for a while and have found similar cases for different frameworks and scenarios from some years before. I wanted to know if there was an updated method for doing this in WP8.
I have seen suggestions in manually re-assigning the datacontext of the ContextMenu with a Loaded or Opened event, but the DataContext still relies on a specific item in the LLS. So I can't just point it's context to the LLS's.
I've also seen that it's been pointed to as a bug with a patch here which seems exactly like my issue, but I had no idea on how to apply the patch or if it even pertained to my situation with WP8.
I've also been ensuring that the LLS's selected item is cleared and have tried re-assigning it's itemSource after each operation to no avail.
Any help or advice in the right direction would be great. I've seen some posts on here about this, but I believe I've already passed those points (such as simply getting the menuItem and using an ObservableCollection...).
xaml:
<toolkit:ContextMenu Opened="ContextMenu_Opened">... </toolkit:ContextMenu>
c#
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
var menu = (ContextMenu)sender;
var owner = (FrameworkElement)menu.Owner;
if (owner.DataContext != menu.DataContext)
menu.DataContext = owner.DataContext;
}
you can see: Windows Phone Toolkit Context Menu Items have wrong object bound to them when an item is removed and then added
I just came across a simular issue:
When adding items to a listbox the datacontext of the menuitem belonging to the newly added items is not set correctly.
The workaround I ended up implementing was to rebuild the listbox after adding an item:
MyListBox.ItemsSource = null;
MyListBox.Items.Clear( );
MyListBox.ItemsSource = theList;
Not sure, if would also work for your issue...
Also the performance impact for listboxes with many items should be considered.