Question

I wanted to try and port a C# application of mine to F# in order to more fully take advantage of Type Providers. I wrote the Code behind as an F# file and referenced the XAML window using the XAML typeprovider in fsharpx. When I run the application the window displays, but doesn't accept any input. I can't even get a flashing cursor in the inputboxes or get the buttons to display their click animations so it definitely deeper than a simple unwired event handler:

XAML:

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" SizeToContent="WidthAndHeight" MinHeight="300" MinWidth="500" IsHitTestVisible="False" Icon="{DynamicResource Icon}">
    <Window.Resources>
        <BitmapImage x:Key="Icon" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" UriSource="../W.ico"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="325*" />
        </Grid.ColumnDefinitions>
        <GroupBox HorizontalAlignment="Left" Margin="12,15,0,45" Name="groupBox1" Width="144">
            <Grid>
                <TextBox HorizontalAlignment="Left" Margin="6,6,0,0" Name="txtSerial" Width="120" Grid.ColumnSpan="2" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Grid.RowSpan="2" VerticalContentAlignment="Stretch" />
            </Grid>
        </GroupBox>
        <DataGrid Name="dgResults" AutoGenerateColumns="True" Margin="5,76,5,45"   HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Column="1" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">
        </DataGrid>
        <Button Grid.Column ="1" Content="Do Stuff" Height="23" HorizontalAlignment="Left" Margin="5,12,0,0" Name="btnQuery" VerticalAlignment="Top" Width="75"/>
        <Button Grid.Column ="1" Content="Find Stuff" Height="23" Width="75" Margin="5,38,230.4,0" Name="btnScout" VerticalAlignment="Top" />
        <TextBox Grid.Column ="1" Height="23" Margin="5,38,5,0" Name="txtTarget" VerticalAlignment="Top"  Width="100" />
    </Grid>
</Window>

F#

let Fetch(e: RoutedEventArgs) =
    dataTable.Rows.Clear()
    ...

let loadWindow() =
    let window = MainWindow()   
    window.btnQuery.Click.Add(Fetch)
    window.Root

[<EntryPoint; STAThread>]
let main args =
(new Application()).Run(loadWindow()) |> ignore  
0

Any ideas on what could be going wrong?

Was it helpful?

Solution 2

Issue was as stupid as it was simple - at some point while importing the window this flag activated on the MainWindow object - IsHitTestVisible="False". This turned off the ability to interact with the form.

Thank you all for your help on this issue.

OTHER TIPS

I think @JohnPalmer is right in the comment. I'm not entirely sure where the attribute ends up in your code (I have not checked), but I'm pretty sure that the compiler does not automatically lift it to the main function (I'm surprised this code even compiles.)

You need to put the code into a function and add STAThread annotation to that function.

[<EntryPoint; STAThread>]
let main args =
    (new Application()).Run(loadWindow()) |> ignore
    // Return 0. This indicates success.
    0

See also entry point (F#) on MSDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top