Elmish.XamarinForms/docs/index.md

127 строки
5.8 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

2018-06-19 20:31:34 +03:00
F# Functional App Development, using Xamarin.Forms
========
2018-06-19 20:31:34 +03:00
Never write a ViewModel class again! Conquer the world with clean dynamic UIs!
2018-06-19 20:45:04 +03:00
<img src="https://user-images.githubusercontent.com/7204669/39318922-57c95174-4977-11e8-94a9-cc385101ce5d.png" width="100"> <img src="https://user-images.githubusercontent.com/7204669/39318926-59f844e6-4977-11e8-9834-325a6517ced6.png" width="100"> <img src="https://user-images.githubusercontent.com/7204669/39318929-5b66c776-4977-11e8-8317-ee1c121301d4.png" width="100"> <img src="https://user-images.githubusercontent.com/7204669/39318934-5cbe3c3a-4977-11e8-92aa-c3fdf644b01c.png" width="100"> <img src="https://user-images.githubusercontent.com/7204669/39318936-5e2380bc-4977-11e8-8912-f078744a2bde.png" width="100"> <img src="https://user-images.githubusercontent.com/7204669/39318938-5f6ec4f4-4977-11e8-97a9-779edd3594bc.png" width="100"> <img src="https://user-images.githubusercontent.com/7204669/39318941-60c1b0f0-4977-11e8-8a4a-57e17ef8c6ec.png" width="100">
This library allows you to use a variation of [elmish](https://elmish.github.io/), an Elm architecture implemented in F#,
2018-07-04 17:08:20 +03:00
to build Xamarin.Forms applications for iOS, Android, Mac and more.
2018-06-19 20:31:34 +03:00
2018-06-20 12:20:17 +03:00
> The amount of code I'm *not* writing is great! [@jimbobbennett](https://github.com/jimbobbennett/)
**This project is a sample and may change.**
2018-06-19 20:31:34 +03:00
2018-06-20 02:32:33 +03:00
{% include_relative contents.md %}
2018-06-19 20:44:18 +03:00
2018-06-19 20:31:34 +03:00
Getting started
------
1. Enable Xamarin support in Visual Studio or Visual Studio for Mac.
2018-06-26 19:05:45 +03:00
2018-06-27 02:59:39 +03:00
2. Install the template pack:
2018-06-26 19:05:45 +03:00
dotnet new -i Elmish.XamarinForms.Templates
2018-06-27 02:59:39 +03:00
3. Create a blank F# Functional Xamarin Forms app:
2018-06-27 03:02:26 +03:00
dotnet new elmish-forms-app -lang F# -n SqueakyApp
2018-06-26 20:41:24 +03:00
2018-06-28 18:49:00 +03:00
4. Open, edit, build and deploy in Visual Studio, Visual Studio for Mac and/or "msbuild" command line
2018-06-19 20:31:34 +03:00
2018-06-28 18:43:12 +03:00
SqueakyApp/SqueakyApp.sln
2018-06-19 20:31:34 +03:00
A Basic Example
------
2018-07-04 17:08:20 +03:00
2018-06-19 20:31:34 +03:00
Here is a full example of an app:
2018-07-04 17:08:20 +03:00
2018-06-19 20:31:34 +03:00
```fsharp
/// The messages dispatched by the view
type Msg =
| Pressed
/// The model from which the view is generated
2018-07-04 17:08:20 +03:00
type Model =
2018-06-19 20:31:34 +03:00
{ Pressed: bool }
/// Returns the initial state
let init() = { Pressed=false }
2018-07-04 17:08:20 +03:00
2018-06-19 20:31:34 +03:00
/// The funtion to update the view
let update (msg:Msg) (model:Model) =
match msg with
| Pressed -> { model with Pressed = true }
/// The view function giving updated content for the page
let view (model: Model) dispatch =
2018-07-04 17:08:20 +03:00
if model.Pressed then
2018-07-13 02:40:20 +03:00
View.Label(text="I was pressed!")
2018-06-19 20:31:34 +03:00
else
2018-07-13 02:40:20 +03:00
View.Button(text="Press Me!", command=(fun () -> dispatch Pressed))
2018-06-19 20:31:34 +03:00
2018-07-04 17:08:20 +03:00
type App () =
2018-06-19 20:31:34 +03:00
inherit Application ()
2018-07-04 17:08:20 +03:00
let runner =
2018-06-19 20:31:34 +03:00
Program.mkSimple init update view
|> Program.withConsoleTrace
2018-07-04 17:08:20 +03:00
|> Program.runWithDynamicView
2018-06-19 20:31:34 +03:00
```
2018-07-04 17:08:20 +03:00
2018-06-19 20:31:34 +03:00
The init function returns your initial state, and each model gets an update function for message processing. The `view` function computes an immutable Xaml-like description. In the above example, the choice between a label and button depends on the `model.Pressed` value.
Some advantages of using an immutable model are:
* It is easy to unit test your `init`, `update` and `view` functions
* You can save/restore your model relatively easily
* It makes tracing causality usually very simple
2018-06-20 17:45:07 +03:00
Samples
2018-06-19 20:31:34 +03:00
------
2018-06-20 12:23:10 +03:00
The sample [CounterApp](https://github.com/fsprojects/Elmish.XamarinForms/blob/master/Samples/CounterApp/CounterApp/CounterApp.fs) contains a slightly larger example of Button/Label/Slider elements.
2018-06-19 20:31:34 +03:00
2018-06-20 12:23:10 +03:00
The sample [TicTacToe](https://github.com/fsprojects/Elmish.XamarinForms/blob/master/Samples/TicTacToe/TicTacToe/TicTacToe.fs) contains examples of the Grid and Image elements.
The sample [AllControls](https://github.com/fsprojects/Elmish.XamarinForms/blob/master/Samples/AllControls/AllControls/AllControls.fs) contains examples of instantiating most elements in `Xamarin.Forms.Core`.
2018-06-19 20:31:34 +03:00
2018-06-20 02:30:35 +03:00
The external sample [Calculator](https://github.com/nosami/Elmish.Calculator/) is a small calculator app. (Note: because this is an external sample it may not be up-to-date with the latest version of his library.)
2018-06-20 02:30:19 +03:00
2018-07-07 12:21:51 +03:00
The external sample [PocketPiggyBank](https://github.com/jimbobbennett/PocketPiggyBank) is a small client-server app with login authentication. (Note: because this is an external sample it may not be up-to-date with the latest version of this library.)
2018-06-20 02:30:19 +03:00
2018-06-29 00:47:29 +03:00
Further Resources
--------
2018-07-13 03:46:35 +03:00
<iframe width="560" height="315" src="https://www.youtube.com/embed/si9YdWhbwSI?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
2018-06-29 00:47:29 +03:00
**General Docs**
2018-07-04 17:08:20 +03:00
2018-06-29 00:47:29 +03:00
* [Xamarin](https://docs.microsoft.com/xamarin/)
* [Xamarin Forms](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/)
* [Performance and Security](https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/performance)
* [Deployment and Debugging](https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/)
* [Cross-platform for Desktop Developers](https://docs.microsoft.com/en-us/xamarin/cross-platform/desktop/)
* [Xamarin Essentials](https://docs.microsoft.com/en-us/xamarin/essentials/index?context=xamarin/xamarin-forms)
**Android Setup**
2018-07-04 17:08:20 +03:00
2018-06-29 00:47:29 +03:00
* [SDK](https://docs.microsoft.com/en-us/xamarin/android/get-started/installation/android-sdk?tabs=vswin)
* [Emulator](https://docs.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/)
* [Device](https://docs.microsoft.com/xamarin/android/get-started/installation/set-up-device-for-development)
**iOS Setup**
2018-07-04 17:08:20 +03:00
2018-06-29 00:47:29 +03:00
* [SDK](https://docs.microsoft.com/en-gb/visualstudio/mac/installation)
* [Emulator](https://docs.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/)
* [Pair to Mac](https://docs.microsoft.com/en-us/xamarin/ios/get-started/installation/windows/connecting-to-mac/)
* [Device](https://docs.microsoft.com/en-us/xamarin/ios/get-started/installation/device-provisioning/)
2018-06-19 20:31:34 +03:00
2018-06-20 12:23:10 +03:00
Contributing
------
2018-06-19 20:31:34 +03:00
Please contribute to this library through issue reports, pull requests, code reviews and discussion.
2018-07-04 17:08:20 +03:00
* [Submit a fix to this guide](https://github.com/fsprojects/Elmish.XamarinForms/tree/master/docs)