Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, April 17, 2017

Binding Control to Control in Xamarin Forms

Introduction:

Databinding is a mechanism in Xamarin Forms Application.  Binding is common context, is the process of mapping a property on a Page, to a property in a view or ViewModel. In this Article I will show how bind Control property to Control property  binding in xamarin Forms.
 
Create New Xamarin. Forms Application:
Let's start with creating a new Xamarin Forms Project in Visual Studio.

Open Run - Type Devenev.Exe and enter - New Project (Ctrl+Shift+N) - select Blank Xaml App (Xamarin.Forms Portable) template.
You can refer my previous article for create new xamarin forms application from here - http://www.c-sharpcorner.com/article/how-to-create-first-xamarin-form-application/

Binding Control to Control using xaml :
Data bindings can be defined to link properties of two Controls on the same page. In this case, you set the BindingContext of the target object using the x: Reference markup extension.
In the following sample added one Entry control and Label control for print text
  1. <StackLayout>
  2.  <Entry x:Name ="txtname"></Entry>
  3.  <Label  Text="Welcome Mr.."
  4.           VerticalOptions="Center"
  5.           HorizontalOptions="Center" />
  6.  <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text}"
  7.           VerticalOptions="Center"
  8.           HorizontalOptions="Center" />
  9.  </StackLayout>
Entry Control Name : txtname
Label control adding Binding Context and Binding the value.
While assign binding Context, we can use following two way
BindingContext="{x:Reference Name=txtname} OR BindingContext="{x:Reference txtname} // Name attribute is not required
Same like while assign value to property, we can use following two way
Text="{Binding Path=Text}" OR Text="{Binding Text}" // Path attribute is not required

Binding Control with String Builder:
All the database data or other srorage data is not formatted data so you Can bind Property with string or any type like below  
<Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}"/>
  1. <StackLayout>
  2. <Entry x:Name ="txtname"></Entry>
  3. <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}"
      1. VerticalOptions="Center"
      2. HorizontalOptions="Center" />
  4. </StackLayout>
Binding Control in C#:
You can do it effectively the same way as the XAML example simply by setting your view's BindingContext or Source to the other view.
label.SetBinding(Label.TextProperty, new Binding("<Property Name>", stringFormat: "Welcome Mr {0}", source: <BindingContext Control  name>));
or
var label = new Label{ BindingContext = <Control Name> };
label.SetBinding(Label.TextProperty, new Binding("<Property Name>", stringFormat: "Welcome Mr {0}"));
  1. StackLayout layout = new StackLayout();
  2. var editor = new Editor();
  3. layout.Children.Add(editor);
  4. var label = new Label();
  5. label.SetBinding(Label.TextProperty, new Binding("Text", stringFormat: "Welcome Mr {0}", source: editor));
  6. layout.Children.Add(label);
  7. this.Content = layout;
Binding User Control:
Xamarin Forms doesn’t have a control called a User Control. However, we can make any VisualElement, or combination of Visual Element’s into a reusable control, to use on different pages, just like a User Control.
Create User Control:
Right Click PCL project > Select” Forms XAML View”  and click on Add

In xaml and C# file, change content View to any layout control like Grid, Stack layout

In Xaml Page:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MVVMXamarin.MyControl">
  3.    <Label Text="{Binding Text, Source={x:Reference this}}" />
  4.  
  5. </Grid>
In Code behind file, you might want to make your control, have a bindable property to pass data through to it, on each page it is used. First, create a Bindable Property in your UserControl. It doesn’t have to be bindable if you just want to pass a static value through, however if you want to bind to it, you must make it bindable.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace MVVMXamarin
  8. {
  9.    public partial class MyControl : Grid
  10.    {
  11.        public MyControl()
  12.        {
  13.            InitializeComponent();
  14.        }
  15.        public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MyControl));
  16.        public string Text
  17.        {
  18.            get
  19.            {
  20.                return (string)GetValue(TextProperty);
  21.            }
  22.            set
  23.            {
  24.                SetValue(TextProperty, value);
  25.            }
  26.        }
  27.    }
  28. }
Going to your MainPage, you can now assign a value to the Text property. You can also use {Binding PropertyName} if you want, as this is a bindable property.
  1. <StackLayout>
  2. <Entry x:Name ="txtname"></Entry>
  3. <local:MyControl BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}">
  4. </local:MyControl>
  5.  </StackLayout>


Tuesday, April 11, 2017

Deploy Xamarin.iOS App to iOS Device without an Apple Developer Account

Introduction:
On 2011 at Xamarin announced great and happy news to all c#.net developer, can develop ios application using xamarin .
On 2016 Microsoft announced that they had acquired Xamarin and all the visual studio developer can develop ios and android application, without pay any extra payment.
iOS developer they can do for testing in simulator if they want to test in the device, need to buy Apple Developer account, its cost 99$ /year.
On June 8 2015 at the Apple Worldwide Developers Conference Xcode version 7 was announced and good news is that you can develop and test your apps on your iOS device without a paid Apple Developer account.
This is great news for all the iOS developer, before upload store we can do testing ios application from device without a paid any payment.
In this article, I am going to share about how to deploy xamarin.ioS application to iOS device using free provisioning file
Requirements:
  1. XCode 7 or higher
  • Go to Mac machine and verify Xcode version is 7 or higher, if it’s not update xcode
  1. iOS 9 or higher
  2. Create free Apple ID from here( https://appleid.apple.com/account#!&page=create)
You can follow bellows steps for how to setup and deploy xamarin.iOS application into iOS device.
Step 1: Add Apple Account from Xcode
In mac Machine, Open Xcode , Xcode Menu >Preferences > Click on Account tab > Select + Add Apple ID > login with Apple ID
Click on View Details and create signing identity and click on create button next to iOS development
Step 2: Create XCode project:
Xcode > create new project > project name as “testapp” > Connect iphone /IPad from mac machine > Select your device and press run button
You will get Code Singing issue
Issue 1: Xcode 7: Project requires a development team
If your using XCode 7 means, you will get following error
Select your Team as below from Project Options and General tab
Issue 2: Xcode8: Code signing Error in Build Error:
If you are using Xcode 8 or above, the code signing error will appear while build xcode project


Solution:
Step 2.1: Go to the ioS project editor
Step 2.2: Select the General Section
Step 2.3: Select Checkbox for Automatically manage signing
Step 2.4: Select the Personal Team you created earlier from the ‘Team’ dropdown in the ‘Signing’ section
Step 3: Trusting the certificate from device
You will get following error could not launch “Your app name “
In your iOS device > open the Settings > go to general > Device management (if you’re not getting this option re deploy the application again)
You will see the email address associated with the Apple ID you used to code sign your app.
Tap it, then tap ‘Trust <your_email>’
Now go back to xcode and run the application
Step 4: Update bundle Identifier from Xamarin Project:


In Xamarin.iOS project , Goto Info.Plist file > Change Bundle Identifier (use same xcode project bundle identifier)


And make sure from project options and select IOS bundle signing > Signing identity select your apple account




Select your iphone device from xamarin.ios project and click on run the application.


I believe this article will helpful for you deploy application to ios device ,if you have question /issue/feedback share into comments box .