Advertisement
Guest User

Xamarin Exception Sample

a guest
Jun 14th, 2014
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. /* MAINVIEW.CS */
  2. public class MainView : ContentPage
  3.     {
  4.         public MainView()
  5.         {
  6.             // Label Header
  7.             Label header             = new Label();
  8.             header.Text              = "Main Menu";
  9.             header.Font              = Font.BoldSystemFontOfSize (40);
  10.             header.HorizontalOptions = LayoutOptions.Center;
  11.  
  12.             // Declaring Buttons
  13.             Button btnGetAllUsers      = new Button ();
  14.             btnGetAllUsers.Text        = "Get All Users";
  15.             btnGetAllUsers.Font        = Font.BoldSystemFontOfSize (NamedSize.Large);
  16.             btnGetAllUsers.BorderWidth = 1;
  17.  
  18.             Button btnCreateUser      = new Button();
  19.             btnCreateUser.Text        = "Create a User";
  20.             btnCreateUser.Font        = Font.BoldSystemFontOfSize(NamedSize.Large);
  21.             btnCreateUser.BorderWidth = 1;
  22.  
  23.             // Button Click Events
  24.         // I AM PUSHING THE SAME VIEW CLASS ON THE "BUTTON CLICK EVENT" JUST TO ILUSTRATE THE EXCEPTION. THE SAME ERROR IS RAISED
  25.             // BY DOING THIS.
  26.             btnGetAllUsers.Clicked += async (sender, args) => await Navigation.PushAsync (ViewsHandler.GetMainPage());
  27.  
  28.             // Building the Page
  29.             this.Content = new StackLayout
  30.             {
  31.                 Children =
  32.                 {
  33.                     header,
  34.  
  35.                     new StackLayout
  36.                     {
  37.                         HorizontalOptions = LayoutOptions.Center,
  38.                         Children = { btnGetAllUsers, btnCreateUser }
  39.                     }                
  40.                 }
  41.             };
  42.         }
  43.     }
  44.  
  45. /* VIEWS HANDLER.CS - JUST A WRAPPER TO PROVIDE NEW INSTANCES OF THE VIEWS */
  46. public class ViewsHandler
  47. {
  48.         public static Page GetMainPage()
  49.         {
  50.             return new NavigationPage (new MainView());
  51.         }
  52.  
  53.         public static Page GetUsersListPage()
  54.         {
  55.             return new NavigationPage (new UsersListView ());
  56.         }
  57. }
  58.  
  59. /* ANDROID INITIALIZATION CODE */
  60. public class MainActivity : AndroidActivity
  61. {
  62.         protected override void OnCreate(Bundle bundle)
  63.         {
  64.             base.OnCreate(bundle);
  65.  
  66.             // Initializing Xamarin Form
  67.             Xamarin.Forms.Forms.Init (this, bundle);
  68.  
  69.             // Setting main page
  70.             SetPage (ViewsHandler.GetMainPage());
  71.         }
  72. }
  73.  
  74. /* WINDOWS PHONE INITIALIZATION CODE */
  75. public MainPage()
  76. {
  77.             InitializeComponent();
  78.  
  79.             // Sample code to localize the ApplicationBar
  80.             //BuildLocalizedApplicationBar();
  81.  
  82.             Forms.Init ();
  83.  
  84.             Content = TrumpetMobile.Views.ViewsHandler.GetMainPage().ConvertPageToUIElement (this);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement