Getting XAML of the current screen in silverlight 2 - silverlight

I am trying to send my dynamically created silverlight 2 page/image to a an ASP.net web service to render it as an bitmap image.
I can see many examples how to get the XAML using javascript (see here) in version 1 of silverlight but I have a few differences.
a) I am using silverlight 2 RC1
b) I have dynamically add controls to the page in c#
c) I would prefer to use c# to get the new XAML to keep all the coed in one place.
does anyone know how to extract the XAML from a control or the page in c#?
Thank you
Update: This is now possible under silverlight 3 using a writable bitmap to save the XAML as a JPEG see my blog post here: http://blog.blueboxes.co.uk/2009/07/21/rendering-xaml-to-a-jpeg-using-silverlight-3/

unfortunately there is not a method for a .ToXaml() on an element tree unfortunately. You can use VisualTreeHelper to build such a method and recurse through a particular element I suppose.

SilverlightContrib has a XamlWriter class that can extract the XAML from a live control.
It's free.
The link is: http://silverlightcontrib.org
The code would be something like:
// using SilverlightContrib.Xaml;
var cb = new GroupBox();
StringBuilder sb = new StringBuilder();
XamlWriterSettings settings = new XamlWriterSettings();
using (XamlWriter writer = XamlWriter.CreateWriter(sb, false, settings))
{
writer.WriteElement(cb);
}
string result = sb.ToString();

Related

DotNetBrowser WinFormsBrowserView opening in tiny window within form

I followed the getting started tutorial for WinForms (https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000056958-quick-start-guide-for-winforms-developers).
I am using VS 2017 and .NET 4.6.1
Everything works great, but the browser window within the form is opening in a tiny window (approximately 50px square) with scrollbars and not taking up the full form. I've been scanning SO questions and the documentation and haven't found anyone reporting this before and I have not been able to understand how to configure this. Are there parameters for placing the control?
I tried using the 'UpdateSize' method, but that does not seem to do anything.
Has anyone else encountered this issue?
Hopefully this is a simple fix.
Thanks! Aaron
I figured it out. The BrowserView must be cast to a Control which then exposes many additional WinForm control properties such as DockStyle.
BrowserView browserView = new WinFormsBrowserView(BrowserFactory.Create();
Control browserWindow = (Control)browserView;
browserWindow.Dock = DockStyle.Fill;
Controls.Add(browserWindow);
In DotNetBrowser 1.16 and earlier versions WinFormsBrowserView.Dock property was set to DockStyle.Fill value by default.
In DotNetBrowser 1.17 and higher this property is set to DockStyle.None value by default.
Using the latest 1.19.1 even after replacing references multiple times.
WinFormsBrowserView does not show the .Dock property
Able to work around using this:
Public browser As Browser
Public browserView As BrowserView
browser = BrowserFactory.Create(BrowserType.HEAVYWEIGHT)
browserView = New WinFormsBrowserView(browser)
'browserView.dock = DockStyle.Fill '--this will not work so instead:
Dim obj As Control '--or Object
obj = browserView
obj.dock = DockStyle.Fill
If Controls.Contains(browserView) = False Then
'Controls.Add(browserView) '--Before
Controls.Add(obj) '--Now
There has to be a better solution. Please educate me.

Localization in Silverlight 4 application using PRISM

I am new to silverlight development and PRISM framework. We are using http://happynet.codeplex.com/ as a template. The localization is working well if resource text is bind directly in XAML. But it is not working well for :
Texts in ViewModel Classes.
Grid Header Column Texts in View's Code behind.
They requires reloading after language switch. How can it be done so that text change occurs on language switch?
I don't know much about Happynet as it is the first time I see the project.
It depends how they implement localization but if you want dynamic localization (eg. selecting a language in a combobox) it can be done. Follow the following steps to set it up:
http://vanderbiest.org/blog/2009/11/30/silverlight-resource-files-localization-language-specification/
To dynamically change it, you just need to set the current UI thread.
static void SetLanguage(string culture)
{
if (culture != System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.CultureName)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
}

Reverse engineer a ControlTemplate

Windows Workflow Foundation 4 contains a WPF property grid similar to the one available for Winforms. The toolbar up the top has a CLEAR button rather than the X shown by Visual Studio. This control has a ControlTemplate property that is not null at runtime. The template makes use of various glyphs for toolbar buttons.
Are there any tools or techniques that will allow me to extract this template? I would like to examine and modify the XAML to restyle the toolbar (mostly just change the button label).
I have programmatic access to the template object but I'm not sure how to serialize this into XAML.
Have you tried simply using XamlWriter.Save?
e.g.
var template = control.Template;
using (var stream = new FileStream("template.xaml", FileMode.Create))
{
using (var writer = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
{
XamlWriter.Save(template, writer);
}
}
have you tried Red Gate's Reflector? Of course, if the assembly is obfuscated it may not really be of much help.

WPF - example of creating XPS document that is NOT from Visual

I'm looking for a reporting/printing solution that does not involve RDLC/SSRS. I'd like to use the DocumentViewer, which I know supports XPS. I have found plenty of examples that use Visual to XPS but I haven't found many examples where I can take an existing WPF page, with various controls like labels, listboxes, grids, etc and create that into an XPS document. Is there a code example out there that takes an entire XAML page and creates XPS?
It's not trivial, the basic problem here is that XPS represent fixed pages. An existing WPF page does not necessarily translate to pages on a document. How will your report be split if it cannot fit the page? This information is needed.
What you can do is to create the report as a FlowDocument (see http://msdn.microsoft.com/en-us/library/aa970909.aspx). This will give the .NET framework enough info on how to paginate your report such that when you do this:
FlowDocument flowDocument;
// load, populate your flowDocument here
XpsDocument xpsDocument = new XpsDocument("filename.xps", FileAccess.ReadWrite);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
it works. (Code lifted from Pro WPF in C# Book).
Usually your WPF page has a root UI element, say Grid. As Grid is a specific type of Visual(please vide "Inheritance Hierarchy" part #http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx for more details), you just need to write that root Grid element like other visuals into XPS. And then all embedded controls will be automatically written into XPS document.

How do you load and save content from a Silverlight 4 RichTextBox control?

I've been reviewing the features of the RichTextBox control in Silverlight 4.
What I've yet to find is any examples of Loading and Saving content in the RichTextBox.
Anyone come across any or can shed some light on it?
The control has a BlocksCollection in which I guess one could use the XamlReader to load a bunch of markup assuming that markup has a single top level node of type Block. Then add that block to the Blocks collection. Seems a shame that the RichTextBox bothers to have a "collection" in this case, why not simply a top-level Block item?
Never-the-less that still leaves saving the content of a RichTextBox, I have no idea where to begin with that one?
I'm sure I must be missing the obvious here but unless loading and saving data in to and from RichTextBox is at least possible if not easy I can't see how we can actually put it to use.
Edit
Thanks to DaveB's answer I found discussion of something called the DocumentPersister. However no reference to this class can be found in the MSDN documentation nor can I find it in the installed dlls via object browser search. Anyone, anyone at all?
Check out this tutorial
on the RichTextArea control. Persisting content is described in exercise 2, task 3. The code for the tutorial includes a helper class.
Edit:
The question was raised about the DocumentPersister class referenced in the tutorial. It is found in the source code download for the tutorial. I think the author created it. By looking at the code you will get an idea as to persisting your data. The only downside was that if your data contained images, the helper class did not support them. Here is the link to the download.
http://ecn.channel9.msdn.com/o9/learn/Silverlight4/Labs/TextEditor/Source.zip
Just to update the link in the accepted answer, it's moved to here:
http://channel9.msdn.com/learn/courses/Silverlight4/NewFeatures/RichTextBox/Introduction/
Be wary of investing too much in the Silverlight 4 RichTextArea until it's confirmed that it will support full RichEdit functionality like bullet points/lists etc which it currently does in SL Beta 1... although I'm sure it will in RTM?
I have a sample from Microsoft to persist the contents which I have to find which I will do tomorrow. I got this sample in october when beta 4 was not even announces due to which there were no tutorials available. Since that project I have not worked on silverlight so I don't know how many tutorials are available now.
ok I have found it. Where should I upload it?
One option for loading text into RichTextBox is to use XamlReader. Dependent on the text which you are planning to load, you might need to add tag around it
public class TextToXamlConverter
{
private const String ParaHead = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">";
private const String ParaTail = "</Paragraph>";
static public Paragraph Convert(string text)
{
String formattedText = ParaHead + text + ParaTail;
Paragraph p = (Paragraph)XamlReader.Load(formattedText);
return p;
}
}

Resources