Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:34:34+00:00 2026-05-15T05:34:34+00:00

I’m trying to implement drag and drop in Silverlight using F# and asynchronous workflows.

  • 0

I’m trying to implement drag and drop in Silverlight using F# and asynchronous workflows.

I’m simply trying to drag around a rectangle on the canvas, using two loops for the the two states (waiting and dragging), an idea I got from Tomas Petricek’s book “Real-world Functional Programming”, but I ran into a problem:

Unlike WPF or WinForms, Silverlight’s MouseEventArgs do not carry information about the button state, so I can’t return from the drag-loop by checking if the left mouse button is no longer pressed. I only managed to solve this by introducing a mutable flag.

Would anyone have a solution for this, that does not involve mutable state?

Here’s the relevant code part (please excuse the sloppy dragging code, which snaps the rectangle to the mouse pointer):

type MainPage() as this =
    inherit UserControl()
    do
        Application.LoadComponent(this, new System.Uri("/SilverlightApplication1;component/Page.xaml", System.UriKind.Relative))
    let layoutRoot : Canvas = downcast this.FindName("LayoutRoot")
    let rectangle1 : Rectangle = downcast this.FindName("Rectangle1")

    let mutable isDragged = false

    do
        rectangle1.MouseLeftButtonUp.Add(fun _ -> isDragged <- false)

        let rec drag() = async {
            let! args = layoutRoot.MouseMove |> Async.AwaitEvent
            if (isDragged) then
                Canvas.SetLeft(rectangle1, args.GetPosition(layoutRoot).X)
                Canvas.SetTop(rectangle1, args.GetPosition(layoutRoot).Y)
                return! drag()
            else
                return()
            } 
        let wait() = async {
            while true do
                let! args = Async.AwaitEvent rectangle1.MouseLeftButtonDown
                isDragged <- true
                do! drag()
            }

        Async.StartImmediate(wait())
        ()

Thank you very much for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:34:34+00:00Added an answer on May 15, 2026 at 5:34 am

    The way to solve this issue is to use an overloaded AwaitEvent that allows you to wait for two events. Instead of just waiting for MouseMove, you can also wait for the MouseUp event – in the first case, you can continue moving and in the second case, you can return from the loop and stop drag&drop (this is actually discussed later in the book in section 16.4.5).

    Here is the code – it actually uses AwaitObservable variant of the method (see below), which is a better choice in general, because it works with Observable.map and similar combinators (in case you wanted to use these).

    let! args = Async.AwaitObservable(layoutRoot.MouseMove, layoutRoot.MouseUp)
    match args with
    | Choice1Of2(args) ->
        // Handle the 'MouseMove' event with 'args' here
        Canvas.SetLeft(rectangle1, args.GetPosition(layoutRoot).X)  
        Canvas.SetTop(rectangle1, args.GetPosition(layoutRoot).Y)  
        return! drag()  
    | Choice2Of2(_) ->
        // Handle the 'MouseUp' event here
        return()  
    

    As far as I know, the overloaded AwaitObservable method is not available in the F# libraries (yet), but you can get it from the book’s web site, or you can use the following code:

    // Adds 'AwaitObservable' that takes two observables and returns
    // Choice<'a, 'b> containing either Choice1Of2 or Choice2Of2 depending
    // on which of the observables occurred first
    type Microsoft.FSharp.Control.Async with   
      static member AwaitObservable(ev1:IObservable<'a>, ev2:IObservable<'b>) = 
        Async.FromContinuations((fun (cont,econt,ccont) -> 
          let rec callback1 = (fun value ->
            remover1.Dispose()
            remover2.Dispose()
            cont(Choice1Of2(value)) )
          and callback2 = (fun value ->
            remover1.Dispose()
            remover2.Dispose()
            cont(Choice2Of2(value)) )
          // Attach handlers to both observables
          and remover1 : IDisposable  = ev1.Subscribe(callback1) 
          and remover2 : IDisposable  = ev2.Subscribe(callback2) 
          () ))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 480k
  • Answers 480k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are including WAY to much stuff. Your error is… May 16, 2026 at 6:16 am
  • Editorial Team
    Editorial Team added an answer Where did this code come from? And what OS is… May 16, 2026 at 6:16 am
  • Editorial Team
    Editorial Team added an answer Perhaps you are talking about the jQuery History plugin here:… May 16, 2026 at 6:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.