3

I am getting a XAMLParseException when trying to load a xaml component with a Loaded event. I am not quite sure how write the method signature for the handled event. C# examples are clear but I'm not sure how to convert this to the F# version. I will include my first attempt below.

ViewModel code:

let theWindow = Application.LoadComponent(new Uri("/MyApp;component/MyWindow.xaml", UriKind.Relative)) :?> Window

XAML Header:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:converters="clr-namespace:MyApp.Converters;assembly=MyApp"
    xmlns:local="clr-namespace:MyApp;assembly=MyApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"        
    x:Name="_theWindow"
    WindowStartupLocation="CenterOwner"
    Title="My App - Popup Window"
    Loaded="Window_Loaded"
    Icon="MyApp;component/icons/MyApp.ico"
    Width="484.667" Height="215.333"
    Left="100" Top="100">

F# method:

member x.Window_Loaded (sender : object, eventArgs : RoutedEventArgs) = // Do stuff
  • What's the exception message? – Fyodor Soikin May 06 '16 at 15:56
  • Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll Additional information: 'Failed to create a 'Loaded' from the text 'Window_Loaded'.' Line number '9' and line position '9'. – user6301034 May 06 '16 at 18:33
  • Can you show an equivalent attempt in C# that worked for you? – Fyodor Soikin May 06 '16 at 20:22
  • I'm not using C# I'm just saying above that the examples in C# seem pretty straightforward based on this discussion here: http://stackoverflow.com/questions/12589772/window-loaded-and-wpf – user6301034 May 06 '16 at 22:56
  • Well, one thing that jumps out at me is that you never specify a reference to your class that's supposed to back the window. I can't found it in XAML, neither in the `LoadComponent` call. How do you expect WPF to find your class and create an instance of it? – Fyodor Soikin May 07 '16 at 02:48
  • The view model code is referenced further down in the xaml file. I didn't include that code because the window is displayed normally. I should have been more clear but the window is being launched as a child window from a main view model. The problem came about when I wanted to add a handler for the Loaded event and following the c# example from the link in the earlier comment caused this error and I am not sure how to proceed with it. There's an action I need to perform when the window is launched or finishes launching. – user6301034 May 07 '16 at 03:06
  • Why don't you use FsXAML + FSharp.ViewModule? – FoggyFinder May 07 '16 at 10:27
  • @FyodorSoikin - sorry, I misunderstood your comment earlier. I'm not subclassing the window. I suppose that would be a requirement to handle the Loaded event? – user6301034 May 08 '16 at 01:12
  • Think about it first: how do you expect WPF to find the class in which event handler is defined, and create an instance of that class? It has to be referenced in _some_ way. – Fyodor Soikin May 08 '16 at 01:14
  • Alternatively, you can attach the handler in F# code after loading the window, not in XAML. – Fyodor Soikin May 08 '16 at 01:15
  • To echo @FyodorSoikin said. As `Window_Loaded` is defined in `MyWindow` (I guess) the XAML needs to specify `x:Class`. If you do not WPF looks for `Window_Loaded` in the `Window` class. But no such method exists in that class. – Just another metaprogrammer May 08 '16 at 05:22

1 Answers1

0

When using FsXaml.Wpf v.3.1.3 I had...

StartupUri="MainWindow.xaml"

...in my app.xaml file. I removed it and used this in my App.fs file to open MainWindow.xaml...

[<STAThread>]
[<EntryPoint>]
let main argv =
    Wpf.installSynchronizationContext ()
    Views.MainWindow()
    |> App().Run 

...which matches the WpfMvvmAgent demo. That resolved the XamlParseException above for me.