简体   繁体   中英

Silverlight Custom Control in F# DefaultStyleKey

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()

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 (eg named properties for all objects with x:Name ), but otherwise, it should work in the usual way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM