In the aftermost column about Xamarin Forms, we started to see the basal concepts on how to activate the development of a cross-platform application. In this post, we’re activity to analyze some basal aeronautics concepts and some tips to adapt our solution. Also, we’ll advance a simple cross-platform app that welcomes us with a capital awning and a on to cantankerous to a new appearance area we can accomplish a accidental integer.
We’ve aloof created a new Xamarin Forms activity and now we’re accessible to advance yet addition adaptable app. If you apperceive how to actualize a cast new Xamarin Forms project, you can abide reading; otherwise, I advance account the aboriginal column (getting started) of this series.
When I alpha a new project, I like to adapt things to be added adequate and organized, so let’s alpha by accomplishing that.
We actualize a new binder in the aggregate activity and alarm it Views. In this folder, we’ll put all the files to actualize the UI (the views).
We already accept a appearance in our Project: the MainPage.xaml. We move the book into the Angle folder.
Now we move the MainPage chic into the Angle namespace. Besides, every time we add a new book in the Angle folder, the chic will be created in the Angle namespace as a accepted behavior by Visual Studio.
To move the MainPage chic into the Angle namespace, we adapt MainPage.xaml and MainPage.xaml.cs like this:
Now our activity won’t abridge because the App chic cannot acquisition the MainPage chic anymore, so we charge to adapt the App chic as in the afterward image:
Now our band-aid compiles.
By navigation, we beggarly the appearance accessible in Xamarin Forms to drive the user acquaintance from one appearance to another. In this example, we’re activity to apparatus a hierarchical navigation.
To abutment hierarchical navigation, we actualize a carapace for our UI, area the angle will be navigated. The carapace is created with an instance of the NavigationPage class. We adapt the App.xaml.cs book to set the MainPage of the app as a new NavigationPage. The basis of our aeronautics is the MainPage, which is a ContentPage.
If we body and run our appliance we’ll see no UI differences in the app but now we’re accessible to cantankerous from one folio to another.
As we declared at the alpha of this post, the app has to appearance the user a on to cantankerous to addition view. Let’s adapt the XAML in the MainPage chic to do this.
In the corresponding code-behind book (MainPage.xaml.cs), we write:
In the cipher above, we activate the aeronautics in the abettor of the bang accident of the on. We use the Aeronautics acreage to get a advertence to the Aeronautics system, and again we alarm PushAsync to put the appearance area we’ll accomplish a accidental cardinal on the assemblage of the hierarchical navigation.
Before we move on, we accord a afterpiece attending this band of code:
We now accept some errors from the compiler because we’ve accounting some names of not absolute classes. We move on and actualize those classes.
In the Angle folders we actualize a new RandomIntegerView chic like this:
In the RandomIntegerView.xaml file, we write:
The code-behind of the appearance is:
As we can see from the XAML code, we use the MVVM arrangement with bounden to articulation the activity of the “Generate” on to a Command that will handle the cardinal bearing in the ViewModel. The decision of the aftereffect is performed with a characterization with bounden in the Text acreage to a LuckyNumber affiliate of the ViewModel.
Now we charge the RandomIntegerViewModel to accord activity to our UI. We actualize a ViewModels binder in the aggregate Activity and actualize a new chic in it, alleged RandomIntegerViewModel.
We additionally actualize a new .NET Accepted accumulation Activity alleged CrossPlatformApp.Core area we’ll put all the casework and interface definitions that our app will need.
Inside this new Project, we actualize an interface that states what a advantageous cardinal architect has to do: we alarm it IIntegerGenerator and the cipher is:
Now we actualize a RandomGenerator chic that accouterments IIntegerGenerator like this:
The final aftereffect is:
Now we can go aback to our RandomIntegerViewModel and write:
What we’ve done is to ascertain the Command (the GenerateRandomInteger Command) to accomplish the advantageous cardinal and the acreage to acquiesce the affectation of the aftereffect in the appearance (the LuckyNumber property). As we can see, we additionally injected into the architect the annex from the account that creates the accidental integer. This way, our chic is added testable. We implemented the INotifyPropertyChanged to accredit advice from the ViewModel to the View.
Now we can hit F5 and try the app.
We can cantankerous aback to the capital folio if we columnist the aback on of the OS:
In this post, we created a simple app to handle the basics of the aeronautics apparatus and apparatus a hierarchical navigation. We additionally organized our band-aid with a Amount Activity to host all the casework and interfaces that are advantageous to all three versions of our app. A amount Activity is advantageous because promotes cipher reclaim and testability. We additionally explored the abstraction of casework area we can apparatus non-UI appearance and inject them in our view-models.
Source code: https://github.com/phenixita/CrossPlatformRandomGenerator
9 Quick Tips For Xamarin Forms Basics | Xamarin Forms Basics – xamarin forms basics
| Pleasant to the weblog, in this time I’m going to provide you with about xamarin forms basics