Services are reusable components that do not require a user interface. In Silverlight
Composite Application service is any class. For public accessibility it can be registered
in Application.Resources. In most cases modules are responsible for service registration.
To create a service
To create a service and register it in Application.Resources you can:
- Specify the required service in App.xaml of your application.
<Application.Resources>
<scabsvc:ScriptManager x:Key="ScabScriptManagerService" />
<scabsvc:Trace x:Key="ScabTraceService"/>
</Application.Resources>
- Or create and register it from code like this:
ScabApplication app = ScabApplication.Current;
if (!app.Resources.Contains("ScabScriptManagerService"))
app.Resources.Add("ScabScriptManagerService", new ScriptManager());
Getting references to services
To access services from code you should use the following code template:
if (ScabApplication.Current.Resources.Contains("ScabScriptManagerService"))
ScriptManager sm = (ScriptManager) ScabApplication.Current.Resources["ScabScriptManagerService"];
Registering services to access from JavaScript code
Silverlight supports objects to be accessible from JavaScript. Services can use this
possibility to publish their functionality.
- Mark your service with [ScriptableType] attribute.
- Mark your service members with [ScriptableMember] attribute.
- Register your service object for scriptable access by JavaScript code using
HtmlPage.RegisterScriptableObject
method.
Example
Services.zip (70.67 kb)
The following example contains Service module assembly and CompositeApplication
application. Service assembly contains Service service class and ServiceModule module
class declaration. Service class declares one ShowMessage module. This method shows
message box with the specified string parameter. Service class and its method are
marked with scriptable attributes to allow access from JavaScript. ServiceModule
registers Service instance both in Application.Resources and calling HtmlPage.RegisterScriptableObject static method.
CompositeApplication specifies ServiceModule to load at App.xaml file. After that
Page.xaml.cs gets reference to service and calls ShowMessage method from managed
code and emulating JavaScript call.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5