⬆️ ⬇️

.Net: we get 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)


Immediately, a disklamer is not a Rosetta stone, but a trick that will help if you have a UserControl that is almost identical for all platforms.



The bonus is the 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 it was confusing.



We were presented with a Portable Library - a great thing, for all platforms at once. But, there is also a fly in the ointment - almost nothing but numbering and business processes cannot be welded with this, the supported namespaces have failed.

This thing not only does not know what XAML is, but it doesn’t even know what Point is ! It's much simpler, two coordinates, X and Y - but this is a problem, and the subject of a separate discussion with the rays of hatred for Hindus and the mess of architects.



So, my first acquaintance with the Portable Library happened not in the best way - it reminded me of a case when a girl came up to me in a DJ and imitated Allegrov: “I was waiting for you, I was waiting, you were my frustrated dream ! "(But there the bosses of the corporate party ordered music ..)

')

But I figured out how to overcome the problem with PL, which was brewing in my future article on the HSL Color Dialog, and this article is a prefix to the next article and tutorial.

Immediately make a reservation, it will not help in all cases, for example, there is no Label in WinRT, but there is a TextBlock everywhere, “platform specificity” will have to be respected.



Short video description:



1. We create solution, and in it the WPF UserControl project. This will be our sandbox; it is here that we will create and edit future universal control (s). By itself, this project is not needed in production, so I personally called it Sandbox and scored.

1a We are only interested in bare XAML. Remove the class name from XAML, delete the .cs file itself.

1b. We edit XAML, and it is the most important, it is a working file suitable for work for GUI-designers, for example, in Blend.



2. Create a Portable Library project, select the necessary versions of everything we need from the dialogue.

2a In the project PL add XAML with control from the sandbox. Important: add as Link to file. Otherwise, all the GUI changes in the sandbox project over the control will have to be dragged by hand each time.

2b. Added link to file to make Embedded Resource.

2c. Embedded Resource is received and transmitted in Raintime 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 reliably that this is valid XAML, so in production it is important to overlap with checks! I dropped them for the sake of simplicity code.



3. We create any GUI-project from the family of WPF-relatives. A working killer feature in this multiplatform is parsing XAML in RayTime. We use it 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 PCL library acted as a proxy, passing control from the sandbox to any WPF family platform, taking advantage of its unique ability to be alone loaded by any of them. The irony is that she doesn’t know what she did. And at the same time, we still have a project in the sandbox with a control, which GUI-designers can work on.

Yes, a crutch of course. But there is a benefit, the Portable Library is suitable for transmitting anything (for example, icons) as a single source for all platforms.



Further, under each platform you can already get a Dictionary with a description of the styles for the elements. If necessary, if the default platform did not fit.



The video below covers all platforms, but is cut and speeded up to 8 minutes (the original screencast was ~ 45 minutes). Vimeo gave a clearer picture at the same resolution. Youtube "from the box" shows HD, but much worse quality.



Vimeo


Youtube


Direct links:

Vimeo

Youtube



Have a great weekend everyone!



PS Sources lay in the continuation of the next article.

Source: https://habr.com/ru/post/168811/



All Articles