
.Net: Groom Portable Library and UI
One control for all, and all for one: WPF, Silverlight 4-5, WinPhone 7-8, Windows Store App (x86, x64, ARM)
At once, the disclaimer is not a Rosetta stone, but only a trick that will help if you have a UserControl that is almost identical for all platforms.
Bonus - a video of the development process at the end of the article - the article is simple and short, and the video is 8 minutes of great music. Not everyone has Windows8, many will probably just be curious to see the process in W8 + VS2012, so I got confused.
We were presented with Portable Library - a great thing, immediately for all platforms. But, there is a fly in the ointment - almost nothing can be combined with the number of threshers and business processes, the supported namespaces failed.
This thing not only doesn't know what XAML is, but it doesn't even know what Point is.! Much easier, two coordinates, X and Y - but this is a problem, and the subject of a separate discussion with rays of hatred for the Indians and the mess of architects.
So, my first acquaintance with the Portable Library didn’t happen in the best way - it reminded me of a case when a girl came up to me in a DJ and mimicked Allegrova: “I was waiting for you, so you were waiting, you were my frustrated dream ! "(But there the corporate bosses ordered music ..)
But I figured out how to overcome the problem with PL, which was brewing in my future article about HSL Color Dialog, and this article is a prefix to the next article, and a tutorial.
I’ll make a reservation right away, it won’t help in all cases, for example, there is no Label in WinRT , but there is TextBlock everywhere, you will have to observe the “platform specifics”.
A brief description of the video:
1. We create a solution, and in it the WPF UserControl project. This will be our sandbox, it is in it that we will create and edit future universal control (s). This project itself is not needed in production, so I personally named it Sandbox and scored.
1a. We are only interested in naked XAML. Remove the class name from XAML, delete the .cs file itself.
1b. We edit XAML, and the most important thing is that it is a working file suitable for work by GUI designers, for example, in Blend.
2. We create the Portable Library project, select from the dialogue the necessary versions of everything we need.
2a. Add XAML with sandbox control to the PL project. Important: add as Linkper file. Otherwise, all GUI changes in the sandbox project over the control will have to be dragged by hands each time.
2b. Added link to file to make Embedded Resource.
2c. The Embedded Resource is acquired and transmitted on time as follows:
public class Class1
{
public Stream GetXaml()
{
string[] names = this.GetType().Assembly.GetManifestResourceNames();
Stream s = this.GetType().Assembly.GetManifestResourceStream(names[0]);
return s;
}
}
Important: PL does not know what it is actually transmitting, and the receiving applications do not know for sure that this is valid XAML, so in production it is important to override with checks! I omitted them for the sake of simplicity of code. 3. We create any GUI project from the WPF family of relatives. A working killer feature in this multi-platform is XAML parsing in rantime. We use it something like this:
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
Class1 class1 = new Class1();
DependencyObject importedXaml;
using (Stream stream = class1.GetXaml())
{
importedXaml = XamlReader.Load(stream) as DependencyObject;
stream.Close();
}
UserControl control = importedXaml as UserControl;
rootGrid.Children.Add(control);
}
Once again, I remind you of the checks. Total:
Our PСL library acted as a proxy, transferring control from the sandbox to any platform of the WPF family, using its unique ability to be loaded any of them alone. The irony is that she is not aware that she did. And at the same time, we still have a project in the sandbox with control over which GUI designers can work.
Yes, a crutch of course. But there is some benefit, Portable Library is suitable for transferring anything (for example, icons) as a single source for all platforms.
Further, for each platform, you can already create a Dictionary with a description of the styles for the elements. If necessary, if the default of the platform did not fit.
The video below covers all platforms, but it was cut and accelerated to 8 minutes (the original screencast was ~ 45 minutes). Vimeo gave garozdo a clearer picture at the same resolution. Youtube "out of the box" shows HD, but much worse quality.
Vimeo
Youtube
Direct links:
Vimeo
Youtube
Have a great weekend!
PS I will lay out source codes in continuation in the following article.