Why data binding? - xamarin.forms

Please check ListView Data Sources. You can find following code in XAML file:
<ListView x:Name="EmployeeView"
ItemsSource="{Binding Employees}">
But if you remove ItemsSource="{Binding Employees}", it is still working because there is EmployeeView.ItemsSource = employees in c# file. What's the point to do ItemsSource="{Binding Employees}?
Please advise. TIA.

The first two examples achieve the same thing with different approaches - look at the text between the code sections ("The equivalent C# code is:")

Related

Data binding to SfChart properties should work with elements in StaticResource?

We have a common DateTimeAxis that we refer to in all our content pages with an SfChart.
<xForms:DateTimeAxis
x:Key="CommonDateTimeAxis"
AxisLineStyle="{StaticResource ChartAxisLineStyle}"
EdgeLabelsDrawingMode="Shift"
Interval="{Binding DateTimeAxisIntervalGrouping}"
IntervalType="{Binding DateTimeAxisInterval, Converter={StaticResource DataTimeAxisTypeConverter}}"
MajorTickStyle="{StaticResource ChartMajorTickStyle}"
Maximum="{Binding CurrentDataWindow.End}"
Minimum="{Binding CurrentDataWindow.Start}"
PlotOffset="12"
ShowMajorGridLines="False">
<xForms:DateTimeAxis.LabelStyle>
<xForms:ChartAxisLabelStyle
FontFamily="{StaticResource LightFontFamily}"
FontSize="10"
LabelFormat="{Binding DateTimeAxisDateFormat}"
TextColor="{StaticResource OffWhite}" />
</xForms:DateTimeAxis.LabelStyle>
</xForms:DateTimeAxis>
Note that we are binding to a ViewModel property as we change the format dynamically.
LabelFormat="{Binding DateTimeAxisDateFormat}"
On initial load of a page and viewmodel the setting of the format works fine. However if once we close the View\ViewModel then go back into any other of our View\ViewModels with sfChart (or the same one) the LabelFormat remains stuck on whatever value it was prior to closing the first View/ViewModel.
I thought I was doing something foolish with retaining instances but I then realised that if i simply declare the DateTimeAxis locally i.e. not a shared resource in a ResourceDictionary, the binding always works.
That's not expected right? FWIW I have opened an incident with Syncfusion seperately and will report anything useful back here
The reported issue has been fixed and moved to Syncfusion SfChart NuGet Version: 18.2.0.47

Generate Bar Code in Xamarin Forms

I need to generate a Bar Code in my Xamarin.Forms project. I have found samples to scan Bar Code. Anyone has tried to generate the Bar Code ?
With the ZXing library, you can not only scan barcodes but also generate them.
The sample repo can be found here: https://github.com/jfversluis/Blurry-ZXingBarcodeImageView and was created to answer another question here on StackOverflow about barcodes being blurry. But it also shows how to generate a barcode.
Basically, you just install the ZXing library onto your projects and add a ZXingBarcodeImageView to your page.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:BlurryZXing" x:Class="BlurryZXing.MainPage" xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" xmlns:zx="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable">
<forms:ZXingBarcodeImageView
IsVisible="True"
x:Name="QRCodeView"
BarcodeFormat="QR_CODE"
HeightRequest="300"
WidthRequest="300"
BarcodeValue="-1">
<zx:ZXingBarcodeImageView.BarcodeOptions>
<zxcm:EncodingOptions Width="300" Height="300" />
</zx:ZXingBarcodeImageView.BarcodeOptions>
</forms:ZXingBarcodeImageView>
</ContentPage>
You can specify the format and value. Supported formats at time of writing are: UPC-A, UPC-E, EAN-8, Code 39, EAN-13, ITF, RSS-14, RSS-Expanded, QR Code, Code 93, Data Matrix, Code 128, Aztec (beta), Codabar, PDF 417 (beta), MaxiCode.
Don't forget to initialize the ZXing library in your platform projects, else it will show up empty. You can do this by adding:
// On iOS in your AppDelegate.cs in the FinishedLaunching method
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
and
// On Android in the MainActivity.cs in the OnCreate method
ZXing.Net.Mobile.Forms.Android.Platform.Init();

How do you define a single blendable design time instance?

The default Windows 8 project template has a CollectionViewSource in the template.
<CollectionViewSource
x:Name="itemsViewSource"
Source="{Binding Model.Invitations}"
d:Source="{Binding Invitations, Source={d:DesignInstance Type=vm:DesignerFilteredInvitations, IsDesignTimeCreatable=True}}" />
Obviously not all pages have a collection as their model, you can define a DataContext like this:
<vm:MySingleItemViewModel x:Key="Model" />
How do you define the design instance for this kind of model?
Well, design time data is best accomplished like this: http://blog.jerrynixon.com/2012/08/most-people-are-doing-mvvm-all-wrong.html
I realize your question is asking about using d:DesignInstance which also works with this type of technique - just not demonstrated in that article.
All it requires is a good constructor.
Okay, using this works fine:
<Page
d:DataContext="{d:DesignInstance Type=Models:ViewModel, IsDesignTimeCreatable=True}"
And using this works fine:
<d:Page.DataContext>
<Models:ViewModel/>
</d:Page.DataContext>
I must tell you the latter is an easier approach, too. It is also what Visual Studio will generate when you setup a data source in the designer. It also gives you fully-typed bindings. But either is acceptable.
Another note. I can see no good reason to set an object directly to the source of a CollectionViewSource. Normally you would be binding the CVS's Source property to a property inside your ViewModel. But, given your question: Here's how:
<CollectionViewSource
x:Name="TestCVS" Source="{Binding}"
d:DataContext="{Binding Source={d:DesignInstance Type=Models:ViewModel, IsDesignTimeCreatable=True}}"/>
Binding to the Source in the designer caused me endless trouble. But it irritated me more because I knew I would never do it this way. This is what I wanted to do:
<d:Page.DataContext>
<Models:ViewModel/>
</d:Page.DataContext>
<Page.Resources>
    <CollectionViewSource x:Name="TestCVS" Source="{Binding}" />
</Page.Resources>
You better have a great reason for your approach!
Best of luck!

Linq To Xml datasource for grid view is not working. The grid show no rows

I'm getting some Xml back from a service. I would like it to be the datasource of a grid view on my aspx page. Here is a sample of the Xml
<?xml version="1.0" encoding="utf-16" ?>
<ArrayOfTripTollCompleteDC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TripTollCompleteDC>
<TripTollId>5</TripTollId>
<DMSLaneModeID xsi:nil="true" />
<HOVOnly>false</HOVOnly>
<CreateDateTime>2010-06-07T15:54:01.023</CreateDateTime>
<ConfigVTMSDelaySeconds>5</ConfigVTMSDelaySeconds>
</TripTollCompleteDC>
and here is my code that parses the xml and tries to bind the grid. What am I missing here?
var retVal = service.GetTripDetailsByTripID(tripId);
var xmlTrips = XDocument.Parse(retVal);
var tripTolls =
from t in xmlTrips.Elements("TripTollCompleteDC")
select new {
TripTollId = (int)t.Element("TripTollId")
, DMSLaneModeID = (int?)t.Element("DMSLaneModeID")
, HOVOnly = (bool)t.Element("HOVOnly")
, CreateDateTime = (DateTime)t.Element("CreateDateTime")
, ConfigVTMSDelaySeconds = (int)t.Element("ConfigVTMSDelaySeconds")
};
grdTripDetails.DataSource = tripTolls;
grdTripDetails.DataBind();
I realize these are anonymous types. Is that a problem? I have verified the service is returning the Xml as stated above. Can anyone out there point me in the right direction? Thanks so much for any tips.
Just for completeness, here is the grid markup
<asp:GridView runat="server" ID="grdTripDetails" />
Cheers,
~ck in San Diego
Try this:
from t in xmlTrips.Root.Elements("TripTollCompleteDC")
Note the addition of Root in there. There's only one top-level element, and it's not a TripTollCompleteDC.
You may also need to autogenerate the columns - I don't know about grid views. That raises a useful point about debugging this sort of thing. There are two potential problems here:
Parsing and transforming the XML
Generating the grid view
You can test the first point via logging - log the transformed values in whatever way you normally do logging. Heck, you could even just use a separate console app for testing. (That's what I've just done.)
You can test the second point by hard-coding some data and reloading the page - does it show up how you expect it to? If not, tweak the code until it does what you want.
Separating these two concerns makes it much easier to work out the problem.

How to get an XML value from FLEX

i have the following Problem - first of all i'm totally new to Flex. I was search through about 20 Websites but haven't found a Solution:
I have xml file like that:
<xml>
<settings>
<mainurl>http://localhost/website/</mainurl>
<adminurl>http://localhost/website/admin</adminurl>
</settings>
</xml>
Now i have
<mx:Model id="xmlfile" source="conf/config.xml">
That works fine for my DataGrid, but in the
<mx:Script></mx:Script>
section i have a function:
private function loadConf():void
{
var admURL:String = xmlfile.setting.mainurl;
}
But it does not work?!?!
How to get it working? As i told before, i have read a lot of Tutorials and Adobe Examples but always found a way working with it on DataGrids and Stuff like that - that ways all work for me, but i can't get the above working.
Hope you can help.
Thx so much,
Sascha
xmlfile.setting.mainurl
should be
xmlfile.settings.mainurl

Resources