























Silverlight
If the target managed property or input parameter is strongly typed (that is, not typed as an object), Silverlight attempts to convert the JavaScript object to the corresponding .NET Framework object. If the conversion fails (because of mismatched types or overflows), an exception is returned to the JavaScript caller.
Type conversions are described in the following sections:
Note: |
Silverlight for Windows Phone does not support the HTML Bridge feature. |
Primitive Types
Primitive JavaScript types are converted to their best-match counterparts in the .NET Framework. In the list below, an asterisk (*) indicates that this rule also applies to return values from the GetProperty, Invoke, and InvokeSelf methods.
.NET Framework properties or input parameters that are structures are marshaled by value from a JavaScript object. Only public properties and fields on the target structure are candidates for matching JavaScript properties. If the structure does not have a corresponding public property or field for a JavaScript property, the JavaScript property value is silently ignored. You still have to use createManagedObject or createObject to marshal the JavaScript object to a managed structure. Structures are marshaled between managed code and JavaScript as follows:
Well-Known Types
Silverlight handles the following well-known JavaScript objects as special cases and returns them as the appropriate HTML Bridge object reference instead:
Complex Types
The following table provides conversion information for complex types.
JavaScript variable | Target .NET Framework type | Result |
JavaScript array/dictionary | .NET Framework array/list type on a [ScriptableMember] property or parameter on a [ScriptableMember] method. | You must wrap the JavaScript array/dictionary with a call to the create methods and related helper methods to convert it to a managed type. |
JavaScript array/dictionary | Object on a [ScriptableMember] property or parameter on a [ScriptableMember] method. | You must wrap the JavaScript array/dictionary with a call to the create methods and related helper methods to convert it to a managed type. |
JavaScript array/dictionary | ScriptObject . This could be a return value or used as the type for a [ScriptableMember] property or parameter on a [ScriptableMember] method. | A ScriptObject reference for which ManagedObject is null. |
Array/dictionary interop wrapper | .NET Framework array/list type on a [ScriptableMember] property or parameter on a [ScriptableMember] method. | A reference to the concrete underlying .NET Framework type, assuming the type exists in the current application domain. |
Array/dictionary interop wrapper | Object on a [ScriptableMember] property or parameter on a [ScriptableMember] method. | A reference to the concrete underlying .NET Framework type, assuming the type exists in the current application domain. You must explicitly cast to get to the underlying type. |
Array/dictionary interop wrapper | ScriptObject . This could be a return value, or used as the type for a [ScriptableMember] property or parameter on a [ScriptableMember] method. | A ScriptObject reference on which ManagedObject is non-null. You can cast the ManagedObject property to get to the underlying type, assuming the type exists in the current application domain. |
Array and List Types
The default is explicit by-value marshaling from JavaScript to managed code. Automatic JSON serialization to managed input parameters is not supported. Instead, you must explicitly use creatable types or the create helper methods.
By-Reference Marshaling
You can make a JavaScript array available by reference to managed code in two ways:
By-Value Marshaling
If you want to pass a JavaScript array by value to managed code, the target .NET Framework property or input parameter must be typed as something other than ScriptObject. You must then use one of the create or createObject helper methods to explicitly force a conversion from the JavaScript array to a .NET Framework list or array type. This has the effect of disconnecting the JavaScript representation of the data from the .NET Framework representation.
The JavaScript array will be recursively marshaled by value onto the target .NET Framework type. Silverlight will use the JSON serializer to deserialize from a JavaScript array to a conforming .NET Framework array or list type.
Dictionaries and Custom Types
The default is explicit by-value marshaling from JavaScript to managed code. Automatic JSON serialization to managed input parameters is not supported. Instead, you must explicitly use the create or createObject helper methods.
By-Reference Marshaling
You can make a JavaScript dictionary available by reference to managed code by using one of the following two methods:
By-Value Marshaling
If you want to pass a JavaScript dictionary by value to managed code, the target .NET Framework property or input parameter must be typed as something other than ScriptObject. You must then use one of the create or createObject helper methods to explicitly force a conversion from the JavaScript dictionary to a .NET Framework custom type or dictionary type. This has the effect of disconnecting the JavaScript representation of the data from the .NET Framework representation.
The JavaScript dictionary will be recursively marshaled by value onto the target .NET Framework type. Silverlight will use the JSON serializer to deserialize from a JavaScript dictionary onto a conforming .NET Framework custom type or dictionary.
从 <http://msdn.microsoft.com/zh-cn/library/cc645079(v=vs.95).aspx> 插入
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。