I found how to programmatically parse/load a XAML string by using XamlReader.Load(), however if I were to have a user drag/drop a ".xaml" file onto my Silverlight application, how would I go about parsing that file to a XAML file and reading in the contents as a string to serve up to the XamlReader.Load() method.
Or is there a more efficient/better way through using reflection?
I'd like to allow the user to use a "silverlight for dummies" silverlight designer (within reason) to design a simple control. (i.e. a label and a textbox).
Then save this or export it as a XAML file (or maybe I can simply have them save it as a TXT file?)
Then they'd drop this file into my Silverlight application and it would parse the text into a XamlReader.Load(), then I can add my newly programmatically created object to a listbox.
I have the logic for drag/drop file and for loading up the XAML string, but that middle point of getting the contents of a ".xaml" file is what I'm confusing myself on...
EDIT
#nicholas, that's exactly what I went with. I don't know what was going on yesterday but I think I just had a major 'brain-fart'.
This is how I ended up going: (still have some cleanup and refactoring, but this was to test)
IDataObject data = e.Data;
if (data.GetDataPresent(DataFormats.FileDrop))
{
FileInfo[] files = data.GetData(DataFormats.FileDrop) as FileInfo[];
if (files.Length > 1)
{
//TODO
}
else
{
FileInfo file = files[0];
extension = file.Extension;
string xaml = string.Empty;
using (Stream stream = file.OpenRead())
{
xaml = StreamUtils.StreamToString(stream);
}
if (!xaml.IsEmpty())
{
try
{
myListBox.Items.Add(XamlReader.Load(xaml);
}
catch (Exception ex)
{
//TODO
}
}
}
So you have a Drop event handler, from which you receive event args with Data property, an IDataObject. From MSDN you find out how to get a FileStream for the dropped file, which you can then load into a string (eg use StreamReader) to be parsed by XamlReader.Load().
If I understood you correctly there will be three different steps:
Parse the content of .xaml file or any type of file into a "string"
(it can be a .txt file or any other format)
Use XamlReader.Load() to generate an UI element out of this string
from step 1
Insert the UI element from step 2 to ListBox.Items collection
Does that help?
Related
I have a WindowsForm application having all text(On form labels/buttons/other controls) written in the Norwegian language. I want to convert all text in English using localization. Is there any way in Devexpress where we can convert all text into English without writing the meaning of every text in a resource file manually?
For example:- In the attachment, "Brukernavn" is hardcoded on a label. I want to auto-convert it into English without assigning its value in English-ResourceFile. What should be the approach in Devexpress localization?
I'm not aware of any Devexpress method to do this. A good solution would be to actually do the work of building a localisation file for English which would work like following.
Add a InternationlisationLayer to your application.
This layer
scours the application to locate all Controls you would possible want
to translate.
After finding all Controls you have to match their
Text values to the translated text.
After finding a matching English
text you will have to replace the Text Property on these Controls.
If you want to avoid building a proper Localisation system a far easier solution is explained below.
Make a List of type and fill it with all control texts you want translated.
Translate the strings and add format them as such that you have a Dictionary of type (where Key would be the original text and Value being the translated text).
At application Start get a list of all Controls you want to translate and do something like the following:
public static IEnumerable<System.Windows.Forms.Control> GetAllControlsOfType(this System.Windows.Forms.Control control, Type type)
{
var controls = control.Controls.Cast<System.Windows.Forms.Control>();
return controls.SelectMany(ctrl => GetAllControlsOfType(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
public void DoTranslation()
{
var ctrls = this.GetAllControlsOfTypes(new List<Type>() { typeof(Label), typeof(Button) });
foreach (var ctr in ctrls)
{
var element = dict.FirstOrDefault(i => i.Key == ctr.Text);
ctr.Text = element.Value;
}
}
I need to search an expression inside a xps document then list all matches (with the page number of each match).
I searched in google, but no reference or sample found which addresses this issue .
SO: How can I search a xps document and get this information?
The first thing to note is that an XPS file is an Open Packaging package. It can be opened and the contents accessed via the System.IO.Packaging.Package class. This makes any operations on the contents much easier.
Here's an example of how to search the page content with a given regex, while also tracking which page the match occurs on.
var regex = new Regex(#"th\w+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
using(var xps = System.IO.Packaging.Package.Open(#"C:\path\to\regex.oxps"))
{
var pages = xps.GetParts()
.Where (p => p.ContentType == "application/vnd.ms-package.xps-fixedpage+xml")
.ToList();
for (var i = 0; i < pages.Count; i++)
{
var page = pages[i];
using(var reader = new StreamReader(page.GetStream()))
{
var s = reader.ReadToEnd();
var matches = regex.Matches(s);
if (matches.Count > 0)
{
var matchText = matches
.Cast<Match>()
.Aggregate (new StringBuilder(), (agg, m) => agg.AppendFormat("{0} ", m.Value));
Console.WriteLine("Found matches on page {0}: {1}", i + 1, matchText);
}
}
}
}
It is not going to be as simple as you might have thought. XPS files are compressed (zipped) files containing a somewhat complex folder structure containing all the text, fonts, graphics and other items. You can use compression tools such as 7-Zip or WinZip etc. to extract the entire folder structure from an XPS file.
Having said that, you can use the following sequence of steps to do what you want:
Extract the contents of your XPS file programmatically in a temp folder. You can use the new ZipFile class for this purpose if you're using .NET 4.5 or better.
The extracted folder will have the following folder structure:
_rels
Documents
1
_rels
MetaData
Pages
_rels
Resources
Fonts
MetaData
Go to Documents\1\Pages\ subfolder. Here you'll find one or more .fpage files, one for each page of your document. These files are in XML format and contain all text contained in the page in a structured manner.
Use simple loop to iterate through all .fpage files, opening each of them using an XML reader such as XDocument or XmlDocument and search for required text in node values using RegEx.IsMatch(). If found, note down the page number in a List and move ahead.
I have created a "FileTree" class which displays a file system via a JTree, and only displays folders and ".xlsx" or ".xls" files. I want to be able to add a double-click action to the DefaultMutableTreeNodes if they are a ".xlsx" or ".xls" file. It doesnt seem possible to be able to add an ActionListener or a MouseListener to a DefaultMutableTreeNode, is there a way I can control the double-click action on these nodes?
I have a solution of sorts, its not the one I was looking for, but it does work.
I have added a MouseListener to the JTree, and then when the click count is 2, I check the event source is an instanceof the JTree, then call
Object comp = tree.getLastSelectedPathComponent();
if (comp instanceof FileTreeNode)
{
FileTreeNode ftn = (FileTreeNode) comp;
File file = ftn.getFile();
}
and then I can do whatever I want with the file. FileTreeNode is an extension of DefaultMutableTreeNode which contains the file at that node.
I am trying to load a word document into Rich Textbox control so that user can edit the contents and eventually save it. Following is the code I use:
string fileName = #"..\..\Files\test.doc";
TextRange range;
FileStream fStream;
if (File.Exists(fileName))
{
range = new TextRange(RTB.Document.ContentStart, RTB.Document.ContentEnd);
fStream = new FileStream(fileName, FileMode.OpenOrCreate);
range.Load(fStream, DataFormats.Rtf);
fStream.Close();
}
When I run it I get the following error:
Unrecognized structure in data format 'Rich Text Format'.Parameter name: stream
I have made sure that that document contains no images.
A word document isn't a rich text document.
The doc would need to be first saved as an RTF file before you can do this.
Greetings,
I have a problem with printing in WPF.
I am creating a flow document and add some controls to that flow document.
Print Preview works ok and i have no problem with printing from a print preview window.
The problem exists when I print directly to the printer without a print preview. But what is more surprisingly - when I use XPS Document Writer as a printer
everyting is ok, when i use some physical printer, some controls on my flow document are not displayed.
Thanks in advance
Important thing to note : You can use XpsDocumentWriter even when printing directly to a physical printer. Don't make the mistake I did of avoiding it just because you're not creating an .xps file!
Anyway - I had this same problem, and none of the DoEvents() hacks seemed to work. I also wasn't particularly happy about having to use them in the first place. In my situation some of the databound controls printed fine, but some others (nested UserControls) didnt. It was as if only one 'level' was being databound and the rest wouldn't bind even with a 'DoEvents()' hack.
The solution was simple though. Use XpsDocumentWriter like this. it will open a dialog where you can choose whichever installed physical printer you want.
// 8.5 x 11 paper
Size sz = new Size(96 * 8.5, 96 * 11);
// create your visual (this is a WPF UserControl)
var template = new PackingSlipTemplate()
{
DataContext = new PackingSlipViewModel(order)
};
// arrange
template.Measure(sz);
template.Arrange(new Rect(sz));
template.UpdateLayout();
// print to XpsDocumentWriter
// this will open a dialog and you can print to any installed printer
// not just a 'virtual' .xps file
PrintDocumentImageableArea area = null;
XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area,);
xps.Write(template);
I found the OReilly book on 'Programming WPF' quite useful with its chapter on Printing - found through Google Books.
If you don't want a print dialog to appear, but want to print directly to the default printer you can do the following. (For me the application is to print packing slips in a warehouse environment - and I don't want a dialog popping up every time).
var template = new PackingSlipTemplate()
{
DataContext = new PackingSlipViewModel(orders.Single())
};
// arrange
template.Measure(sz);
template.Arrange(new Rect(sz));
template.UpdateLayout();
LocalPrintServer localPrintServer = new LocalPrintServer();
var defaultPrintQueue = localPrintServer.DefaultPrintQueue;
XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue);
xps.Write(template, defaultPrinter.DefaultPrintTicket);
XPS Document can be printed without a problem
i have noticed one thing:
tip: the controls that are not displayed are the controls I am binding some data, so the conclusion is that the binding doesn't work. Can it be the case that binding is not executing before sending the document to the printer?