views:

35

answers:

1

Wondering how to accomplish setting the Style xaml with the code in F#. The code is simple enough:

this.DefaultStyleKey <- typeof<MyControl>

In a C# project the build options allow you to mark the XAML as a resource custom build command of: MSBuild:Compile

I don't see it in the properties panel, so I tried to add it by hand to the project file myself...

Any ideas? The application loads - the custom control has no output (but the code executes).

Thanks

UPDATE:

I checked the manifests and the resource was included as expected between my project and the project I am porting... Looking for a next step.

UPDATE 2:

Well it may be included in the manifest OK - but it is not being "compiled" as the C# version of the project throws an error in the build process when I malform the XML while the F# version allows the malformed XML to be brought into the application.

UPDATE 3:

Loading the XAML is fine now (i guess) however I am having some issues with the properties of the control:

 static member ItemsProperty : DependencyProperty = 
        DependencyProperty.Register(
            "Items",
            typeof<MyMenuItemCollection>,
            typeof<MyMenu>,
            null);

 member this.Items
        with get () : MyMenuItemCollection = this.GetValue(MyMenu.ItemsProperty) :?> MyMenuItemCollection
        and set (value: MyMenuItemCollection) = this.SetValue(MyMenu.ItemsProperty, value);

The problem occurs on access:

for menuItem in this.Items do
    let contentElement: FrameworkElement = menuItem.Content

where I get a null pointer exception on this.Items; however I have it initialized in the constructor:

do
    this.Items <- new CoolMenuItemCollection()
A: 

The C# style of compilation of XAML files is not supported by the F# tools for Visual Studio, so there is no way to get the same behavior as in C#. I think you have two options:

  • Create a C# project with XAML files and reference F# library which implements the core functionality (or reference C# library from F# and load user interface from the C# library in your F# application)

  • Use XamlReader object (see MSDN) and load the XAML file (embedded in resources in the simple way) programmatically. You won't get any of the C#-compiler generated features (e.g. named properties for all objects with x:Name), but otherwise, it should work in the usual way.

Tomas Petricek
Is this the same XAML reader I used for the main page? I have a basic app running 100% f# silverlight 3.0. I am stuck implmenting the cusom control.On the app I compile the XAML and load it in my implementation of th Application object. Easy enough everything works;The custom control example I am porting (a menu component with dancing icons) contains an XAML that defines the basic style of the control. protected Object DefaultStyleKey from what I understand (and it is very little) the property takes a string which represents the "compiled" XAML for the default style XAML.
akaphenom
Perhaps using the style property of the control. I can embed the XAML as a resource and load like i did for the application object.
akaphenom