Search This Blog

Tuesday 7 February 2017

validation

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

namespace App2
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            et.TextChanged += Et_TextChanged;
        }
        private void Et_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (e.NewTextValue != "")
            {
                if (e.NewTextValue.StartsWith("."))
                {
                    return;
                }
                long value = (long)Convert.ToDouble(e.NewTextValue);
                if (value <= 90.0)
                {
                    et.Text = e.NewTextValue;
                }
                else
                {
                    et.Text = et.Text.Remove(et.Text.Length - 1);
                }
                if (et.Text.Length >= 3)
                {
                    char[] chars = et.Text.ToCharArray();
                    if (chars[2] != '.')
                    {
                        et.Text = et.Text.Remove(et.Text.Length - 1);
                    }
                    else
                    {
                        if (chars[2] == '.')
                        {
                            if (et.Text.Length <= 5)
                            {
                                et.Text = e.NewTextValue;
                            }
                            else
                            {
                                et.Text = et.Text.Remove(et.Text.Length - 1);
                            }
                        }
                        else
                        {
                            et.Text = et.Text.Remove(et.Text.Length - 1);
                        }
                    }
                }
            }
        }
    }
}

Wednesday 1 February 2017

pdf Android dependecy

https://forums.xamarin.com/discussion/25746/how-to-open-pdf-in-xamarin-forms
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.IO;
using System.Threading.Tasks;
using App2.Droid;
using Java.IO;
using Xamarin.Forms;

[assembly: Xamarin.Forms.Dependency(typeof(SaveAndLoad_Android))]
namespace App2.Droid
{
    public class SaveAndLoad_Android : ISaveAndLoad
    {
        public async Task Save(string fileName, String contentType, MemoryStream stream)
        {
            string root = null;
            if (Android.OS.Environment.IsExternalStorageEmulated)
            {
                root = Android.OS.Environment.ExternalStorageDirectory.ToString();
            }
            else
                root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

            Java.IO.File myDir = new Java.IO.File(root + "/Documents");
            myDir.Mkdir();

            Java.IO.File file = new Java.IO.File(myDir, fileName);

            if (file.Exists()) file.Delete();

            try
            {
                FileOutputStream outs = new FileOutputStream(file);
                outs.Write(stream.ToArray());

                outs.Flush();
                outs.Close();
            }
            catch (Exception e)
            {
            }
            if (file.Exists())
            {
                Android.Net.Uri path = Android.Net.Uri.FromFile(file);
                string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                Intent intent = new Intent(Intent.ActionView);
                intent.SetDataAndType(path, mimeType);
                Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
            }
        }
    }
}

pdf PCL Interface

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

namespace App2
{
   public interface ISaveAndLoad
    {
        Task Save(string filename, string contentType, MemoryStream stream);
    }
}

pdfButton Click

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;

namespace App2
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            btnRecord.Clicked += BtnRecord_Clicked;
        }

        private void BtnRecord_Clicked(object sender, EventArgs e)
        {
            PdfFixedDocument document = new PdfFixedDocument();
            PdfPage page = document.Pages.Add();
            PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 24);
            PdfBrush brush = new PdfBrush();
            page.Graphics.DrawString("World is a common name for the whole of human civilization, specifically human experience, history, or the human condition in general, worldwide, i.e. anywhere on Earth or pertaining to anywhere on earth.", helvetica, brush, 100, 100);
            MemoryStream stream = new MemoryStream();
            document.Save(stream);

            DependencyService.Get<ISaveAndLoad>().Save("HelloWorld.pdf", "application/pdf", stream);
        }
    }
}