📄 widget.cs
字号:
{ } public virtual bool Render(Surface surface) { IDataSourced ds = this as IDataSourced; if (ds != null && ! ds.IsDataBound) { ds.DataBind(); } surface.Write(GetClientCommand()); return true; } internal void RecurseRender( Widget widget, Surface surface ) { if( widget.ClientEvents != null && widget.VisibleToRoot ) { Widget c = null; while( widget.ClientEvents.Count > 0 ) { object o = widget.ClientEvents.Dequeue(); if( o is Widget ) { c = o as Widget; if( c.VisibleToRoot && ! c.rendered ) { c.Render( surface ); RecurseRender( c, surface ); c.rendered = true; } } else if( o != null ) { string cmd = o.ToString().Replace(clientIdPlaceHolder, widget.UID); surface.Write(cmd); } } } if( widget.Widgets != null ) foreach( Widget child in widget.Widgets ) RecurseRender( child, surface ); } protected void RenderSelfAndChildren(Surface surface) { Render(surface); RecurseRender(this, surface); this.rendered = true; } public virtual void Update() { if( Widgets != null ) foreach( Widget c in Widgets ) c.Update(); } public void SendEvents(Surface surface) { if( widgets != null ) { foreach( Widget c in widgets ) { c.SendEvents(surface); } } if( ClientEvents != null ) { while( ClientEvents.Count > 0 ) { surface.Write( ClientEvents.Dequeue().ToString() ); } } } private const string constructorFormat = "new {0}({1})\r\n"; private const string renderFormat = "{0}.Render({1})\r\n"; public string GetClientCommand() { return GetClientCommand(false); } public string GetClientCommand( bool onlyGetConstructor ) { ClientArguments["id"] = Util.Quotize(ClientId); ClientArguments["baseElem"] = OverrideBaseElement != null ? OverrideBaseElement : this.Parent.BaseElement; if (!this.clientArguments.ContainsKey("parent") && this.Parent.UID != null) { clientArguments["parent"] = Parent.UID; } if( this.className != null ) clientArguments["className"] = Util.Quotize(this.className); if (elementArguments != null && elementArguments.Count > 0) { clientArguments["elemArgs"] = JSON.HashToJSON(elementArguments); } if (styleArguments != null && styleArguments.Count > 0) { clientArguments["styles"] = JSON.HashToJSON(styleArguments); } string idx = position != -1 ? position.ToString() : string.Empty; string ret = string.Format(constructorFormat, ClientClass, JSON.HashToJSON(clientArguments)); if (!onlyGetConstructor) ret += string.Format(renderFormat, ClientId, idx); return ret; } public void SendCommand( string format, params object[] args ) { SendCommand( string.Format( format, args ) ); } public void SendCommand( string cmd ) { if (RootContext == null || RootContext.CometClient == null) { if (cmd == null || cmd.Length == 0) return; if (cmd[cmd.Length - 1] != ';') cmd += ';'; if (ClientEvents == null) ClientEvents = new System.Collections.Queue(); ClientEvents.Enqueue(cmd); } else { try { RootContext.CometClient.Write(cmd.Replace(clientIdPlaceHolder, UID)); } catch { ClientEvents.Enqueue(cmd); } } } public void InvokeClientMethod(string methodName) { InvokeClientMethod(methodName, string.Empty); } public void InvokeClientMethod(string methodName, string args) { SendCommand("{0}.{1}({2});", clientIdPlaceHolder, methodName, args); } public void SetClientElementStyle(string name, string value, bool quotize) { if (quotize) value = Util.ToJavaScriptString(value); SetClientElementStyle(name, value); } public void SetClientElementStyle(string name, string value) { if (rendered) { InvokeClientMethod("SetStyle", string.Format("{{{0}:{1}}}", name, value)); } else { StyleArguments[name] = value; } } public void SetClientElementAttribute(string name, string value, bool quotize) { if (quotize) value = Util.Quotize(value); SetClientElementAttribute(name, value); } public void SetClientElementAttribute(string name, string value) { if (rendered) { InvokeClientMethod("SetElem", string.Format("{{{0}:{1}}}", name, value)); } else { ElementArguments[name] = value; } } public virtual void HandleEvents( string evt, string args ) { switch(evt) { case "OnDelayedMouseOver": if( onDelayedMouseOver != null ) { string[] coords = args.Split(','); int x = int.Parse(coords[0]); int y = int.Parse(coords[1]); onDelayedMouseOver(this, x, y); } break; case "OnDelayedMouseOut": if (onDelayedMouseOut != null) { onDelayedMouseOut(this); } break; case "OnReceiveDrop": if (onReceiveDrop != null) { onReceiveDrop(this, args); } break; case "OnClick": if (onClick != null) { onClick(this, args); } break; } } const string clientIdPlaceHolder = "$ClientId"; public void SetClientAttribute( string Name, object Value ) { if (rendered) SendCommand("SetAttribute({0},'{1}',{2});", clientIdPlaceHolder, Name, Value); else ClientArguments[Name] = Value.ToString(); } public void Center() { Effect("Center"); } public void Effect(string name) { SendCommand("Effect.{0}($('{1}'));", name, clientIdPlaceHolder); } private string foreColor; public string ForeColor { get { return foreColor; } set { foreColor = value; backgroundColor = value; SetClientElementStyle("color", value, true); RaisePropertyChangedNotification("ForeColor"); } } string backgroundColor; virtual public string BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; SetClientElementStyle("backgroundColor", value, true); RaisePropertyChangedNotification("BackgroundColor"); } } float opacity; virtual public float Opacity { get { return opacity; } set { opacity = value; SetClientElementStyle("opacity", value.ToString()); } } virtual public object this[string key] { get { return GetAttribute(key); } set { if (value is string) SetAttribute(key, value as string); else { PropertyInfo pi = this.GetType().GetProperty(key); pi.SetValue(this, value, null); } } } public Type GetFieldTypeFromName(string name) { try { return this.GetType().GetProperty(name).PropertyType; } catch (System.Reflection.AmbiguousMatchException e) { return this.GetType().GetProperty(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).PropertyType; } } public virtual void SetAttribute( string Name, string Value ) { //# is the databind symbol for now. Two #s (##) escapes a #. //what about: //"Title: #Title#" can it somehow => "Title: {0}", if( Value != null && Value.IndexOf('{') > -1 ) { Regex r = new Regex(@"\{\S+?\}"); if( r.IsMatch( Value ) ) { if( DataBoundAttributes == null ) DataBoundAttributes = new Dictionary<string,string>(); DataBoundAttributes[ Name ] = Value; } } //test for an event first Type thisType = this.GetType(); EventInfo ei = thisType.GetEvent(Name); if (ei != null) { MethodInfo handlerMI = null; if( Value.Contains(".") ) { List<string> handlerParts = new List<string>(Value.Split('.')); string methodName = handlerParts[ handlerParts.Count - 1 ]; handlerParts.RemoveAt( handlerParts.Count - 1 ); string handlerTypeName = Util.Join( handlerParts.ToArray(), "." ); Type handlerType = TypeLoader.GetType(handlerTypeName); handlerMI = handlerType.GetMethod( methodName ); //if this is a generic method, let's try to assign it the first generic param of this. if (handlerMI.IsGenericMethod) { handlerMI = handlerMI.MakeGenericMethod(this.GetType().GetGenericArguments()[0]); } ei.AddEventHandler(this, Delegate.CreateDelegate(ei.EventHandlerType, handlerMI)); } else { Context currentRoot = root; while( currentRoot != null ) { MemberInfo[] mis = currentRoot.GetType().FindMembers( MemberTypes.Method, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, Type.FilterName, Value); if (mis == null || mis.Length == 0) { if (currentRoot.parent != null) { currentRoot = currentRoot.parent.root; continue; } else break; } else { handlerMI = mis[0] as MethodInfo; break; } } if( handlerMI != null ) { ei.AddEventHandler(this, Delegate.CreateDelegate(ei.EventHandlerType, currentRoot, Value, true)); } } } else { PropertyInfo pi = null; try { pi = this.GetType().GetProperty(Name); } catch (System.Reflection.AmbiguousMatchException e) { pi = this.GetType().GetProperty(Name, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); //Debug.Trace("ambiguous property: {0} on {1} (message:{2})", Name, this.GetType().Name, e.Message); } if (pi == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -