private IEnumerable <XamlNode> ReadMemberNode(XamlDomMember memberNode) { if (memberNode.Items != null && memberNode.Items.Count > 0) { yield return(XamlNode.GetStartMember(memberNode)); foreach (var itemNode in memberNode.Items) { var objectNode = itemNode as XamlDomObject; IEnumerable <XamlNode> enumerable; if (objectNode != null) { enumerable = ReadObjectNode(objectNode); } else { enumerable = ReadValueNode(itemNode as XamlDomValue); } foreach (var node in enumerable) { yield return(node); } } yield return(XamlNode.GetEndMember(memberNode)); } }
internal XamlDomObject Process() { _rootNode.Namespaces.Add(new XamlDomNamespace(new NamespaceDeclaration(XamlLanguage.Xaml2006Namespace, "x"))); //TODO: We need to create "unknown" nodes for Content properties since Silverlight 3 doesn't support // direct content. I think that if we spew out unknown members with the right values, it'll right // things as attributes instead of content. foreach (var contentMember in from objectNode in _rootNode.DescendantsAndSelf() where objectNode.Type.ContentProperty != null where objectNode.HasMember(objectNode.Type.ContentProperty) select objectNode.GetMemberNode(objectNode.Type.ContentProperty)) { // Only special case things that have one child that is a value node if (contentMember.Items.Count == 1) { var valueNode = contentMember.Item as XamlDomValue; if (valueNode != null) { var fakeMember = new NonContentPropertyXamlMember(contentMember.Member); var newMemberNode = new XamlDomMember(fakeMember, valueNode); var parentObject = contentMember.Parent; parentObject.RemoveMemberNode(contentMember); parentObject.MemberNodes.Add(newMemberNode); } } } return _rootNode; }
public override void WriteStartMember(XamlMember property) { var propertyNode = new XamlDomMember(property); // Only need to set the SchemaContext if it's a XamlDirective if (property.IsDirective) { propertyNode.SchemaContext = SchemaContext; } if (RootNode != null) { var objectNode = (XamlDomObject)writerStack.Peek(); objectNode.MemberNodes.Add(propertyNode); } else { RootNode = propertyNode; } propertyNode.StartLineNumber = _lineNumber; propertyNode.StartLinePosition = _linePosition; writerStack.Push(propertyNode); }
internal static XamlNode GetEndMember(XamlDomMember memberNode) { _xamlNode.Clear(); _xamlNode.NodeType = XamlNodeType.EndMember; _xamlNode.LineNumber = memberNode.EndLineNumber; _xamlNode.LinePosition = memberNode.EndLinePosition; return(_xamlNode); }
private bool IsAttribute(XamlDomMember memberNode) { if (memberNode.Items.Count == 1) { if (memberNode.Item is XamlDomValue) { return(true); } var objectType = (memberNode.Item as XamlDomObject).Type; if (objectType != null && objectType.IsMarkupExtension) { return(true); } } return(false); }
public virtual void SetMemberValue(XamlMember xamlMember, object value) { if (xamlMember == null) { throw new ArgumentNullException("xamlMember"); } var node = GetMemberNode(xamlMember); if (node == null) { node = new XamlDomMember(xamlMember); MemberNodes.Add(node); } node.Item = new XamlDomValue(value); }
public virtual XamlDomMember RemoveMemberNode(XamlDomMember node) { if (node == null) { throw new ArgumentNullException("node"); } if (_memberNodes != null) { if (_memberNodes.Remove(node)) { return(node); } } return(null); }
internal static new List<XamlDomObject> CreateObject(XElement tag, XamlSchemaContext schema) { var domObjects = new List<XamlDomObject>(); var listBox = new XamlDomObject(schema.GetXamlType(typeof(ListBox))); var childrenMember = new XamlDomMember(listBox.Type.ContentProperty); listBox.MemberNodes.Add(childrenMember); var titles = GetElementWithMatchingAttribute(tag, "key", "IBSegmentTitles"); Debug.Assert(titles != null); foreach (var title in titles.Elements(XName.Get("string"))) { childrenMember.Items.Add(new XamlDomObject(schema.GetXamlType(typeof(ListBoxItem)), new XamlDomMember(XamlLanguage.Initialization, title.Value))); } SetCommonProperties(tag, listBox); domObjects.Add(listBox); return domObjects; }
private IEnumerable<XamlNode> ReadMemberNode(XamlDomMember memberNode) { if (memberNode.Items != null && memberNode.Items.Count > 0) { yield return XamlNode.GetStartMember(memberNode); foreach (var itemNode in memberNode.Items) { var objectNode = itemNode as XamlDomObject; IEnumerable<XamlNode> enumerable; if (objectNode != null) { enumerable = ReadObjectNode(objectNode); } else { enumerable = ReadValueNode(itemNode as XamlDomValue); } foreach (var node in enumerable) { yield return node; } } yield return XamlNode.GetEndMember(memberNode); } }
private bool IsAttribute(XamlDomMember memberNode) { if (memberNode.Items.Count == 1) { if (memberNode.Item is XamlDomValue) { return true; } var objectType = (memberNode.Item as XamlDomObject).Type; if (objectType != null && objectType.IsMarkupExtension) { return true; } } return false; }
private static XamlDomObject CreateView(XElement viewTag, XamlSchemaContext schemaContext) { Debug.Assert(viewTag != null); // Create the view and canvas var canvas = new XamlDomObject(schemaContext.GetXamlType(typeof(Canvas))); var canvasChildrenMember = new XamlDomMember(schemaContext.GetXamlType(typeof(Canvas)).ContentProperty); SetFrameSize(viewTag, canvas); var backgroundColor = GetElementWithMatchingAttribute(viewTag, "key", "IBUIBackgroundColor"); //GetColor(canvas.Type, canvas, backgroundColor); // Add children var subViews = GetElementWithMatchingAttribute(viewTag, "key", "NSSubviews"); if (subViews != null) { foreach (var element in subViews.Elements(XName.Get("object"))) { AddControlToTree(canvasChildrenMember, element); } } // TODO: Fix XamlToolkit to not choke on members with nothing inside of it if (canvasChildrenMember.Items != null && canvasChildrenMember.Items.Count > 0) { canvas.MemberNodes.Add(canvasChildrenMember); } return canvas; }
internal static void AddControlToTree(XamlDomMember member, XElement element) { var controlName = element.Attribute(XName.Get("class")).Value; List<XamlDomObject> objects; switch (controlName) { //TODO: Figure out if this is correct for SL Mobile. Do they have the toolkit. Should we use label case "IBUILabel": objects = LabelHandler.CreateObject(element, member.SchemaContext); break; case "IBUIButton": objects = ButtonHandler.CreateObject(element, member.SchemaContext); break; case "IBUITextField": objects = TextFieldHandler.CreateObject(element, member.SchemaContext); break; case "IBUITextView": objects = TextViewHandler.CreateObject(element, member.SchemaContext); break; case "IBUIImageView": objects = ImageViewHandler.CreateObject(element, member.SchemaContext); break; case "IBUIWebView": objects = WebViewHandler.CreateObject(element, member.SchemaContext); break; case "IBUISegmentedControl": objects = SegmentedControlHandler.CreateObject(element, member.SchemaContext); break; case "IBUISlider": objects = SliderHandler.CreateObject(element, member.SchemaContext); break; case "IBUIScrollView": objects = ScrollViewHandler.CreateObject(element, member.SchemaContext); break; case "IBUIView": objects = CreateObject(element, member.SchemaContext); break; case "IBUITableView": objects = TableViewHandler.CreateObject(element, member.SchemaContext); break; case "IBUITableViewCell": objects = TableViewCellHandler.CreateObject(element, member.SchemaContext); break; case "IBUIActivityIndicatorView": objects = ActivityIndicatorViewHandler.CreateObject(element, member.SchemaContext); break; case "IBUIBarButtonItem": objects = BarButtonHandler.CreateObject(element, member.SchemaContext); break; case "IBUISwitch": objects = SwitchHandler.CreateObject(element, member.SchemaContext); break; //case "IBUINavigationBar": objects = LabelHandler.CreateObject(element, member.SchemaContext); break; //case "IBUINavigationBar": return typeof(UserControl); //case "IBUITableView": return typeof(UserControl); //case "IBUITableViewCell": return typeof(UserControl); //case "IBUITextField": return typeof(TextBox); //case "IBUIImageView": return typeof(Image); //case "IBUITextView": return typeof(TextBox); //case "IBUIProgressView": return typeof(ProgressBar); //case "IBUIWebView": return typeof(Microsoft.Phone.Controls.WebBrowser); ////TODO: FIgure out the correct thing for UIWebView. Is there one in SL Mobile? //case "IBUISegmentedControl": return typeof(RadioButton); //case "IBUIView": return typeof(UserControl); //case "IBUIPageControl": return typeof(UserControl); //case "IBUITableView": return typeof(UserControl); //case "IBUITableViewCell": return typeof(UserControl); //case "IBUIScrollView": return typeof(ScrollViewer); //case "IBUIActivityIndicatorView": return typeof(UserControl); //case "IBUIPickerView": return typeof(UserControl); //case "IBUIDatePicker": return typeof(UserControl); //case "IBUINavigationBar": return typeof(UserControl); //case "IBUISearchBar": return typeof(UserControl); //case "IBUITabBar": return typeof(UserControl); //case "IBUIToolbar": return typeof(ToolBar); default: throw new NotImplementedException("Unknown control type: " + controlName); } Debug.Assert(objects != null); foreach (var domObject in objects) { member.Items.Add(domObject); } //// Find control type and create a node //XamlType controlType = member.SchemaContext.GetXamlType(XamlLegacyXibReader.GetControlType(controlName)); //XamlDomObject domObject = new XamlDomObject(controlType); //member.Items.Add(domObject); //GetDimensions(element, domObject); //if (controlType.UnderlyingType == typeof(TextBox)) //{ // domObject.SetMemberValue("TextWrapping", "Wrap"); //} //var colors = from nsColor in element.Elements(XName.Get("object")) // where nsColor.Attribute(XName.Get("class")).Value == "NSColor" // select nsColor; //foreach (var color in colors) //{ // GetColor(controlType, domObject, color); //} //GetNormalTitle(element, domObject); //GetFont(element, controlType, domObject); //GetOpacity(element, controlType, domObject); //GetText(element, domObject); //GetImage(element, domObject); }
protected static void AddChildren(XElement tag, XamlMember contentMember, XamlDomObject domObject) { var contentMemberNode = new XamlDomMember(contentMember); // Add children var subViews = GetElementWithMatchingAttribute(tag, "key", "NSSubviews"); if (subViews != null) { foreach (var element in subViews.Elements(XName.Get("object"))) { AddControlToTree(contentMemberNode, element); } } domObject.MemberNodes.Add(contentMemberNode); }
private XamlDomObject CreateXamlDom(string file) { var schemaContext = new XamlSchemaContext(); _document = XDocument.Load(file); if (_document?.Root == null) { throw new NotSupportedException("Failed to find valid XAML document: file"); } var rootDomObject = new XamlDomObject(schemaContext.GetXamlType(typeof(Page))); rootDomObject.MemberNodes.Insert(0, new XamlDomMember(XamlLanguage.Class, $"IslandwoodAutoGenNamespace.{Path.GetFileNameWithoutExtension(file)}")); var canvas = new XamlDomObject(schemaContext.GetXamlType(typeof(Canvas))); rootDomObject.MemberNodes.Add(new XamlDomMember(schemaContext.GetXamlType(typeof(Page)).ContentProperty, canvas)); var canvasChildrenMember = new XamlDomMember(schemaContext.GetXamlType(typeof(Canvas)).ContentProperty); canvas.MemberNodes.Add(canvasChildrenMember); // TODO: This may no longer be needed but it's referenced in certain control handlers so more investigation required /* rootDomObject.MemberNodes.Add( new XamlDomMember(rootDomObject.Type.GetMember("RenderTransform"), new XamlDomObject(typeof(ScaleTransform), new XamlDomMember("CenterX", (IPHONE_WIDTH/2).ToString()), new XamlDomMember("CenterY", (IPHONE_HEIGHT/2).ToString()), new XamlDomMember("ScaleX", WidthMultiplier.ToString(CultureInfo.InvariantCulture)), new XamlDomMember("ScaleY", HeightMultiplier.ToString(CultureInfo.InvariantCulture))))); */ var rootObjectsTag = _document.Root.Element(XName.Get("objects")); if (rootObjectsTag == null) { throw new NotSupportedException("Failed to find XML node: objects"); } var placeholderTag = GetElementWithMatchingAttribute(rootObjectsTag, "placeholderIdentifier", "IBFilesOwner"); if (placeholderTag == null) { throw new NotSupportedException("Failed to find XML node with required attribute: placeholderIdentifier"); } // Loop over outlets vwhere property attribute is a view foreach (var outletElement in from outletNode in placeholderTag.Descendants(XName.Get("outlet")) let propertyAttr = outletNode.Attribute(XName.Get("property")) where propertyAttr != null && propertyAttr.Value == "view" select outletNode) { // Find the node with the matching ID var destinationAttr = outletElement.Attribute(XName.Get("destination")); if (destinationAttr == null) { throw new NotSupportedException("Failed to find XML outlet node with required attribute: destination"); } // TODO: Handle the possibility that subviews don't exist var viewTag = GetElementWithMatchingAttribute(rootObjectsTag, "id", destinationAttr.Value); var subviewsTag = viewTag.Element(XName.Get("subviews")); if (subviewsTag != null) { foreach (var subviewTag in from objectTag in subviewsTag.Elements() let attriNode = objectTag.Attribute(XName.Get("id")) where attriNode != null select objectTag) { // Build the tree ViewHandler.AddControlToTree(canvasChildrenMember, subviewTag); } } } // Second pass over the outlets to insert x:Name and x:Bind against added controls WireNamesAndEvents(placeholderTag); return rootDomObject; }
internal static void AddControlToTree(XamlDomMember member, XElement element) { List<XamlDomObject> objects; var controlName = element.Name.LocalName; switch (controlName) { case "label": objects = LabelHandler.CreateObject(element, member.SchemaContext); break; case "button": objects = ButtonHandler.CreateObject(element, member.SchemaContext); break; case "textField": objects = TextFieldHandler.CreateObject(element, member.SchemaContext); break; case "textView": objects = TextViewHandler.CreateObject(element, member.SchemaContext); break; case "imageView": objects = ImageViewHandler.CreateObject(element, member.SchemaContext); break; case "webView": objects = WebViewHandler.CreateObject(element, member.SchemaContext); break; case "segmentedControl": objects = SegmentedControlHandler.CreateObject(element, member.SchemaContext); break; case "slider": objects = SliderHandler.CreateObject(element, member.SchemaContext); break; case "scrollView": objects = ScrollViewHandler.CreateObject(element, member.SchemaContext); break; case "view": objects = CreateObject(element, member.SchemaContext); break; case "tableView": objects = TableViewHandler.CreateObject(element, member.SchemaContext); break; case "tableViewCell": objects = TableViewCellHandler.CreateObject(element, member.SchemaContext); break; case "activityIndicatorView": objects = ActivityIndicatorViewHandler.CreateObject(element, member.SchemaContext); break; case "barButtonItem": objects = BarButtonHandler.CreateObject(element, member.SchemaContext); break; case "switch": objects = SwitchHandler.CreateObject(element, member.SchemaContext); break; //case "IBUINavigationBar": return typeof(UserControl); //case "IBUIProgressView": return typeof(ProgressBar); //case "IBUIPageControl": return typeof(UserControl); //case "IBUIPickerView": return typeof(UserControl); //case "IBUIDatePicker": return typeof(UserControl); //case "IBUISearchBar": return typeof(UserControl); //case "IBUITabBar": return typeof(UserControl); //case "IBUIToolbar": return typeof(ToolBar); default: throw new NotImplementedException("Unknown control type: " + controlName); } Debug.Assert(objects != null); foreach (var domObject in objects) { member.Items.Add(domObject); } }
internal static XamlNode GetStartMember(XamlDomMember memberNode) { _xamlNode.Clear(); _xamlNode.NodeType = XamlNodeType.StartMember; _xamlNode.Member = memberNode.Member; _xamlNode.LineNumber = memberNode.StartLineNumber; _xamlNode.LinePosition = memberNode.StartLinePosition; return _xamlNode; }
private XamlDomObject CreateXamlDom(XDocument xDocument) { _document = xDocument; var schemaContext = new XamlSchemaContext(); var rootObjectsTag = GetElementWithMatchingAttribute(xDocument.Root.Element(XName.Get("data")), "key", "IBDocument.RootObjects"); var windowTag = GetElementWithMatchingAttribute(rootObjectsTag, "class", "IBUIWindow"); XamlDomObject rootDomObject; if (windowTag != null) { rootDomObject = ViewHandler.CreateObject(windowTag, schemaContext)[0]; } else // No window so there's one or more views { rootDomObject = new XamlDomObject(schemaContext.GetXamlType(typeof(UserControl))); var canvas = new XamlDomObject(schemaContext.GetXamlType(typeof(Canvas))); // TODO: We should make the DOM handle string based lookup of content here correctly. Null refs currently rootDomObject.MemberNodes.Add( new XamlDomMember(schemaContext.GetXamlType(typeof(UserControl)).ContentProperty, canvas)); var canvasChildrenMember = new XamlDomMember(schemaContext.GetXamlType(typeof(Canvas)).ContentProperty); canvas.MemberNodes.Add(canvasChildrenMember); foreach (var viewTag in from objectTag in rootObjectsTag.Elements() let attriNode = objectTag.Attribute(XName.Get("class")) where attriNode != null select objectTag) { if (viewTag.Attribute(XName.Get("class")).Value == "IBProxyObject" || viewTag.Attribute(XName.Get("class")).Value == "IBUICustomObject") { continue; } ViewHandler.AddControlToTree(canvasChildrenMember, viewTag); } } // Scale to make it the right dimensions rootDomObject.MemberNodes.Add( new XamlDomMember(rootDomObject.Type.GetMember("RenderTransform"), new XamlDomObject(typeof(ScaleTransform), new XamlDomMember("CenterX", (IPHONE_WIDTH/2).ToString()), new XamlDomMember("CenterY", (IPHONE_HEIGHT/2).ToString()), new XamlDomMember("ScaleX", WIDTHMULTIPLIER.ToString()), new XamlDomMember("ScaleY", HEIGHTMULTIPLIER.ToString())))); WireNamesAndEvents(rootDomObject, xDocument); return rootDomObject; }
internal static new void SetCommonProperties(XElement tag, XamlDomObject domObject) { ViewHandler.SetCommonProperties(tag, domObject); // Find the normal state which translates into the default attributes on a XAML button var state = GetElementWithMatchingAttribute(tag, "key", "normal"); if (state != null) { GetAndSetValue(state, domObject, "title", domObject.Type.ContentProperty); var normalColor = GetElementWithMatchingAttribute(state, "key", "titleColor"); if (normalColor != null) { SetColor(domObject, normalColor, domObject.Type.GetMember("Foreground")); } } var normalTitle = GetValueWithMatchingAttribute(tag, "key", "normalTitle"); var highlightedTitle = GetValueWithMatchingAttribute(tag, "key", "highlightedTitle"); var disabledTitle = GetValueWithMatchingAttribute(tag, "key", "disabledTitle"); var selectedTitle = GetValueWithMatchingAttribute(tag, "key", "selectedTitle"); if (highlightedTitle != normalTitle && !string.IsNullOrEmpty(highlightedTitle)) { throw new NotImplementedException(); } if (disabledTitle != normalTitle && !string.IsNullOrEmpty(disabledTitle)) { throw new NotImplementedException(); } if (selectedTitle != normalTitle && !string.IsNullOrEmpty(selectedTitle)) { throw new NotImplementedException(); } string normalImagePath = null; string selectedImagePath = null; string disabledImagePath = null; string highlightedImagePath = null; // Check for images var normalImage = GetElementWithMatchingAttribute(tag, "key", "normalImage"); if (normalImage != null) { Debug.Assert("NSImage" == GetElementWithMatchingAttribute(normalImage, "key", "NSClassName").Value); normalImagePath = GetElementWithMatchingAttribute(normalImage, "key", "NSResourceName").Value; } var selectedImage = GetElementWithMatchingAttribute(tag, "key", "selectedImage"); if (selectedImage != null) { Debug.Assert("NSImage" == GetElementWithMatchingAttribute(normalImage, "key", "NSClassName").Value); selectedImagePath = GetElementWithMatchingAttribute(normalImage, "key", "NSResourceName").Value; } var disabledImage = GetElementWithMatchingAttribute(tag, "key", "selectedImage"); if (disabledImage != null) { Debug.Assert("NSImage" == GetElementWithMatchingAttribute(normalImage, "key", "NSClassName").Value); disabledImagePath = GetElementWithMatchingAttribute(normalImage, "key", "NSResourceName").Value; } var highlightedImage = GetElementWithMatchingAttribute(tag, "key", "selectedImage"); if (highlightedImage != null) { Debug.Assert("NSImage" == GetElementWithMatchingAttribute(normalImage, "key", "NSClassName").Value); highlightedImagePath = GetElementWithMatchingAttribute(normalImage, "key", "NSResourceName").Value; } if (highlightedImagePath != normalImagePath) { throw new NotImplementedException(); } if (disabledImagePath != normalImagePath) { throw new NotImplementedException(); } if (selectedImagePath != normalImagePath) { throw new NotImplementedException(); } Debug.Assert(string.IsNullOrEmpty(normalImagePath) || string.IsNullOrEmpty(normalTitle)); if (normalImagePath != null) { var controlTemplateObject = new XamlDomObject(domObject.SchemaContext.GetXamlType(typeof(ControlTemplate)), new XamlDomMember("TargetType", "Button"), new XamlDomMember(domObject.SchemaContext.GetXamlType(typeof(ControlTemplate)).ContentProperty, new XamlDomObject(typeof(Image), new XamlDomMember("Source", normalImagePath) ) ) ); var sourceMember = new XamlDomMember(domObject.Type.GetMember("Template")); sourceMember.Items.Add(controlTemplateObject); domObject.MemberNodes.Add(sourceMember); } }
public override void WriteStartMember(XamlMember property) { var propertyNode = new XamlDomMember(property); // Only need to set the SchemaContext if it's a XamlDirective if (property.IsDirective) { propertyNode.SchemaContext = SchemaContext; } if (RootNode != null) { var objectNode = (XamlDomObject) writerStack.Peek(); objectNode.MemberNodes.Add(propertyNode); } else { RootNode = propertyNode; } propertyNode.StartLineNumber = _lineNumber; propertyNode.StartLinePosition = _linePosition; writerStack.Push(propertyNode); }
private static XamlDomObject CreateView(XElement viewTag, XamlSchemaContext schemaContext) { Debug.Assert(viewTag != null); // Create the view and canvas var canvas = new XamlDomObject(schemaContext.GetXamlType(typeof(Canvas))); var canvasChildrenMember = new XamlDomMember(schemaContext.GetXamlType(typeof(Canvas)).ContentProperty); SetFrameSize(viewTag, canvas); // Add children var subViews = viewTag.Element(XName.Get("subviews")); if (subViews != null) { foreach (var element in subViews.Elements(XName.Get("object"))) { AddControlToTree(canvasChildrenMember, element); } } if (canvasChildrenMember.Items != null && canvasChildrenMember.Items.Count > 0) { canvas.MemberNodes.Add(canvasChildrenMember); } return canvas; }