In Xamarin Forms i have the following listview:
<ListView x:Name="StudentView" RowHeight="55" SeparatorVisibility="None" CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding Image}" Grid.Row="0" Grid.Column="0" Aspect="AspectFill"></Image>
<Image Source="{Binding Image}" Grid.Row="0" Grid.Column="1" Aspect="AspectFill"></Image>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In the code behind i have declared an array of 1000 items.
The problem is that if i scroll up and down the listview, everytime i see in the xamarin profile that the memory usage increase and seems that CachingStrategy="RecycleElement" not work properly (tested on real android device).
After scroll some times, the program crash with out of memory.
What's the problem? How can i resolve it?
Not sure how you are setting the ItemSource of the list view, or what it is. It would be useful to also show some of the code-behind file for this.
As a blind guess there can be performance issues with the recycling if your source is not indexable. The recommendation from Xamarin is to use an IList for the itemsource, using something like an IEnumerable will cause performance issues for a large array because it essentially has to keep looping through the data to find the right item for display.
1000 Image items are a lot! I bet this is Android, I suggest to use different solutions like:
1.- Pagging strategies (Load 10, the load another 10 and so).
2.- Reduce the images sizes.
3.- Use a cacheable image like:
https://github.com/luberda-molinet/FFImageLoading
4.- Implement a View custom renderer with a Recycler view.
And then the topic gets bigger and bigger on how you can improve your ListView performance, here are some useful links:
-https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/performance/
-https://blog.xamarin.com/creating-highly-performant-smooth-scrolling-android-listviews/
-https://blog.xamarin.com/tips-for-creating-a-smooth-and-fluid-android-ui/
-http://kent-boogaart.com/blog/jason-smith's-xamarin-forms-performance-tips
I had the same issue , large list of images or images with large resolution causes this problem, I solved this by using FFImageLoading found here, also available on nuget , Also try to bind thumbnails instead of the actual Image if possible.
<ContentPage xmlns:ci="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms">
<ci:CachedImage Source="{Binding ActualImageThumbnail}" Aspect="Fill"/>
Related
I notice that Android is a lot slower than iOS when it comes to drawing out forms.
Can anyone tell me is there any difference in the speed taken to draw these three:
<Grid ColumnSpacing="{DynamicResource TemplateColumnSpacing}"
HeightRequest="{DynamicResource DefaultGridHeight}"
Padding="{DynamicResource GridGridPadding}">
</Grid>
<Grid ColumnSpacing="10"
HeightRequest="50"
Padding="5">
</Grid>
I have a problem making my ImageButton's width fixed and the Height will be changed with the size of the image that I use in the ImageButton, I use the ImageButton multiple times in a Scrollview, I use Visual Studio to create a Cross-Platform App using Xamarin, So what I used to solve my problem are here and here, But I wasn't able to solve it using anything, And this is a picture that describes what I want:-
And please don't make it duplicate if there's answers for my question because I am new to Xamarin Forms And I didn't find any answer exactly to my question, And this is my code:-
<Frame CornerRadius="10" OutlineColor="#fce9cc" Padding="4" Margin="15" VerticalOptions="StartAndExpand"
HasShadow="True" x:Name="ViewLayout" x:FieldModifier="public" BackgroundColor="Black">
<StackLayout>
<Label x:Name="filmNameLabel_1" HorizontalOptions="Center" FontSize="Large" TextColor="White"
Margin="0,15,0,15"/>
<StackLayout Orientation="Vertical">
<ImageButton x:Name="imageWidget_1" BackgroundColor="Black" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>
</StackLayout>
</StackLayout>
</Frame>
First time I set the StackLayout or the parent of the ImageButton's HeightRequest to a number and it would make the ImageButton's Height a fixed number but I dont want that I want the ImageButton's Height to change with the Image's size Ratio bye the width which is fixed next to
the parent layout.
i have used wrappanel to arrange images within a listbox but in my picture collection some images have different sizes. Some are horizontal and some are vertical. Due to different orientation, there is some spaces between the images which is not so good in vision. Can any body suggest me any way to remove these spaces?
thanks
i am using following code but still the problem is same...
<ListBox Name="lstBoxMyRecentPhotos"
MaxHeight="650"
Margin="0,8,0,0"
SelectionChanged="lstImageList_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit1:VariableSizedWrapGrid ItemHeight="200" ItemWidth="230" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Name="brdr"
Margin="5"
BorderBrush="White"
BorderThickness="3">
<Border.Background>
<ImageBrush ImageSource="Images/capsule_image_holder.png" Stretch="Uniform" />
</Border.Background>
<Image Width="{Binding width}"
Height="{Binding height}"
Hold="imgRecent_Hold"
Source="{Binding photoUrl}"
Stretch="UniformToFill">
</Image>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Two ways
Make the image always stretch UniformToFill and use hardcoded size - that way it will uniformly fill the space that it's been given and all the photos will use the given space nicely
Use VariableSizedWrapGrid.
Provides a grid-style layout panel where each tile/cell can be
variable size based on content. Similar to the WinRT
VariableSizedWrapGrid.
I don't know which toolkit you are using for the VariableSizedWrapGrid, but are you sure that you need to set its ItemHeight? What happens if you don't?
I'm personally using Telerik's controls to give me a WrapGrid. If you have access to their controls, I can share an example.
Here's what my implementation looks like:
I am using a Grid view and have defined two elements, WebBrowser and an Image, to be contained inside that Grid.
<Grid x:Name="ContentPanel" Background="Black">
<phone:WebBrowser Name="WebView"
IsScriptEnabled="True"
Grid.Row="0"
IsGeolocationEnabled="True"
ScriptNotify="webView_ScriptNotify"
NavigationFailed="webView_NavigationFailed"
Navigated="webView_Navigated"
Navigating="webView_Navigating"
VerticalAlignment="Top"
LoadCompleted="WebView_LoadCompleted"
Canvas.Top="0"
Canvas.Left="0"
Height="{Binding ActualHeight, ElementName=LayoutRoot}"
Width="{Binding ActualWidth, ElementName=LayoutRoot}">
</phone:WebBrowser>
<Image Name="StartLogo" Source="/Assets/StartScreen3.png" Stretch="None" VerticalAlignment="Bottom" HorizontalAlignment="Center"></Image>
</Grid>
I want the image to come over WebBrowser view at bottom. But with WebBrowser visible Image is not showing up. Can someone help me to fix it. I don't want to resize the WebBrowser to some smaller height.
And also how does order of defining elements inside a Grid affects the view?
EDIT:
I used Canvas to contain both of these. Works fine now.
the image should be show as apparently nothing is wrong with your code. It might be that image color and opened web page color is same that is why image might not be visible. or some issue with image path or build action of the image.
the last UI element will be on top all other UI elements in the gird. as per your xaml image control will be top of the webbrowser control but at the bottom of the grid.
There is no mistake in your code but still if you are facing this Problem
Try this:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<phone:WebBrowser Name="WebView"
IsScriptEnabled="True"
Grid.Row="0"
Grid.RowSpan="2"
IsGeolocationEnabled="True"
ScriptNotify="webView_ScriptNotify"
NavigationFailed="webView_NavigationFailed"
Navigated="webView_Navigated"
Navigating="webView_Navigating"
VerticalAlignment="Top"
LoadCompleted="WebView_LoadCompleted"
Canvas.Top="0"
Canvas.Left="0"
Height="{Binding ActualHeight, ElementName=LayoutRoot}"
Width="{Binding ActualWidth, ElementName=LayoutRoot}">
</phone:WebBrowser>
<Image Name="StartLogo" Source="/Assets/ApplicationIcon.png" Stretch="None" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="1"></Image>
</Grid>
The Order of Defining UI Elements Overlaps the Previos Defined Control
If you right click the object in design mode you should be able to order the objects on the application page?
I have two TextBlocks, each with a different font size, and am trying to align them vertically so that their baselines match up.
I know how to do this by creating multiple Run elements within a single TextBlock, but in this case my TextBlocks are not directly adjacent, so that won't work. Here's a simplified example of my layout:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0"
FontSize="20"
VerticalAlignment="Bottom">Small</TextBlock>
<TextBlock Grid.Column="1"
FontSize="50"
VerticalAlignment="Bottom">Large</TextBlock>
</Grid>
If I could find a way to calculate the baseline of the font in code, that would be good enough - I could do the alignment in code, using a Canvas or custom Panel - but I haven't been able to find any WP7 APIs for finding font metrics.
I have found a very similar question here, but the solution involves the FormattedText class, and unfortunately this is not available on WP7.
I would really like to be able to do this without hardcoding any margins/offsets, if possible.
Whilst I agree with Matt's answer, if you actually need to solve the problem without manually setting margins, etc, then you can align them "properly" by setting the LineHeight to the tallest line, and then setting LineStackingStrategy to BlockLineHeight.
In that you are hardcoding the fontsizes in XAML, what's so bad about hardcoding margins to create the effect you are looking for?
This will also mean that you only need to do the calcuation once, rather than forcing the app to do it every time the page is laid out.