Javascript Observable collection count
I'm, accessing Silverlight ObservableCollection count in javascript, but I get the following error.,
Microsoft JScript runtime error: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provide开发者_JS百科r)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Hosting.ScriptingInterface.GetScriptParamValueForType(ScriptParam scriptParam, Type desiredType)
at System.Windows.Hosting.ScriptingInterface.ConvertFromScriptParams(Type[] desiredTypes, ScriptParam[] args)
at System.Windows.Browser.ManagedObjectInfo.ListIndexerMember.Invoke(ManagedObject obj, InvokeType invokeType, ScriptParam[] args)
at System.Windows.Browser.ManagedObjectInfo.Invoke(ManagedObject obj, InvokeType invokeType, String memberName, ScriptParam[] args)
at System.Windows.Hosting.ManagedHost.InvokeScriptableMember(IntPtr pHandle, Int32 nMemberID, Int32 nInvokeType, Int32 nArgCount, ScriptParam[] pArgs, ScriptParam& pResult, ExceptionInfo& pExcepInfo)
I'm using following code, where children is a observablecollection of custom object.,
reg.OnDropping = function (sender, args) {
if (args.toItem.Children.Count > 0) {
args.cancel = true;
}
else {
args.cancel = false;
}
}
Is there any other way to access the count in Javascript?
Regards, Karthik
In order to access a property of an object from Javascript either the property needs to be marked with the ScriptableMember
attribute or the class to which it belongs is marked as ScriptableType
. Neither of these is true of the ObservableCollection<T>
class.
A pragmatic solution would be to add a HasChildren
property to your Custom object:-
[ScriptableMember]
public bool HasChildren
{
get { return Children.Count > 0; }
}
Have you made that observablecollection ScriptableMember? You need to add this attribute to make in available in javascript. Check the following link for more information:
Walkthrough: Calling Managed Code from JavaScript
HtmlPage.RegisterScriptableObject Method
I hope it helps.
精彩评论