Monday, 23 April 2018

MyAzureBlob

Step1: App.Xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace mylovee
{
public partial class App : Application
{
public App ()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
view raw App.xaml.cs hosted with ❤ by GitHub
Step2: BlobStorageService.cs & Don't Forget to add nuget package(WindowsAzure.Storage)
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace (PleaseAddYourProjectNameSpace)
{
public class BlobStorageService
{
readonly static CloudStorageAccount _cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=samir;AccountKey=xGedI/E6fPRLs2qXEicRbX48qbi1XTEc0XFjWac8wrLUhFAZ8GduGjrHEDcj2zuwmQGleaZEn5f3OUQ7dIg3Og==;EndpointSuffix=core.windows.net");
readonly static CloudBlobClient _blobClient = _cloudStorageAccount.CreateCloudBlobClient();
public static async Task<List<T>> GetBlobs<T>(string containerName, string prefix = "", int? maxresultsPerQuery = null, BlobListingDetails blobListingDetails = BlobListingDetails.None) where T : ICloudBlob
{
var blobContainer = _blobClient.GetContainerReference(containerName);
var blobList = new List<T>();
BlobContinuationToken continuationToken = null;
try
{
do
{
var response = await blobContainer.ListBlobsSegmentedAsync(prefix, true, blobListingDetails, maxresultsPerQuery, continuationToken, null, null);
continuationToken = response?.ContinuationToken;
foreach (var blob in response?.Results?.OfType<T>())
{
blobList.Add(blob);
}
} while (continuationToken != null);
}
catch (Exception e)
{
//Handle Exception
}
return blobList;
}
public static async Task<CloudBlockBlob> SaveBlockBlob(string containerName, byte[] blob, string blobTitle)
{
var blobContainer = _blobClient.GetContainerReference(containerName);
var blockBlob = blobContainer.GetBlockBlobReference(blobTitle);
await blockBlob.UploadFromByteArrayAsync(blob, 0, blob.Length);
return blockBlob;
}
}
}
Step3:MainPage.xaml
<?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:YourNameSpace"
x:Class="YourNameSpace.MainPage"
Title="MyBlobStoragePage">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label x:Name= "mytitle" HorizontalOptions="Center" VerticalOptions="Center"/>
<Image x:Name="myimage" HorizontalOptions="Center" VerticalOptions="Center"/>
<ActivityIndicator x:Name="myindicator"/>
</StackLayout>
</ContentPage>
view raw MainPage.xaml hosted with ❤ by GitHub
Step4:MainPage.Xaml.cs
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace mylovee
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
myindicator.IsRunning = true;
var blobList = await BlobStorageService.GetBlobs<CloudBlockBlob>("photos");
var firstBlob = blobList?.FirstOrDefault();
var photo = new PhotoModel { Title = firstBlob?.Name, Uri = firstBlob?.Uri };
mytitle.Text = photo?.Title;
myimage.Source = ImageSource.FromUri(photo?.Uri);
myindicator.IsRunning = false;
myindicator.IsVisible = false;
}
}
}
Step5:PhotoModel.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace (YourNameSpace)
{
public class PhotoModel
{
public System.Uri Uri { get; set; }
public string Title { get; set; }
}
}
view raw PhotoModel.cs hosted with ❤ by GitHub

Wednesday, 4 April 2018

Simple Calculator

Hey guys this is asimple implementation of calculator in Xamarin Forms Cross Platform
Enjoy the code: :D
Step1: Your MainPage.xaml will look like this
<?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:XamarinCalculator"
x:Class="XamarinCalculator.MainPage">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<OnPlatform.iOS>0,20,0,0</OnPlatform.iOS>
</OnPlatform>
</ContentPage.Padding>
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key ="infostyle" TargetType="Button">
<Setter Property="WidthRequest" Value="60" />
<Setter Property="HeightRequest" Value="60"/>
<Setter Property="BorderRadius" Value="30"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="36"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Grid Padding="5,0" RowSpacing="5" ColumnSpacing="5" >
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label x:Name="resultText" FontSize="48" FontAttributes="Bold" BackgroundColor="Black" Text="0"
TextColor="White" HorizontalTextAlignment="End" VerticalTextAlignment="Center"
LineBreakMode="NoWrap" Grid.ColumnSpan="4" />
<Button Text="AC" Grid.Row="1" Grid.Column="0" Style="{StaticResource infostyle}" BackgroundColor="#808080" Clicked="OnClear" />
<Button Text="√" Grid.Row="1" Grid.Column="1" Style="{StaticResource infostyle}" BackgroundColor="#808080" FontSize="36" Clicked="OnSquareRoot"/>
<Button BorderRadius="30" Text="%" Grid.Row="1" Grid.Column="2" Style="{StaticResource infostyle}" BackgroundColor="#808080" Clicked="OnPercentage"/>
<Button Text="7" Grid.Row="2" Grid.Column="0" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/>
<Button Text="8" Grid.Row="2" Grid.Column="1" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" />
<Button Text="9" Grid.Row="2" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" />
<Button Text="4" Grid.Row="3" Grid.Column="0" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/>
<Button Text="5" Grid.Row="3" Grid.Column="1" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" />
<Button Text="6" Grid.Row="3" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" />
<Button Text="1" Grid.Row="4" Grid.Column="0" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/>
<Button Text="2" Grid.Row="4" Grid.Column="1" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/>
<Button Text="3" Grid.Row="4" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" />
<Button Text="0" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/>
<Button Text="x2" Grid.Row="5" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="squareclicked" Style="{StaticResource infostyle}" />
<Button Text="=" Grid.Row="5" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnCalculate" Style="{StaticResource infostyle}"/>
<Button Text="÷" Grid.Row="1" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}"/>
<Button Text="*" Grid.Row="2" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}" />
<Button Text="-" Grid.Row="3" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}" />
<Button Text="+" Grid.Row="4" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}" />
</Grid>
</ContentPage>
view raw MainPage.xaml hosted with ❤ by GitHub

Step2: Your MainPage.xaml.cs will look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace XamarinCalculator
{
public partial class MainPage : ContentPage
{
int currentState = 1;
string myoperator;
double firstNumber, secondNumber;
public MainPage()
{
InitializeComponent();
OnClear(this, null);
}
void OnSelectNumber(object sender, EventArgs e)
{
Button button = (Button)sender;
string pressed = button.Text;
//validation before button is pressed
//if the current result is 0 in text box then we will direct the calculator to exclude 0 when pressing busttons
if (this.resultText.Text == "0" || currentState < 0 )//at first current state is 1
{
this.resultText.Text = "";//here the text value will be cleared when pressing button
if (currentState < 0) //at first current value is 1 so this condition is excluded
currentState *= -1;
}
this.resultText.Text += pressed;// this condition is called when current state is greater and text box will aquire the pressed
double number;//if we are going to assign two dynamic number for a given variable using try parse method
if (double.TryParse(this.resultText.Text, out number))
{
this.resultText.Text = number.ToString("N0");
if (currentState == 1)
{
firstNumber = number;//at first current state will be 1 and it will assign first number with the pressed number variable
}
else
{
secondNumber = number;//it will be implemented as the number of current state changes i.e. 2
}
}
}
void OnSelectOperator(object sender, EventArgs e)//event is called when the select operator is called
{
currentState = -2;
Button button = (Button)sender;
string pressed = button.Text;
myoperator = pressed;
}
void OnClear(object sender, EventArgs e)// this method is called when we press the AC button
{
firstNumber = 0;
secondNumber = 0;
currentState = 1;
this.resultText.Text = "0";
}
void OnPercentage(object sender,EventArgs e) //This method is useful when we are going to find out percentage of last rsulting variable or the initial variable
{
if ((currentState == -1) || (currentState == 1))//please use some break point to check when we are going to get current state value as -1 or 1
{
//var result = OperatorHelper.MyPercentage(firstNumber, myoperator);
var result = firstNumber / 100;
this.resultText.Text = result.ToString();
firstNumber = result;
currentState = -1;
}
}
void OnCalculate(object sender, EventArgs e) //This method is called when we have both first number and second number and we are going to evaluate those number
{
if (currentState == 2)
{
var result = OperatorHelper.Calculate(firstNumber, secondNumber, myoperator);
this.resultText.Text = result.ToString();
firstNumber = result;
currentState = -1;
}
}
void OnSquareRoot(object sender, EventArgs e) //We call this event when we have one resulting number or initial number ,we are going to find out the square root of that number
{
if ((currentState == -1)||(currentState == 1))
{
//var result = OperatorHelper.MySquareRoot(firstNumber, myoperator);
var result = Math.Sqrt(firstNumber);
this.resultText.Text = result.ToString();
firstNumber = result;
currentState = -1;
}
}
private void squareclicked(object sender, EventArgs e)//We call this method when we have one resulting number or initial number ,we are going to find out the square root of that number
{
if ((currentState == -1) || (currentState == 1))
{
//var result = OperatorHelper.MySquare(firstNumber, myoperator);
var result = firstNumber * firstNumber;
this.resultText.Text = result.ToString();
firstNumber = result;
currentState = -1;
}
}
}
}


Step3: Your OperatorHelper.cs will lookLike this
using System;
using System.Collections.Generic;
using System.Text;
namespace XamarinCalculator
{
public static class OperatorHelper
{
public static double Calculate(double value1 , double value2 , string myoperator)
{
double result = 0;
switch(myoperator)
{
case "÷":
result = value1 / value2;
break;
case "*":
result = value1 * value2;
break;
case "+":
result = value1 + value2;
break;
case "-":
result = value1 - value2;
break;
}
return result;
}
}
}

Step4: You can change Android MainActivity.cs to get round buttons like this:(Not Recommended to implement)
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace XamarinCalculator.Droid
{
[Activity(Label = "XamarinCalculator", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
//TabLayoutResource = Resource.Layout.Tabbar;
//ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
view raw MainActivity.cs hosted with ❤ by GitHub