Skip to content

Commit

Permalink
let's open new windows/dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliohome committed Sep 6, 2018
1 parent 4e8cfd2 commit 86f8ba3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions ElmCounter/ElmCounter.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<None Include="App.config" />
<Content Include="packages.config" />
<Resource Include="MainWindow.xaml" />
<Resource Include="SecondWindow.xaml" />
</ItemGroup>
<ItemGroup>
<Reference Include="Elmish">
Expand Down
4 changes: 3 additions & 1 deletion ElmCounter/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Count}" TextAlignment="Center" FontSize="18" Grid.Row="0" />
<Button Command="{Binding Increment}" FontSize="18" Content="Increment" Grid.Row="1" />
<Button Command="{Binding Decrement}" FontSize="18" Content="Decrement" Grid.Row="2" />
<Button Command="{Binding IncrementDelayed}" FontSize="18" Content="Increment (Delayed)" Grid.Row="3" />
<Button Command="{Binding IncrementX10}" FontSize="18" Content="Increment X 10" Grid.Row="4" />
<Button Command="{Binding AsyncSeq}" FontSize="18" Content="Async Seq X 3" Grid.Row="5" />
<TextBlock Text="{Binding State}" TextAlignment="Center" FontSize="18" Grid.Row="6" />
<Button Command="{Binding SayHello}" FontSize="18" Content="Say Hello!" Grid.Row="6" />
<TextBlock Text="{Binding State}" TextAlignment="Center" FontSize="18" Grid.Row="7" />
</Grid>
</Window>
41 changes: 31 additions & 10 deletions ElmCounter/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ open Elmish
open Elmish.WPF
open System
open FsXaml
open System.Windows.Threading

type State = { Count: int; State: string }
type State = { Count: int; State: string; MyName: string }

type GoOnMsg = {Msg: string; ToGo: int}
type GoOnAdding = {Adder: int; ToGo: int}
Expand All @@ -20,10 +21,13 @@ type Msg =
| ShowMsgButGoOn of GoOnMsg
| AddAndGoOn of GoOnAdding
| StartSeqMsgs
| SayHello
| NameIs of string
| ShowName

let rnd = Random()

let init() = { Count = 0; State="Ready" }, Cmd.none
let init() = { Count = 0; State="Ready"; MyName= "Mister X" }, Cmd.none

let simulateException level =
if rnd.Next(level) = level-1
Expand Down Expand Up @@ -51,7 +55,10 @@ let asyncSeqEvery milliseconds (messages: Msg list) : Cmd<Msg> =
// then you can use it like this:
// StartSeqMsgs -> state, asyncSeqEvery 1000 [Increment; Increment; Increment]

type HelloWindow = XAML<"SecondWindow.xaml">
type MainWindow = XAML<"MainWindow.xaml">

let mainWindow = MainWindow()

let update msg state =
match msg with
Expand Down Expand Up @@ -88,19 +95,33 @@ let update msg state =

| StartSeqMsgs -> state, asyncSeqEvery 1000 ([1..3] |> List.map (fun i -> IncrementMofN (i, 3)))

| SayHello ->
mainWindow.Dispatcher.Invoke(fun () ->
let helloWin = HelloWindow()
helloWin.DataContext <- mainWindow.DataContext
helloWin.ShowDialog()) |> ignore
state, Cmd.none

| NameIs myName -> { state with MyName = myName }, Cmd.ofMsg ShowName
| ShowName -> {state with State= sprintf "Hello %s" state.MyName }, Cmd.none

let bindings model dispatch = [
"Count" |> Binding.oneWay (fun state -> state.Count)
"State" |> Binding.oneWay (fun state -> state.State)
"Increment" |> Binding.cmd (fun state -> Increment)
"Decrement" |> Binding.cmd (fun state -> Decrement)

"Count" |> Binding.oneWay (fun state -> state.Count)
"State" |> Binding.oneWay (fun state -> state.State)
"Increment" |> Binding.cmd (fun state -> Increment)
"Decrement" |> Binding.cmd (fun state -> Decrement)
"IncrementDelayed" |> Binding.cmd (fun state -> IncrementDelayed)
"IncrementX10" |> Binding.cmd (fun state -> IncrementTimes 10)
"AsyncSeq" |> Binding.cmd (fun state -> StartSeqMsgs)
"IncrementX10" |> Binding.cmd (fun state -> IncrementTimes 10)
"AsyncSeq" |> Binding.cmd (fun state -> StartSeqMsgs)
"NameMsg" |> Binding.oneWay (fun state -> sprintf "Hello! Your counter is %d!" state.Count)
"MyName" |> Binding.twoWay (fun state -> state.MyName) (fun name state -> NameIs name)
"SayHello" |> Binding.cmd (fun _ -> SayHello)
]

type MainWindow = XAML<"MainWindow.xaml">


[<EntryPoint; STAThread>]
let main argv =
Program.mkProgram init update bindings
|> Program.runWindow (MainWindow())
|> Program.runWindow (mainWindow)
16 changes: 16 additions & 0 deletions ElmCounter/SecondWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Elmish Hello" >
<Grid>
<StackPanel>
<DockPanel>
<TextBlock Text="Your Name: " />
<TextBox Text="{Binding MyName}" />
</DockPanel>
<TextBlock Text="{Binding NameMsg}" />
</StackPanel>
</Grid>
</Window>

0 comments on commit 86f8ba3

Please sign in to comment.