public void Present(Proposal action) { var needsRefresh = false; if (action is SetFile) { _filename = ((SetFile)action).FileName; _source = ((SetFile)action).Source; needsRefresh = true; _lastError = null; } else if (action is RefreshFromFile) { _source = ((RefreshFromFile)action).Source; needsRefresh = true; _lastError = null; } else if (action is SelectElement) { var elem = ((SelectElement)action).Element; _selected = elem; _lastError = null; } else if (action is ShowError) { var act = ((ShowError)action); _lastError = new Exception(act.Context, act.Exception); } else { throw new Exception("Unsupported action type: " + action.GetType().Name); } if (needsRefresh) { try { var render = new XamlRenderer(null); byte[] byteArray = Encoding.UTF8.GetBytes(_source); using (var stream = new System.IO.MemoryStream(byteArray)) { var tree = render.Render(stream); this._selected = tree; this._tree = tree; } } catch (Exception e) { // keep everything else intact this._lastError = new Exception("Failed to refresh from file", e); } } }
public void SelectElement(Spencen.Mobile.Markup.XamlElement element) { Proposal action; if (element == null) { return; // just ignore } action = new SelectElement(element); _model.Present(action); _display(_model); }
XamlTreeNode FindCorrespondingNode(XamlTreeNode node, Spencen.Mobile.Markup.XamlElement elem) { if (node.Element == elem) { return(node); } foreach (var c in node.Nodes) { var n1 = (XamlTreeNode)c; var r = FindCorrespondingNode(n1, elem); if (r != null) { return(r); } } return(null); }
XamlTreeNode PopulateNodes(Spencen.Mobile.Markup.XamlElement element) { if (element == null) { return(null); } var res = new XamlTreeNode(element.Name, element); foreach (var c in element.Children) { var r1 = PopulateNodes(c); res.Nodes.Add(r1); } return(res); }
public SelectElement(Spencen.Mobile.Markup.XamlElement element) { _element = element; }
public XamlTreeNode(string id, Spencen.Mobile.Markup.XamlElement element) : base(id) { _element = element; }
void MyUpdate(Model model) { if (_root != model.Tree) { _root = model.Tree; // construct the new tree... this.xamlNodes.Nodes.Clear(); this.xamlNodes.Nodes.Add(PopulateNodes(_root)); } if (this.textBox1.Text != model.Source) { this.textBox1.Text = model.Source; } if (model.Tree == null) { this.panelXamlForm.Controls.Clear(); } else { if (this.panelXamlForm.Controls.Count != 1 || this.panelXamlForm.Controls[0] != model.Tree.Instance) { this.panelXamlForm.Controls.Clear(); this.panelXamlForm.Controls.Add((Control)model.Tree.Instance); } } this.textBoxFileName.Text = model.FileName; var nd = model.Selected; if (nd == null) { textBox1.DeselectAll(); xamlNodes.SelectedNode = null; this.xamlPropertyGrid.SelectedObject = null; } else { var startOfs = this.textBox1.GetFirstCharIndexFromLine(nd.StartPos.Line - 1) + nd.StartPos.Column; var endOfs = this.textBox1.GetFirstCharIndexFromLine(nd.EndPos.Line - 1) + nd.EndPos.Column; textBox1.Select(startOfs, endOfs - startOfs); textBox1.ScrollToCaret(); var myNode = FindCorrespondingNode((XamlTreeNode)xamlNodes.Nodes[0], nd); xamlNodes.SelectedNode = myNode; xamlPropertyGrid.SelectedObject = myNode != null? myNode.Element.Instance : null; } if (model.LastError == null) { this.textBoxError.Text = ""; } else { var msg = "Error: \r\n" + model.LastError.Message + "\r\n"; msg += "source: " + model.LastError.Source + "\r\n"; if (model.LastError.InnerException != null) { msg += "Inner Exception: \r\n" + model.LastError.InnerException.Message + "\r\n"; msg += "Inner source: " + model.LastError.InnerException.Source + "\r\n"; msg += "Inner stack trace: " + model.LastError.InnerException.StackTrace + "\r\n"; } this.textBoxError.Text = msg; } }