I have a WPF app in which my client wants to be able to write a custom xml file to change the default layout set by an existing XAML file. What I am trying to do is fall back to the XAML layout if there are errors in the xml file I am parsing. I have the code behind done for parsing the xml file and changing the default layout but I'm trying to find a way to load the XAML file if something goes wrong. Is this at all possible?
If you need an ability to load a layout dynamically, here is one possible solution. This can be achieved by using the XamlReader class. The process is quite straightforward, just use the XamlReader.Load method to load a serialized content from a stream (xml or whatever).
However, i can't see why you don't want to just validate the user-defined xml file, and if something is wrong, don't do anything.
Related
I want to be able to write unit tests against Xaml to look for improper patterns. We have issues where the Xaml is still valid and otherwise compiles and "works" fine but can cause hidden issues or lacks consistency. In order to help combat this we want to write unit tests against the files.
A simple example: If you define a grid and the grid column is bound to a numeric field on the underlying object then the column title of the grid should have the appropriate right align template per our internal standards. Leaving the template off the definition will yield a left aligned column header which doesn't really break anything other than our internal standards. Given this is basically xml, I should easily be able to write a test that interrogates the xml element, detects the existence of a numeric column and checks to make sure that the file also defines a title template that is right aligned. This way I don't have to rely on the developer or QA to cover things that are easily testable like this.
I have access to the assembly during the unit test process and can get to all the baml resource files by reading the manifest resource stream. But I can't figure out how to read them into a useful format that I could load into something like an XDocument for parsing and perusing. Has anyone done this? I've tried using Baml2006Reader, etc... without any good success.
If you want to convert BAML to XAML, you could take a look at the following BamlTranslator class on GitHub.
It uses a BinaryReader to decompile the BAML.
I am working on a C/GTK+3 project right now. I have a little experience with front end but mostly only with Android. Even then that was VERY LITTLE experience.
I notice that in the C code I place objects and set properties (like if a textview is editable etc). I also have been able to connect a .ui XML file to my program with the builder functions.
I am wondering how the XML file gets linked to the C code defining the interface. Does it match by structure, by the name or ID properties? Why are properties like "visible" and "editable" present in both the C code and the XML? Do you need the XML file? Do you need to specify the properties in both the XML file and the C code or just one? Will I ever completely understand front end development?
There's two ways of constructing a user interface:
You write the code for it.
You write a file that describes the user interface, and have the user interface built for you at runtime.
If you write the code yourself, everytime you want to move widgets around, you have to modify your code, that you'll need to compile. That is doable for a small UI, and is what is done in most tutorials.
If you use ui files, then you use the Glade application to design your UI graphically, and it will write the UI files for you. This helps in splitting responsabilities too: you may have design people taking care of the UI, and let developpers focus on the behavior. Even without that, you'll be able to design a UI faster with an editor like Glade than by coding it by hand. Think you skip all the compile/debug cycles.
In your ui file, if you name a wigdet "bob", you'll be able to get it in your code by passing its name to gtk_builder_get_object. GtkBuilder takes care of the construction and the memory, so don't destroy the widgets it creates if you need to display them again, and hide them instead.
You have examples of use of UI files in the GTK documentation:
https://developer.gnome.org/gtk3/stable/ch01s04.html#id-1.2.3.12.5
I have started using xaml based icons in my WPF app, but am struggling to find a simple way to store and share them. The icons are DrawingBrushes.
Do I?
A) Store each icon in its own xaml file wrapped in a resource dictionary. This means that I either have to pull in each icon resource every time I want to paint one, or create a standard "icons" dictionary that pulls every xaml file. This seems a pain to have to maintain, and I still have to pull in the icons resource dictionary every time I want to use an icon.
If I did go for this, how do people normally display the icon? Just a Canvas with the brush as the background?
B) Store each icon in its own xaml file as a UserControl. This makes displaying an icon dead easy - just plonk the approprate control in, and its arguably simpler to declare the XML icons namespace at the top of your XAML rather than merging a resource dictionary.
C) Store all icons in one XAML file wrapped in a resource dictionary. One dictionary to merge but without the maintenance of pulling in individual files into that dictionary. Potentially awkward to edit icons though.
D) ....?
How are other people managing their icons?
I have a ListView Binded to an xml file (a simple log file).
This list view is in a secondary dialog Window in my project.
When I open the window for the first time it shows the info in the list view correctly. Then, if I close this window and do some stuff, my xml file changes. If now I reopen the dialog Window containing the xml file, it shows the last status (the info showed the firs time I opened the window), not the whole status with updates.
Why??
The XML doesn't trigger the INotifyPropertyChanged events that WPF databinding relies on to know when items change and need to be re-bound. You may want to set the ItemsSource or DataContext explicitly each time you open the dialog window.
Also, the XML objects don't detect file changes. You may need to reload the XML if you are relying on outside events (system logs?) to create the new XML nodes.
It's hard to tell exactly what will fix your problem because your description is imprecise. You say:
I have a ListView Binded to an xml file (a simple log file).
and this is not actually possible. You can't bind a ListView, only its ItemsSource. And you can't bind to an XML file, but only to an object in memory that contains the data read from the XML file. This sounds like I'm picking nits, but I'm not: the clearer you are in describing what you're actually doing, the easier it will be to provide you with a usable answer.
It sounds as though you probably are defining an XmlDataProvider in XAML with its Source set to the name of the XML file. If you set this as a resource in your main window, it's going to be loaded when the main window is loaded. If your child window has something like
<ListView ItemsSource="{StaticResource MyXmlDocument}" .../>
it's going to look up the XmlDataProvider using the resource dictionary's Find method, and find it in the main window's resource dictionary. You can close and reopen the child window all day long and the XmlDataProvider it uses won't change.
If this is the case, what you probably need is one of two things.
First, you could move the definition of the XmlDataProvider resource to the child window's XAML. That will force the child window to reload the XML every time the child window loads.
Second, you could add a method to the main window that recreates the XmlDataProvider whenever the XML file changes on disk. (How you know when the XML file changes, and how you trigger the method in the main window, are relatively tricky problems to solve - you could use FileSystemWatcher events, for instance.) You'd need to use the DynamicResource markup extension in your child window XAML if you do this.) That will refresh the child window every time the XML file changes - even if it's already open.
I want to add/remove some part of XAML dynamically i.e. from code behind file in C#.how to Add any specific XAML string on specified location (means under some tag) from .cs file. Please help.
XAML is xml you can use XmlReader and XmlWriter or any other of the XML serialization mechanismn of .NET to write/read XML (XMlDocument is probably the best approach here to modify existing xml). ALso there are the XamlReader and XamlWriter class that allow ou to (de)serialize any object graph from/to XAML automatically. If you can use .NET 4.0, you have even more fine grained possibilities to athor XAML because it has a new XAML stack. Use this as a starting point.
What is it that modifying the XAML will do for you?
If you just want to change the appearance of your WPF application (perhaps by adding some more content at certain locations), it will most likely be easier to do this by referencing the objects in question. So, if you need to add some text to a button, name the button with x:Name="myButton" and in code set: myButton.Content = "Click Me"
XAML is really a technology for constructing object hierarchies. Pretty much every element in the XAML corresponds to a .NET CLR class. When loaded, these classes are instantiated nd populated according to the attributes used in the XAML. Once loaded, the XAML has finished it's job and is essentially unloaded/unavailable.
You might need to do something beyond this, but from your brief question it doesn't seem like it. I would just work on the object model and leave the XAML be.