F#ã§WPFããã¨ãã®Tipsã¨ã(ãã®1)
æè¿ãFull F#ã§WPFãã¦ãã®ã§ãTipsã¦ããªãã®ãã¾ã¨ãããã¨æãã¾ãã ãã®2ã¯Tipsããã¾ãã°ããããããã¾ããããé度ãªæå¾ ã¯ããªãã§ãã ããã
ããã¸ã§ã¯ãã®ä½ãæ¹
åºæ¬çã«ã¯ãPure F# WPF GUIã¢ããªéçºã«åãã¦ã«æ¸ãã¦ããéãã§ãã ç´°ãã注æç¹ãããã®ã§æ¸ãã¦ããã¾ãã
.NET Frameworkã®é¸æã«é¢ãã注æç¹
.NET Frameworkã4.5.1ã4.5.2ãªã©ã®ãããª3æ¡ã®ãã®ãé¸ã¼ãã¨ããã¨ãããã¸ã§ã¯ãä½æã«å¤±æãã¾ãã ã¾ãã4.6ãé¸ãã§ã4.5ã¨ãã¦ä½ãããã®ã§ããã®ç¹ã«ã注æãã¾ãããã
åå以å¤ã§ã®ä½æ
è¨ãã¾ã§ããªããã¨ããããã¾ãããä¸å¿ã ä¸åã§ããF# Empty Windows App (WPF)ãã使ã£ã¦ããã¸ã§ã¯ããä½ã£ãå ´åããã以éã¯ããªã³ã©ã¤ã³ãã®æ¹ã§ã¯ãªãããã¤ã³ã¹ãã¼ã«æ¸ã¿ãã®ãã³ãã¬ã¼ããé¸ã¶ãã¨ã«ãªãã¾ãã
MainView.xaml.fs
ãF# Empty Windows App (WPF)ãã¨ãããã³ãã¬ã¼ãã§ããã¸ã§ã¯ããä½ãã¨ãä¸è¨ã®å 容ã§MainView.xaml.fsã¨ãããã¡ã¤ã«ãçæããã¾ãã
namespace ViewModels open System open System.Windows open FSharp.ViewModule open FSharp.ViewModule.Validation open FsXaml type MainView = XAML<"MainWindow.xaml", true> type MainViewModel() as self = inherit ViewModelBase()
ä¸è¦ãªå
å®éã«ãã®ãã³ãã¬ã¼ãã§ä½ãéãMVVMã§ä½ãå ´å㯠MainView
ã¯ä¸è¦ã§ãã
ããã¤ã¯C#ã§ã®ã³ã¼ãããã¤ã³ãã§ä½ãå ´åã«ä½¿ããã®ã«ç¸å½ãããããMVVMã§è¡ãå ´åã¯æ¶ãã¦ãã¾ã£ã¦å¤§ä¸å¤«ã§ãã
å½ç¶ãC#ã§ã®ã³ã¼ãããã¤ã³ãã§ä½ãå ´å㯠MainViewModel
ã®æ¹ãä¸è¦ãªã®ã§ãæ¶ãã¦ãã¾ãã¾ãããã
ãã ããçæãããã³ã¼ãã¯å®å
¨ã«MVVMã§ãããã¨åæãªã³ã¼ãã«ãªã£ã¦ããã®ã§ãæ£ç´ãå§ããã¾ããã
ç´°ãããã¨ã§ãããMainViewModel
ã®å®ç¾©ã® =
ã®å¾ãã«1ã¤ããã®ä¸ã®è¡ã«4ã¤ã空ç½æåãç´ãè¾¼ãã§ããã®ã注æãã¾ãããã
æ°ã«ãªã人ã¯æ¶ãã¦ãã¾ãã¨ããã§ãããã
App.fs
MainView.xaml.fsåæ§ã«ãã³ãã¬ã¼ãã«ãã£ã¦ä½ããããã¡ã¤ã«ã§ãã
module main open System open FsXaml open System.Windows type App = XAML<"App.xaml"> [<STAThread>] [<EntryPoint>] let main argv = App().Root.Run()
ã¢ã¸ã¥ã¼ã«å
ãªããå°æå㧠main
ã¨ããã¢ã¸ã¥ã¼ã«ã«ãªã£ã¦ãã¾ãã
ç´°ããé¨åã§ãããMain
ã¨ã App
ã¨ãã«å¤ããã»ããããã§ãããã
XAML Type Providerã«ã¤ãã¦
ãã³ãã¬ã¼ãã§ä½ãããæ§æãMVVMã¹ã¿ã¤ã«ã§ä½ããã¨ãæèãããã®ã«ãªã£ã¦ãã¾ãã®ã§ããã¾ãXAML Type Providerã®åºçªã¯ãªãã®ã§ãããType Providerã®ä½¿ãæ¹ã®ä¾ã¨ãã¦è§¦ãã¦ããã¾ãã
ã³ã¼ãçæã®ä»£æ¿ã¨ãã¦ã®Type Provider
C#ã§ã®ã³ã¼ãããã¤ã³ãã§ã¯designer.csãã¡ã¤ã«ãèªåçæããã¾ãããF#ã¯XAML Type Providerã®åã«ãããããã£ããã¡ã¤ã«ã¯çæããã¾ããã
ãã®ä»£ããã«ãtype HogeView = XAML<"xamlãã¡ã¤ã«å", true>
ã®ããã«ãã¦ãã¥ã¼ç¨ã®åãXAMLããçæãããã¨ã«ãªãã¾ãã
ãã³ãã¬ã¼ãã«ãã£ã¦çæãããããã¸ã§ã¯ããå¼ã£ã¦ãã¡ãã£ã¨ä½¿ã£ã¦è¦ã¾ãããã
ä¸è¦ãªãã¡ã¤ã«ãæ¶ã
XAML Type Providerã使ãå ´åã¯æ¬¡ã®ãã¡ã¤ã«ã¯ä¸è¦ã§ããæ¶ãã¦ãã¾ãã¾ãããã
MainWindow.xamlãæ¸ãæãã
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ViewModels;assembly=FsEmptyWindowsApp3" xmlns:fsxaml="http://github.com/fsprojects/FsXaml" Title="MVVM and XAML Type provider" Height="200" Width="400"> <Grid> <Label x:Name="Message"/> </Grid> </Window>
Message
ã¨ããååãæã£ã Label
ã追å ãã¾ããã
App.fsãæ¸ãæãã
App.fsãæ¸ãæããå ã»ã©è¿½å ããã©ãã«ã«ããã¹ããè¨å®ãã¦ã¿ã¾ãã
module App open System open FsXaml open System.Windows type MainView = XAML<"MainWindow.xaml", true> [<STAThread>] [<EntryPoint>] let main argv = // XAML Type Providerã«ãã£ã¦çæãããåã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã let view = MainView() // Messageã¨ããããããã£ã«ã¢ã¯ã»ã¹ããContentã«æååãè¨å® view.Message.Content <- "Hello XAML Type Provider!" // Application.Runã«XAMLã®Rootè¦ç´ ã§ããWindowãªãã¸ã§ã¯ãã渡ãã¦ã // ã¢ããªã±ã¼ã·ã§ã³ãèµ·å let app = Application() app.Run(view.Root)
ããã§ãã«ããã¦å®è¡ããã¨ããHello XAML Type Provider!ãã¨è¡¨ç¤ºãããã¦ã£ã³ãã¦ãéãã¾ãã
XAML Type Providerã®åã«ãã£ã¦ãèªåçæã³ã¼ããªãã§ãã¥ã¼ã«ã¢ã¯ã»ã¹ã§ãã¾ããã
ã¤ãã³ãã®ç»é²
ããã ãã ã¨ãå ¨é¨XAMLã«æ¸ãã°ããããããã¨ãã話ã«ãªã£ã¦ãã¾ãã®ã§ããã¿ã³ã追å ãã¦ãã¿ã³ã«ã¤ãã³ããç»é²ãã¦ã¿ã¾ãããã
MainWindow.xamlã®å 容ãå¤æ´ãã¾ãã
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ViewModels;assembly=FsEmptyWindowsApp3" xmlns:fsxaml="http://github.com/fsprojects/FsXaml" Title="MVVM and XAML Type provider" Height="200" Width="400"> <StackPanel> <Label x:Name="Message"/> <Button x:Name="Btn">Please Click!</Button> </StackPanel> </Window>
Grid
ã¯é¢åãªã®ã§ StackPanel
ã«å¤ããLabel
ã®ä¸ã« Btn
ã¨ããååã§ãã¿ã³ã追å ãã¾ããã
ããã¦ãapp.Run(view.Root)
ã®åã«ã¤ãã³ãã追å ããã³ã¼ããæ¸ãã¾ãã
view.Btn.Click.Add(fun x -> view.Message.Content <- "Clicked!")
ããããã«ããã¦å®è¡ããã¨ãå ã»ã©ä½ã£ãç»é¢ã«ãã¿ã³ã追å ãããã¦ã£ã³ãã¦ãéãã¾ãã ããã¦ããã¿ã³ãã¯ãªãã¯ããã¨ã©ãã«ã®å 容ãå¤ããã¾ãã
ãããªæãã§ãç»é¢ã®è¦ç´ ã«ååãä»ãã¦ããã°ãããªãç°¡åã«ç»é¢ãä½ãããã¨ãåããã¾ãã ã¾ããã¡ãã£ã¨ããç»é¢ã§ããã°XAML Type Providerãé¸æè¢ã«å ¥ãã¦ãããããªãã¨ããæãã§ããããã