Skip to content

Instantly share code, notes, and snippets.

// <summary>
/// This class adds the ability to refresh the list when any property of
/// the objects changes in the list which implements the INotifyPropertyChanged.
/// </summary>
/// <typeparam name="T">
public class ObservableCollectionEx<T> : ObservableCollection<T>
{
// Override the vent so this class can access it
public override event NotifyCollectionChangedEventHandler CollectionChanged;
@LoriBru
LoriBru / MainActivity.cs
Created January 14, 2019 14:27
Snippet to copy a file from embedded assets to a folder.
using (var br = new BinaryReader(Application.Context.Assets.Open(FileName)))
{
using (var bw = new BinaryWriter(new FileStream("/Folder/" + $"{FileName}", FileMode.Create)))
{
var buffer = new byte[2048];
var length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
@LoriBru
LoriBru / MainActivity.cs
Created January 14, 2019 14:24
Snippet to run code on the UI thread.
activity.RunOnUiThread(() =>
{
// Some code to run
});
@LoriBru
LoriBru / MainActivity.cs
Created January 14, 2019 11:51
Checking app permissions.
private void CheckAppPermissions()
{
if ((int)Build.VERSION.SdkInt < 23)
{
return;
}
else
{
if (Context.CheckSelfPermission(Manifest.Permission.ReadExternalStorage) != Permission.Granted
&& Context.CheckSelfPermission(Manifest.Permission.WriteExternalStorage) != Permission.Granted)
@LoriBru
LoriBru / MainActivity.cs
Last active January 14, 2019 11:47
Inflate a fragment to the FrameLayout container.
var tran = FragmentManager.BeginTransaction();
var fragment = new HomeFragment();
tran.Replace(Resource.Id.FrameContainer, fragment);
tran.SetTransition(FragmentTransit.FragmentFade);
tran.CommitAllowingStateLoss();
@LoriBru
LoriBru / MainActivity.cs
Created January 14, 2019 11:46
Set our view from the "main" layout resource which contains a FrameLayout.
SetContentView(Resource.Layout.FrameContainer);
@LoriBru
LoriBru / MainActivity.cs
Created January 14, 2019 11:45
Changing status bar color if version is Lollipop or newer.
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
var color = Color.Argb(210, 57, 169, 220);
Window.SetStatusBarColor(color);
}
@LoriBru
LoriBru / MainVM.cs
Created January 14, 2019 11:32
This method check the presence of internet connection.
public static bool CheckConnection()
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.microsoft.com");
request.Timeout = 5000;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
<TargetType="{x:Type ProgressBar}">
<Setter Property="Foreground" Value="{StaticResource Primary}"/>
<Setter Property="Background" Value="#EEEEEE"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ProgressBar}">
<Grid Name="TemplateRoot" SnapsToDevicePixels="true">
<Rectangle Fill="{TemplateBinding Background}"/>
<Rectangle Name="PART_Track" Margin="0"/>
<Style x:Key="TransparentButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{StaticResource PrimaryDark}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="Template">