1

I do not understand how work with commands in WPF. For example I have xaml-file like this:

MainWindow.xaml:

 <Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="assembly=FSharpOnly"
  xmlns:Commands="clr-namespace:View.Commands"
  Title="Sample F# WPF Application Written Only in F#"
  Height="700"
  Width="900">
    <DockPanel LastChildFill="True">
        <TextBlock DockPanel.Dock="Bottom" Background="Aqua"></TextBlock>
        <TextBox DockPanel.Dock="Right" Background="Brown" Width="200"></TextBox>
        <WrapPanel DockPanel.Dock="Top" Background="DimGray">
            <Button Margin="3" Command="Commands:load_tests_command">Загрузить проверки</Button>
            <Button Margin="3">Обновить проверки</Button>
        </WrapPanel>
        <TextBox DockPanel.Dock="Left" Background="Beige">Текст слева</TextBox>
    </DockPanel>
</Window>

and the file where I want to define the command load_tests:

module View.Commands
open System.Windows.Input

open TestLoader

(* commands *)
type LoadTestsCommand () = 
    interface ICommand with
        member this.CanExecute (obj) = true
        member this.Execute (obj) = reload_tests()
        member this.add_CanExecuteChanged (obj) = ()
        member this.remove_CanExecuteChanged (obj) = ()

let load_tests_command =
    LoadTestsCommand()

What you need to change to make it work? Preferably the easiest way.

4
  • This is quite vague question - what have you tried so far? Or do you have a C# sample that you'd like to translate to F#? Commented Mar 16, 2012 at 11:03
  • I havent C# sources. I trying to make command that call some function from some module. And binding this command to some control (button in topic code). Commented Mar 16, 2012 at 11:30
  • 2
    Not really an answer to your question but Daniel Mohl put together some excellent templates for using F# with WP7. As a consequence they have great examples of using F# with WPF/Xaml. You may want to look at his templates--they're in the Visual Studio Gallery Commented Mar 16, 2012 at 12:55
  • 2
    I think this link is what @OnorioCatenacci is talking about: visualstudiogallery.msdn.microsoft.com/… Commented Mar 16, 2012 at 15:44

2 Answers 2

4

I don't know if this is the source of your current error, but creating add_CanExecuteChanged and remove_CanExecuteChanged methods is not the proper way to implement a CanExecuteChanged event. I would do it something like this:

type LoadTestsCommand () = 
    let canExecuteChanged = new Event<_>()
    interface ICommand with
        member this.CanExecute (obj) = true
        member this.Execute (obj) = reload_tests()
        [<CLIEvent>]
        member this.CanExecuteChanged = canExecuteChanged.Publish
Sign up to request clarification or add additional context in comments.

2 Comments

Resolved! I added the ViewModel class with public fields and commands, set new Window instance where DataContext is ViewModel instance and set data bindings to commands and publics. All works. Thank you all
Great! If this answer solved the problem, you should click the green checkmark next to it so that future searchers know it without having to read the comments.
1

A command is a type that implements the ICommand interface. So if you want to create a command in F# you need to implement that interface, see Interfaces (F#)

1 Comment

ok, I changed code in answer. And now get error Missing XmlNamespace, Assembly or ClrNamespace in Mapping instruction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.