Xamarin.Forms.Pages.ListDataPage not loading Data from AzureEasyTableSource - c#

I am trying to use the new Xamarin.Forms.Pages.ListDataPage control that is currently in Preview.
I have followed the instructions on the getting started page Getting Started with DataPages to set up an AzureEasyTableSource.
The app compiles and runs, but this.DataSource.Data.Count always returns 0, on both iOS and Android.
Bellow is the Views XAML
<?xml version="1.0" encoding="UTF-8"?>
<p:ListDataPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Sparrow"
xmlns:azure="clr-namespace:Xamarin.Forms.Pages.Azure;assembly=Xamarin.Forms.Pages.Azure"
xmlns:p="clr-namespace:Xamarin.Forms.Pages;assembly=Xamarin.Forms.Pages"
x:Class="Sparrow.NewsPage" StyleClass="Event" Title="News" BindingContext="This">
<p:ListDataPage.DataSource>
<azure:AzureDataSource>
<azure:AzureDataSource.Source>
<azure:AzureEasyTableSource TableName="NewsItem"
Uri="https://<<AppName>>.azurewebsites.net"/>
</azure:AzureDataSource.Source>
</azure:AzureDataSource>
</p:ListDataPage.DataSource>
<p:ListDataPage.DefaultItemTemplate>
<DataTemplate>
<ViewCell>
<p:ListItemControl
Title="{ Binding Value[headline] }"
Detail="{ Binding Value[newsBody] }"
DataSource="{ Binding Value }"
HeightRequest="30"
/>
</ViewCell>
</DataTemplate>
</p:ListDataPage.DefaultItemTemplate>
</p:ListDataPage>
A Wireshark capture and the App Service streaming logs show that the server is receiving and responding to the requests, but the application fails to process the data.

It looks like perhaps your data bindings are a problem here as they should correspond with the values that are coming in and need special syntax. Here is an example of mine taken from my OCR sample: https://github.com/jamesmontemagno/app-ocr-functions:
<p:CardView
Margin="10"
Text="Total Amount from OCR:"
Detail="{p:DataSourceBinding Total}"
ImageSource="{p:DataSourceBinding Url}"
DataSource="{Binding Value}"
HeightRequest="250"/>
</ViewCell>
You must use the p:DataSourceBinding to the name of it.

Related

Issues with Xamarin Forms 4 (pre 4) using CollectionView in UWP project

I'm facing some tricky bugs while trying to use the new CollectionView
feature implemented in Xamarin Forms 4. On the Android project it works very
well after enabling experimental features in MainActivity.cs with:
global::Xamarin.Forms.Forms.SetFlags(new[] { "CollectionView_Experimental", "Shell_Experimental" });
But xamarin documentation doesn't provide any information about UWP project
so first when I tried to compile the UWP project, it throws me this exception
when it tries to render a page that uses CollectionView
System.InvalidOperationException:
'The class, property, or method you are attempting to use ('VerifyCollectionViewFlagEnabled')
is part of CollectionView; to use it, you must opt-in by calling Forms.SetFlags("CollectionView_Experimental")
before calling Forms.Init().'
So I tried to call the SetFlags in App.xaml.cs in UWP project before calling
InitializeComponent() method. So this time it throws me this exception when
it tries to load the page containing CollectionView:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
So like in this example:
await Navigation.PushAsync(new PageWithCollectionView());
The exception is thrown after successful execution of PageWithCollectionView constructor.
Can somebody help me to solve this problem?
UPDATE
Ok, so adding the SetFlags in App.xaml.cs in UWP project works and
the CollectionView gets initialized correctly. But the NRE is still there
(on Android the CollectionView works without problems), while trying to get
rid of this problem I noticed that it's caused when I try to nest the XAML layout this way:
<CollectionView SelectionMode="Single">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Horizontal"
Span="2"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<StackLayout Orientation="Vertical"
Grid.Column="0"
Grid.Row="0">
<Label Text="{Binding Title}"/>
</StackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
If I remove all the DataTemplate from CollectionView.ItemTemplate, just
leaving it blank like this:
<CollectionView SelectionMode="Single">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Horizontal"
Span="2"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
The page gets rendered a CollectionView shows ItemSource elements with messy
layout (without margin and padding, and calling ToString() method of element
to visualize it inside the cell).
[UPDATE]
After update to Xamarin Forms 4 pre 8, the exception is gone.
I created a code sample that contains CollectionView. According to the document, we need call SetFlag before Xamarin.Forms.Forms.Init(e) in the App.xaml.cs file like the following.
........
Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
Xamarin.Forms.Forms.Init(e);
........
Implement CollectionView
<CollectionView>
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</CollectionView.ItemsSource>
</CollectionView>
And it works well, For NullReferenceException issue, you need check if there is null object in the code behind. Simple implementation of CollectionView does not cause such error.
Update
Please try to update Xamarin Forms 4 to latest pre version.
global::Xamarin.Forms.Forms.SetFlags(“CollectionView_Experimental”);
use MainActivity.cs in Android

ActivityIndicator not showing in XAML or CodeBehind

I have a weird situation on my Xamarin.Forms app. I´m trying to show an ActivityIndicator, but it is not showing in any condition. I´ve set all conditions on XAML and then I tried on code behind. But I could not get it to work. This is the code:
<StackLayout HorizontalOptions="Center"
VerticalOptions="Center">
<ActivityIndicator x:Name="actIndicator"
Color="Blue"
IsVisible="True"
IsRunning="True"/>
<Label x:Name="lblCargando"
Text="Cargando..." />
<Label x:Name="lblStatus"/>
<Button x:Name="btnCerrar"
Text="Cerrar"
IsVisible="False"
Clicked="Cerrar_Clicked"/>
</StackLayout>
Is there something missing?.
Thanks.
Finally, it seems that there was an issue with the emulator that was executing an old version of code.
I deleted the emulator and re create it and get rid of the problem.

CarouselView-Xamarin Forms: Data is not getting displayed

I am trying to display data using carousel view in my forms application. I am not sure what am I missing but data is not getting populated, I have followed almost all available samples but still I have not found any success. Please help me get out of this problem:
I am using the following code in my xaml file:
<forms:CarouselView x:Name="listview" ItemsSource="postIdList" HeightRequest="200" Margin="5">
<forms:CarouselView.ItemTemplate>
<DataTemplate>
<ContentPage >
<StackLayout >
<Label Text="{Binding .}" FontSize="20" TextColor="Black" HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
</DataTemplate>
</forms:CarouselView.ItemTemplate>
</forms:CarouselView>
And I am trying to populate the data in the following manner in .cs file:
List<string> postIdList = new List<string>
{
"Carousel", "Carousel", "carousel"
};
listview.ItemsSource = postIdList;
If I give a background colour for carousal view I am able to see that colour, but text is not getting displayed.
Please let me know my mistake.
In case of a ListView, you need an ObservableCollection instead of a List. Switch it around and see if it works?
On your XAML you are defining a <ContentPage> inside the <Datatemplate>. Replace the <ContentPage> with a <ViewCell> or a <Grid>
Here is a great example on how to use it, you need to keep in mind that CarouselView work like a ListView, it need to have a Datatemplate.

WebView video in xaml Does not play correctly

When I open the Binding url in a web view the video plays just the audio not the visuals on the emulator and my phone.
When I open the Binding url on my pc and phone it works fine.
Any idea how to fix it?
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WiscOnline.Mobile.ViewLearningItem"
Title="{Binding LearningItem.Name}">
<WebView x:Name="webView"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Source="{Binding CurrentSource}" />
<StackLayout>
<Label Text="{Binding LearningItem.ViewUrl}" FontSize="19"></Label>
</StackLayout>
<WebView Source="{Binding LearningItem.ViewUrl}" ></WebView>
</ContentPage>
This issue is with Android and it's WebView and not specifically with Xamarin. Different versions of Android will support different WebView features. Video playback is particularly bad in a WebView.
A better option would be to use the native video player for each platform or to at least use the native video player on Android.
You could either use a handy plugin/component for this like this one (I have not ever tried that one but saw it was recommended by others) or you could try and roll one yourself by using the native Android VideoView.
A Xamarin Forms thread about this very issue and using the VideoView can be found here.

EmbeddedImage in Xamarin Forms: how to bind it in XAML

I am trying to follow the Xamarin documentation about Embedded Images:
https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images
This is the code snippet that I use in my portable application:
var embeddedImage = new Image { Aspect = Aspect.AspectFit };
embeddedImage.Source = ImageSource.FromResource("<MyNamespace>.<MyFolder>.imageFileName.jpg");
listItem.EmbeddedImage = embeddedImage;
Now I am trying to bind it in XAML, as part of a ListView (note: the <Image Source="{Binding EmbeddedImage}" /> part is probably wrong) :
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding EmbeddedImage}" /> // This is most likely wrong since it doesn't work
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I've tested this with <Image Source="{Binding ImageUrl}" /> and it works, so the rest of the code seems to be working as it should.
Edit:
Someone suggested this post as a solution:
How to load an image to a ImageCell on Xamarin.Forms?
But the accepted answer there was:
The images go into your "native" projects
...but actually the Xamarin documentation says that it can be done inside the portable project (so you wouldn't need to copy the same images/icons through all the "native" projects).
I know that putting them in every subproject would probably work, but that's not the point of my question.
You can access the resource directly in xaml by using pack://application uri.
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Image Source="pack://application:,,,/{AssemblyName};component/Images/MyImage.png" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I've combed my hair out over this, and the problem is that Microsoft doesn't tell you the whole story in the Images docs. You have to go over to the Files docs to understand why it won't work.
Yes, you can use embedded resources in the shared project directory, but apparently under only one of two conditions:
You have to use a Markup Extension and then directly specify the resource path in Xaml, a la: <Image Source="{local:ImageResource ProjectName.Images.flower.jpg"/>
You write compiler directives in the code-behind that customize the beginning of the ResourceID per-platform.
Option 1 requires hard-coding the path, so you can't use a dynamically-assigned image, and Option 2 wrecks the whole point of trying to write cross-platform code.
WAY HUGE EDIT: May be solved
This post on the Xamarin forums made a huge difference for me: https://forums.xamarin.com/discussion/17953/helpful-shared-project-image-information
note: the first post basically gives you all the code you need, but a little bit down in the thread there's some advice about project configurations that may be necessary for it to work correctly.
It basically introduces some custom code that programmatically derives the resource ID during runtime, so it's cross-platform code that customizes itself to whatever platform is currently running it. Working beautifully for me! Here's the version of the suggested approach that I'm using:
using System.Linq;
using System.Reflection;
using System.Diagnostics;
using System;
namespace Helpers
{
public class EmbeddedSourceror
{
public static Xamarin.Forms.ImageSource SourceFor(string pclFilePathInResourceFormat)
{
var resources = typeof(EmbeddedSourceror).GetTypeInfo().Assembly.GetManifestResourceNames();
var resourceName = resources.Single(r => r.EndsWith(pclFilePathInResourceFormat, StringComparison.OrdinalIgnoreCase));
Debug.WriteLine("EmbeddedSourceror: resourceName string is " + resourceName);
return Xamarin.Forms.ImageSource.FromResource(resourceName);
}
}
}
This lets you write the Xaml code in a totally normal way, such as:
<Image
x:Name ="backingImage"
Aspect ="AspectFill" />
And in the code-behind all you have to do is:
backingImage.Source = EmbeddedSourceror.SourceFor("flower.jpg");

Categories