Search This Blog

Thursday 30 September 2021

How To Store Android Virtual Device in Different Drive.

You Can Set Environment Variable Like Below.

Variable Name: ANDROID_SDK_HOME  

Variable Path: Drive Path Where you want to Store Virtual Device.



Tuesday 28 September 2021

IValueConverter with MarkupExtension

 If you get a warning like IValueConverter with MarkupExtension.

Just Define Converter like this either on App.XAML Or Particular page.

 <ContentPage.Resources>
    <ResourceDictionary>
            <converter:InverseBoolConverter x:Key="InverseBoolConverter" />
    </ResourceDictionary>
</ContentPage.Resources>




Saturday 25 September 2021

Xamarin Form Currency Control

 



CurrencyControl.Xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Name="this"
             x:Class="CurrencyControl.CustomControl.CurrencyControl">
  <ContentView.Content>
        <Grid>
            <Entry FontSize="20" Keyboard="Numeric" Text="{Binding Source={x:Reference this},Path=Amount}"  x:Name="txtAmmount"  Unfocused="txtAmmount_Unfocused" IsVisible="False" Grid.Row="0"  >
            </Entry>
 
            <Label FontSize="20" x:Name="lblDisplayAmount" IsVisible="True" Grid.Row="0" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_ForDisplayTextBox" />
                </Label.GestureRecognizers>
            </Label>
        </Grid>
    </ContentView.Content>
</ContentView>


CurrencyControl.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
 
namespace CurrencyControl.CustomControl
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CurrencyControl : ContentView
    {
        public CurrencyControl()
        {
            InitializeComponent();
        }
 
        private void TapGestureRecognizer_Tapped_ForDisplayTextBox(object sender, EventArgs e)
        {
 
            lblDisplayAmount.IsVisible = false;
            txtAmmount.IsVisible = true;
            txtAmmount.Focus();
        }
 
        private void txtAmmount_Unfocused(object sender, FocusEventArgs e)
        {
            txtAmmount.IsVisible = false;
            lblDisplayAmount.IsVisible = true;
        }
 
 
        #region Amount Property
        public static readonly BindableProperty AmountProperty = BindableProperty.Create(
       propertyName: nameof(Amount),
       returnType: typeof(double),
       declaringType: typeof(CurrencyControl),
       defaultValue: 0d,
       defaultBindingMode: BindingMode.TwoWay,
       propertyChanged: AmountPropertyChanged);
 
        private static void AmountPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (CurrencyControl)bindable;
            if (newValue != null)
            {
                double val = Convert.ToDouble(newValue);
                if (val > 0)
                {
                    control.lblDisplayAmount.Text = "$" + val.ToString("#,##0.00");
                };
            }
        }
        public double Amount
        {
            get { return (double)base.GetValue(AmountProperty); }
            set { base.SetValue(AmountProperty, value); }
        }
        #endregion
 
        #region Place Holder Property
        public static readonly BindableProperty PlaceHolderProperty = BindableProperty.Create(
       propertyName: nameof(PlaceHolder),
       returnType: typeof(string),
       declaringType: typeof(CurrencyControl),
       defaultValue: "",
       defaultBindingMode: BindingMode.TwoWay,
       propertyChanged: PlaceHolderPropertyChanged);
 
        private static void PlaceHolderPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (CurrencyControl)bindable;
            if (newValue != null)
            {
                string val = (string)newValue;
                if (!string.IsNullOrWhiteSpace(val))
                {
                    control.txtAmmount.Placeholder = val;
                };
            }
        }
        public string PlaceHolder
        {
            get { return (string)base.GetValue(PlaceHolderProperty); }
            set { base.SetValue(PlaceHolderProperty, value); }
        }
        #endregion
    }
}


Implementation

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:customcontrol="clr-namespace:CurrencyControl.CustomControl"
             x:Class="CurrencyControl.MainPage">
 
    <StackLayout Margin="10">
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <Label Text="Currency Control Demo" FontSize="Title" Padding="30,10,30,10"/>
        <customcontrol:CurrencyControl Amount="5000" PlaceHolder="Aamount" />
    </StackLayout>
 
</ContentPage>
 




Sunday 19 September 2021

Xamarin MVVM ViewModel Locator

ViewModelLocator.cs

public static class ViewModelLocator
{
 
        public static readonly BindableProperty AutoWireViewModelProperty =
            BindableProperty.CreateAttached("AutoWireViewModel", typeof(bool), typeof(ViewModelLocator), default(bool), propertyChanged: OnAutoWireViewModelChanged);
 
        public static bool GetAutoWireViewModel(BindableObject bindable)
        {
            return (bool)bindable.GetValue(ViewModelLocator.AutoWireViewModelProperty);
        }
 
        public static void SetAutoWireViewModel(BindableObject bindable, bool value)
        {
            bindable.SetValue(ViewModelLocator.AutoWireViewModelProperty, value);
        }
 
        private static void OnAutoWireViewModelChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var view = bindable as Element;
            if (view == null)
            {
                return;
            }
 
            var viewType = view.GetType();
            var viewName = viewType.FullName.Replace(".Views.", ".ViewModels.");
            var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
            var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}ViewModel, {1}", viewName, viewAssemblyName);
 
            var viewModelType = Type.GetType(viewModelName);
            if (viewModelType == null)
            {
                return;
            }
            var viewModel =  Activator.CreateInstance(viewModelType) as BaseViewModel;
            view.BindingContext = viewModel;
        }
}
How to Use: Add this line to your XAML page.

xmlns:models="clr-namespace:CollegeApp.Models"
models:ViewModelLocator.AutoWireViewModel="True"

models: it's folder name where ViewModelLocator exists.




Saturday 18 September 2021

Play Video In Android Default Media Player

Create the Platform-specific Service and execute the following code.

public void PlayVideoFromLocalStorage(string filePath)
{
    try
    {
        var videoFile = new Java.IO.File(Java.Net.URI.Create($"file://{filePath}"));
        Android.Net.Uri fileUri = FileProvider.GetUriForFile(Android.App.Application.Context, $"{Android.App.Application.Context.PackageName}.fileprovider", videoFile);
        Intent intent = new Intent();
        intent.SetAction(Intent.ActionView);
        intent.AddFlags(ActivityFlags.NewTask);
        intent.AddFlags(ActivityFlags.GrantReadUriPermission);
        intent.SetDataAndType(fileUri, "video/*");
        Android.App.Application.Context.ApplicationContext.StartActivity(intent);
    }
    catch(Exception ex)
    { 
 
    }
}
 
 

Popular Posts