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 3750896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T08:59:49+00:00 2026-05-19T08:59:49+00:00

I’m trying to use the following F# code to access a Xaml control. let

  • 0

I’m trying to use the following F# code to access a Xaml control.

let (?) (source:obj) (s:string) =
    match source with 
    | :? ResourceDictionary as r ->  r.[s] :?> 'T
    | :? Control as source -> 
        match source.FindName(s) with 
        | null -> invalidOp (sprintf "dynamic lookup of Xaml component %s failed" s)
        | :? 'T as x -> x
        | _ -> invalidOp (sprintf "dynamic lookup of Xaml component %s failed because the component found was of type %A instead of type %A"  s (s.GetType()) typeof<'T>)
    | _ -> invalidOp (sprintf "dynamic lookup of Xaml component %s failed because the source object was of type %A. It must be a control or a resource dictionary" s (source.GetType()))

This is from Daniel Mohl’s excellent F# for Windows Phone template.

I’ve created a class to basically read the accelerometer and trigger an event when the phone is shaken. The event gets raised as expected but for some reason it’s spawned in a second thread–which leads the CLR to throw an “invalid cross-thread access” exception when the event handler attempts to execute this code. The exception is thrown on the source.FindName(s) call. I can see a second thread of execution–which kind of surprises me because I didn’t specifically spawn a secondary thread. I mean I didn’t explicitly call async or do anything else that I can think of that would cause a secondary thread of execution to start.

So it seems like there are a few approaches I could take:

  1. I could try to figure out why I’m
    spawning a secondary thread when I
    haven’t specifically requested it and modify the code to prevent a secondary thread of
    execution from being spawned.
  2. I could try to modify the (?)
    function to account for multiple
    threads of execution but I’d really
    like to understand why I’m getting a
    second thread started

I think probably the second approach is best but I’d really like to understand what I’m doing that’s causing a secondary thread to spawn. I realize how hard that is to answer without specific code but I don’t mind researching this if someone can point me in the right direction. I believe this has something to do with the Windows 7 Phone platform because the code is, as far as I can tell, pretty much the idiomatic way to bind a Xaml control with F# code.

Any thoughts, comments, suggestions would be greatly appreciated.

Cross-posted to HubFS as well

  • 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-19T08:59:50+00:00Added an answer on May 19, 2026 at 8:59 am

    Event handling in WP7 is generally handled on async callbacks. Accessing the accelerometer is no exception.

    You’ll need to direct any code that results in a UI update to the dispatcher.

    In c# this can be done like

    Dispatcher.BeginInvoke( () => { /* your UI code */ } );
    

    The approach for sending results to the Dispatcher used in this post may also be helpful for you in f# as it’s more of a functional style than imperative as a result of using Rx.

    WP7 Code: Using the Accelerometer API – Dragos Manolescu’s (work) blog

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.