Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DAY 1 26/08/2017

Xamarin

XAML(WPF)

MVVM : Model View ViewModel

MVVM light and MVVM cross

MVVM Concept :-

1)Commanding

2)DataBinding

(Either we can have events or command events are tightly coupled to view code so we use commanding
pattern)

3)IOC Inversion of Control /Dependency Injection(Decoupling of View)/Abstraction

4)Messaging

<TargetControl

TargetProperty= “{Binding SourceProperty, bindingProperties}” />

Data Binding –DataContext – XAML Tree Traverse

Binding Modes :- one,two way binding ,OneWayToSource

Comment :- ctrl kc

1)

Creating Class HelloViewMode class

namespace Start1
{
class HelloViewModel
{
public string HelloText { get { return "This is your First Xamerin App"; } }

public string HelloAgain { get; set; }

public string City { get; set; }


}
}

2)MainPage.xaml
<!--<ContentPage.BindingContext>
<local:HelloViewModel HelloAgain="Hi Charu" />
</ContentPage.BindingContext>--> on xaml

<StackLayout Margin="30">
<Label Text="{Binding HelloText}"/>
<Label Text ="{Binding HelloAgain}"/>
<Entry Text="{Binding City}" Keyboard="Text" x:Name="textCity">

</Entry>

</StackLayout>

3) Binding :- either through page or class

<!--<ContentPage.BindingContext>
<local:HelloViewModel HelloAgain="Hi Charu" />
</ContentPage.BindingContext>--> on xaml

Or

Overing MainPage.xaml

protected override void OnAppearing()


{
base.OnAppearing();
HelloViewModel viewModel = new HelloViewModel();
viewModel.HelloAgain = "Hi Charu changed";
this.BindingContext = viewModel;
}

COMMANDING
<Button Text="Enter" Clicked="Button_Clicked"></Button>
<Label Text="{Binding Weather}"></Label>
private void Button_Clicked(object sender, EventArgs e)
{
(this.BindingContext as HelloViewModel).GetNextNumber();
}
//

Create New Class ViewModelBase

INotifyPropertyChanged :- For event handling

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Start1
{

class ViewModeBase : INotifyPropertyChanged


{
protected void RaisePropertyChanged(string propertyName)
{
this?.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// this is same as
// if (PropertyChanged != null)
// {
// PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// }

public event PropertyChangedEventHandler PropertyChanged;


}
}

2) Go to HelloViewModel
class HelloViewModel : ViewModeBase

3)

Modify weather property to notify the xaml for updateing in HelloViewModel


public string Weather {
get
{
return Weather;
}
set
{
Weather = value;
RaisePropertyChanged(nameof(Weather));

Threading
public async Task GetWeatherCommandExecute() // Similar to java threads
{
try
{
// IsWeatherCommandNotInProgress = false;
await Task.Delay(500); // different thread
Weather = $"Weather for {City} is 45.90 degree"; // agin in main threads
RaisePropertyChanged(nameof(Weather));

}
finally
{
// IsWeatherCommandNotInProgress = true;
}

//public void GetNextNumber()


//{
// CurrentNumber = rand.Next(0, 101);
//}

//change to
public async Task GetNextNumber()
{
CurrentNumber = rand.Next(0, 101);
await GetWeatherCommandExecute();
}

5)

Create Icommand property

public ICommand GetWeatherCommand { get; private set; }

Loaded constructor will create on loading


and used the command instead of Event

public HelloViewModel()
{
GetWeatherCommand = new Command(async () => await
GetWeatherCommandExecute() );
}

And change Xaml

<Button Text=" Enter" Command="{Binding GetWeatherCommand}" />

// Calling non async from onload

ResetCommand = new Command( new Action(Resetting));

// Func in C sharp

public Func<string, string> fun1 = myfunc;


public static string myfunc(string abc)
{
return "";
}
MainPage.XML

<?xml version="1.0" encoding="utf-8" ?>


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Start1"
x:Class="Start1.MainPage">

<!--<Label Text="Welcome to Xamarin Forms!"


VerticalOptions="Center"
HorizontalOptions="Center" />-->

<!--<ContentPage.BindingContext>
<local:HelloViewModel HelloAgain="Hi Charu" />
</ContentPage.BindingContext>-->

<StackLayout Margin="30">
<Label Text="{Binding HelloText}"/>
<Label Text ="{Binding HelloAgain}"/>
<Entry Text="{Binding City}" Keyboard="Text" x:Name="textCity">
</Entry>
<!--<Button Text=" Enter" Clicked="Button_Clicked">

</Button>-->

<Button Text=" Enter" Command="{Binding GetWeatherCommand}" />


<Button Text=" Reset" Command="{Binding ResetCommand}" />

<!--<Button Text="Enter" Clicked="Button_Clicked" Height = "20" Width = "60"


Margin = "5"></Button>-->
<Label Text="{Binding Weather}"></Label>
</StackLayout>
<!--<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Start1.TabbedPage1">

<ContentPage Title="Charu1" />


<ContentPage Title="Charu2" />
<ContentPage Title="Charu3" />
</TabbedPage>-->
</ContentPage>
ViewSource Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Start1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
HelloViewModel viewModel = new HelloViewModel();
viewModel.HelloAgain = "Hi Charu changed";
this.BindingContext = viewModel;
}

//private void Button_Clicked(object sender, EventArgs e)


//{
// (this.BindingContext as HelloViewModel).GetNextNumber();
//}

}
}

Created Class Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace Start1
{
class HelloViewModel : ViewModeBase
{
public int CurrentNumber = 0;
public string HelloText { get { return "This is your First Xamerin App"; } }

public string HelloAgain { get; set; }

public string City { get; set; }


private string weather;
public string Weather
{
get
{
return weather;
}
set
{
weather = value;
RaisePropertyChanged(nameof(Weather));

public ICommand GetWeatherCommand { get; private set; }


public ICommand ResetCommand { get; private set; }
public HelloViewModel()
{
GetWeatherCommand = new Command(async () => await
GetWeatherCommandExecute() );
ResetCommand = new Command( new Action(Resetting));
}
public async Task GetWeatherCommandExecute() // Similar to java threads
{
try
{
CurrentNumber = rand.Next(0, 101);
// IsWeatherCommandNotInProgress = false;
await Task.Delay(500); // different thread
Weather = $"Weather for {City} is {CurrentNumber} degree"; // agin in
main threads
RaisePropertyChanged(nameof(Weather));

}
finally
{
// IsWeatherCommandNotInProgress = true;
}

}
// calling Action
public Func<string, string> fun1 = myfunc;
public static string myfunc(string abc)
{
return "";
}
public void Resetting() // Similar to java threads
{
try
{

Weather = $"Weather for {City} is 0 degree"; // agin in main threads


RaisePropertyChanged(nameof(Weather));

}
finally
{
// IsWeatherCommandNotInProgress = true;
}

Random rand = new Random();

//public void GetNextNumber()


//{
// CurrentNumber = rand.Next(0, 101);
//}

//change to
public async Task GetNextNumber()
{
CurrentNumber = rand.Next(0, 101);
await GetWeatherCommandExecute();
}

}
}
Interface Class Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Start1
{

class ViewModeBase : INotifyPropertyChanged


{
protected void RaisePropertyChanged(string propertyName)
{
this?.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// this is same as
// if (PropertyChanged != null)
// {
// PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// }

public event PropertyChangedEventHandler PropertyChanged;


}
}

You might also like