Skip to content

Instantly share code, notes, and snippets.

@Driv4r
Driv4r / CustomView.cs
Last active June 14, 2016 23:32
Custom Renderers - Xamarin (https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/) CustomView.cs goes in the PCL project Views folder. MainPage.cs/MainPage.xaml goes in the PCL project Pages folder. CustomViewRenderer.cs goes in the iOS/Android project
using Xamarin.Forms;
namespace Myproject.Views
{
public class CustomView : View
{
public CustomView()
{
}
}
@Driv4r
Driv4r / App.cs
Last active June 14, 2016 23:42
Using platform specific code in the PCL project by using an interface that each project instantiates as their native implementation. This example uses the iOS NSUserDefaults, but Android SharedPreferences would used for the Android implementation.
using Myproject.Interfaces; using Myproject.Pages; using System; using Xamarin.Forms; namespace Myproject { /// <summary> /// The main class that is used to initialze xamarin forms. /// </summary> public partial class App : Application { /// <summary> /// Gets or sets the UserPreferencesStore. /// </summary> public static IUserPreferencesStore UserPreferencesStore { get; private set; } public App(IUserPreferencesStore userPreferencesStore) { InitializeComponent(); //Usually uses an init method, but this allows earlier access. App.UserPreferencesStore = userPreferencesStore; //App.Current.MainPage = new NavigationPage(new MainPage()); } } }
@Driv4r
Driv4r / MessagingCenterExample.cs
Created June 14, 2016 23:55
Simple Xamarin MessagingCenter example.
//Without sending arguments
//Subscribe
MessagingCenter.Subscribe<object> (this, "Arrived", (sender) => {
// do something whenever the "Arrived" message is sent from whatever type
//<object> is.
});
//Send
//All subscribers to "Arrived" of type "this" will be notified.
@Driv4r
Driv4r / LoginPage.xaml
Last active June 15, 2016 00:06
Example for using basic, shared resources in xaml. Put this in App.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" x:Class="Myproject.Pages.LoginPage" Padding="25, 20, 25, 10" BackgroundColor="{StaticResource defaultBackgroundColor}" Title="Sign In"> <ContentPage.Content> <StackLayout Padding="10,0,10,20" VerticalOptions="FillAndExpand"> <StackLayout x:Name="contentLayout" VerticalOptions="CenterAndExpand"> <Image Source="PPP_Logo.png"/> <Label Text="Email" TextColor="{StaticResource defaultLabelTextColor}" /> <Entry x:Name="emailEntry" /> <Label Text="Password" TextColor="{StaticResource defaultLabelTextColor}" /> <Entry x:Name="passwordEntry" IsPassword="true"/> <Button Text="Sign In" TextColor="{StaticResource defaultButtonTextColor}" Clicked="OnLoginButtonClicked" BackgroundColor="{StaticResource greenButtonBackgroundColor}"/>