Bambu
Bambu

Reputation: 548

Calling static methods in a static class via XAML

Searched around and could not find a response for this. Does anybody know of a way to call a static method from a static class within XAML. I know that it is possible to call a static method from a regular class using ObjectDataProvider by doing something like:

<ObjectDataProvider x:Key="mthd" ObjectType="{x:Type l:MyClass}" MethodName="MyStaticMethod">
    <ObjectDataProvider.MethodParameters>
        <sys:String>Test</sys:String>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

I have tried this with the static class and it fails. Since the static class cannot be instantiated with the exception of "Object reference not set to an instance of an object".

Can something similar to this be done for static classes in .Net 4.0? Thanks in advance!

Upvotes: 2

Views: 3228

Answers (1)

brunnerh
brunnerh

Reputation: 184516

Creating something like an ObjectDataProvide is really simple, you just need to use a bit of reflection. Get the class type via a Type property right from XAML along with the method name and parameters, then use GetMethod with the right BindingFlags and invoke it with the passed parameters.

Upvotes: 1

Related Questions