Serialize an image in XAML using XamlWriter - wpf

I'm using XamlWriter to serialize a group of WPF objects. One of these objects is an Image control whose Source is set to a file on disk.
When the XamlWriter serializes the objects, it sets the source image to an Uri that points to the file on the filesystem. Is there a way to include that data in the Xaml so that instead of referencing the file, the information is stored in xaml?
Thanks

From MSDN:
Serialization Limitations of XamlWriter.Save
"Images are also serialized as object references to images as they exist in the project, rather than as original source references, losing whatever filename or URI was originally referenced."

Well, what I did in the end was build a wrapper class that used a binary serializer to store the images and the xaml code from the XamlWriter.

Related

ResourceDictionary v .resx

In a WPF application, for a localizable text resource such as an error message, should I be using a .resx file or a ResourceDictionary. If the answer is either/or what factors would help me decide which to use?
Using .resx files - with the name of the culture in the filename - is probably the easiest way to go. The loading of the appropriate resources is handled for you.
So you'd have "ResourceFile.resx" as your default and then "ResourceFile.en-GB.resx", "ResourceFile.fr-FR.resx" etc for your localised strings.
You only need to put those strings that actually need localisation in the language files. If the string's not present in the culture specific file it falls back to the default file.

Supporting additional Uri Schemas for the Image.Source property

Is there a way to make the following code behind expression to evaluate to true given the Xaml below. I'm asking this question because I have to work with a Library that converts FlowDocuments containing Images to HTML. Only the "Source" Attribute of the Image Control is persisted and you can't use the standard approach of using a BitmapImage because this won't convert back to a Uri String.
Debug.Assert(this.image1.Source.ToString() == "data:image/x-png .. <rest of the Image.Source Property below>")
<Image x:Name="image1" Source="data:image/x-png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAy1JREFUeNpMU1toVFcUXefc18zNZJJJZmLHSDAmCiqKrbV/7U8Rv5oPQT9URKgobUVQQRD88cOfVq20NNZGSotIqQpaTXzGiM+KQWK0kvEVw0xGzENm4sxkzr3nnHt77jRT3HDhcs7ea+219j7E930Ekc1m8ezFMJrq7E88I/RwYmzcHR3NQNM0xGIxNCRmQWfvVuqRukFHyPGR4WEMDQ2BYiaIZoBI3jx2/seu0suHm6gVrpwLIeCBQIeck/nz8JFc/7XdeqimWga9WCyqagopeEO+53B3/t75peEHfT8n1u0rw4wcB3dACYmnf9/fk+071/b6bu+u+RJMa164NwDQ5rW14emTfxK5cwe7iwNXP7YjddCdIhGpvzt4Y+tj124YMXuPXZi+f2mFGY6Ac4nJ/hufUTuqvwvHrpO29nbYvtO6sWnyTnuDndQUqkUJ4LoQVnS6HEmmrPSTjwQx4HCg5HooMo7jo+43I1ZjJ6lqqbfo3D3La7uXx43FxAMMKHOFB58LuL4Ol/tg3EOeeeh8xbY8mBJdFQlVACb9/K2M89eiWrmqJWI2UVWvK/OIrzgUoCeBQpm73w+Vtg+UvKP/m9/R0VH5kVLizfgkklGr5SszNRgDrycega8KXcXsMIF+fdbpi+H2NaKQr+QXCgVFMLMHQVy42gd658RRevv0ZotqVFNXVYASk+BaiGmrv975Ot56JP0shXQ6DT2XywWNYFpITPX+dsi4fXJLjdoBJVmxqCvPr5gnlZRyoRgq/Hqg0/l8rXBqP+jinEMrM4YbN29ambOd3xkDl3fouqloidoLwBE+skU5YQE1TCG6ShJzlalPH38hqc7ydv1dzZUeRl+kmhZMPPolahlh1YgyzEcwiVMZ9tMPw2zDHIt+2qiR5lIAEpiqah69ykw85/QMsW0b1LAQcaaW7VhoX4kbNEFVce+Y88eJjLNeifDDFPGts60rrRb9UCjwewVx5tRbsTbYdLwfyRBd9u3SyOSXc0M97484iBBFYlvSHNyUMPpIsCbVMaoveFBKOCqvJ6qTJWXpZ1S3WfyXGOQEdnKToEU1Zypr3s6wl/8VYACfeYRluTSjlQAAAABJRU5ErkJggg=="></Image>
The solution is to implement a pluggable protocol for the "data" Scheme. This way the Source property acts exactly as with the built-in protocols for Image retrieval. The technique is described here.

Number of Objects Resulting from a Loose xaml File

Suppose I have a loose xaml file with one resource in it, keyed "MyResource", and that I pull that loose xaml file into two other xaml files via ResourceDictionary.MergedDictionaries. Now suppose I put the following line of code in both of the code-behind files for the two xaml files:
object obj = FindResource("MyResource");
Will both references be to the same object, or will they be to distinct objects?
Thanks,
Dave
The answer depends on how you load the loose XAML file. If you load it once and the same reference is added to two different MergedDictionaries, then by default you'd get the same reference to your "MyResource".
If you load the XAML file twice, you will effectively create two instances of the ResourceDictionary (and thus two instances of your resource).
In a single ResourceDictionary, you can specify if a resource is shared using the x:Shared attribute, which is true by default. Setting this to false, will force new instances to be created for each request of the resource. There are some restrictions on the use of this attribute, which are explained on the MSDN page.

Get XAML source of WPF Window

I would like to get the XAML source of a WPF Window (MainWindow). Clicking on a button on that window would return the XAML of the window and I would save it in another file.
Is this possible and how can it be achieved?
You can use the XamlWriter:
using (Stream stream = File.OpenWrite("D:\\Test.xaml"))
{
XamlWriter.Save(this, stream);
}
You can use XamlWriter for some basic Xaml Serialization.
In particular, look at this article on its limitations.
The earlier answers are both correct, but I think it should also be mentioned that you can also extract the original XAML used to create the window (if desired) using the API for Reflector's BAMLViewer extension.
BAMLViewer solves a different problem than XamlWriter: Using Reflector / BAMLViewer will return the original source XAML with all bindings, etc intact but will not include current property values. Using XamlWriter will include current property values but such things as resource references and markup extensions will be lost. Also, some things will not serialize using XamlWriter.
You must choose between these based on your application needs.

Modify XAML string dynamically

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.

Resources