Video: XAML Islands
>> On today's Toolbox, we're going to dig deeper into XAML Islands and we'll see what treasure we can find. Hi. Welcome to Visual Studio Toolbox. I'm your host Robert Green. Joining me today is Adam Braden. Hey Adam. >> Hello Robert. >> Welcome back in the show. >> Thank you. Thank you for having me. >> We did a two-parter on modernizing existing Windows desktop apps. Then the second part, which aired last week, we talked about XAML Islands a little bit briefly. Today, we decided that you'd come back and do a bit more of a deep dive into XAML Islands, to get a little bit more into some of the use cases, and show how it works.
We felt like we skimmed over it a little bit last episode. Let's dive into it a little more. >> Yeah. In the previous episode, I showed some of the easy drag and drop experiences provided by the Windows Community Toolkit which provides wrapper controls for the media player, and web view, and InkCanvas and things like that. I hinted at using the XAML Host Control to load any or I did show using the XAML Host Control to load any control dynamically. >> Right. >> What I didn't really get into showing was actually building a UWP UserControl to get that full dynamic UWP XAML experience and how to load that into your Win forms and WPF applications. >> The idea is that as you're building a UWP app, this bunch of XAML that you have that takes care of the UI. Yes, it's pretty simple to use a control from UWP in WPF app, what if you have a bunch of XAML that you basically want to reuse? >> As well as maybe you want to do some more in-depth the XAML fluent design like the transparency, the animations, that sort of thing. So, what I wanted to do today was actually show you both how to do the hard way through the XAML Host Control and all your WPF code and you'd have to code all that up yourself, as well as doing it in easier way by building a UWP control.
>> Okay, cool. We should mention that you're using the soon-to-be-here Visual Studio 2019 for this demo, which at this point is soon. >> Yes. >> It will launch on April 2nd, I believe it is. >> Yes. >> So, just to level-set. >> Cool. So, what I have here on my desktop right now is the XAML Controls Gallery. I showed a little bit of this in our previous episode and just wanted to highlight some things that I'll be showing. One of the examples is the animation interop. With animation, oftentimes it draws the user's focus to an activity or something you want them to accomplish. Whether it's a transition from one flow to another on the form or really drive the importance of something by exporting it in View and then drop it back down. As the example here in the controls gallery is just a simple blowup of a button. I'll show you how we can do that as well.
There's a little bit of code to create this spring animation effect, is what it's called. There's another one further low or further down on here, where we've got connected animations between multiple controls, and you can spring all of them together. What I have essentially done is taken the code from this sample and poured it into a WPF application using the XAML Host. >> Okay. >> So, let's take a look at that. So, the first thing with my application here, this is just a simple WPF app. If I go to the designer, you'll see I've got a couple of WPF buttons and textboxes as well as this wonderful XAML Host, Windows XAML Host. >> Okay. >> What I've done here is-. >> Just to refresh our memory, how do you get that? From data reference or do something, a NuGet package? >> Generally, you'll manage your NuGet packages. If you add a reference to the Microsoft toolkit here, the Microsoft Toolkit WPF. >> Got it. >> That'll give you that XAML Host, where you can load any content into the form.
I also have a reference to Win UI and the SDK contracts. So, you can program against the Windows APIs. This example we're not focusing on those parts as much. >> Right. >> But one of the main things or the primary thing with using the XAML Host is you set the initial type name of the control. Any set that to the UWP type that you want to load. So, I'm going to host a stack panel. Then with encode, I can dynamically load a whole bunch of controls into that stack panel, right? >> Okay. >> Once the XAML Host is ready for you to start manipulating those controls, it fires the child changed event. So, if I go back to my control or my code, you'll see here at the top that I've declared a number of controls, UWP-based controls, that I'll be manipulating in my code. Down here in the child changed event is where I start initializing those controls. First thing I typically do is get the sender tells me which for the Host Control and then I'll get the child which is the stack panel.
Then I'm doing a cool effect here, the acrylic brush. Are you familiar with acrylic in the background? >> No. >> Well, let me switch back to the controls gallery real quick and we'll show you a quick example of that. So, acrylic is that transparency effect, a little hard to see on the board here. But you can see as I move around, you can see some transparency of both the background of my desktop and this orange thing, different things behind it. >> Okay. >> I've amped up the transparency to be able to show that a little bit better here. So, I'm going to apply this background to the everything in the stack panel. So, ideally everything of that Windows XAML Host should have a transparent or acrylic feel. The next thing I'm doing here is I'm building up the additional controls. This is the hard or the leg work of initializing all of them.
I have a couple of text boxes. Then I have a couple of buttons. The buttons I initialized to various sizes and hook up those spring animations to them. Lastly, I just add them to the stack panel and go. So, we can take a look at that code. But first, let's take a look and run and see what happens. >> The test and the WPF text in the main window is obviously WPF? >> Yeah. >> And inside that are UWP controls? >> Correct. You can see it on the big screen here too, but as I move around, the transparency effect has applied to the entire XAML Host and even there are buttons there. >> Nice. Okay. >> You can see I get the animation effect on these buttons as well driving my focus and my interaction to those.
I also wanted to highlight a couple of things about the UWP intrinsic controls that we've made improvements to compare to the WPF. You may want to actually pull those into your applications as needed. One example is the infamous typos. Caps lock on. But if I do, I have a misspelling here in the Word spell-check. WPF doesn't help me do anything like that. >> Right. >> Another example is emojis. >> I just saw that, they want emoji 12. I'm thinking to myself did I miss one through 11. It sure that means. >> I think I missed a few in the way there too, but Windows 10 has a great built-in support for emojis. Some of that does fall three with UTF-8 support for the WPF textbox. But one thing you'll notice here, WPF it's only black and white. >> Right. >> All right. Now, I created a text box down here in the XAML Island with a UWP control. >> Look, you get squiggles with words. So, it has a dictionary. >> Nice. >> If I right click, I get a whole intrinsics [inaudible] >> Is it the same dictionary that Word uses, by the way? Is it a single dictionary, do you know? >> There's a dictionary that's supported by XAML.
I'll have to double-check and follow up where it hooks into. >> Yeah. Okay. >> But you can fix that up. If it knows some words, it'll actually auto-correct for them. So, I misspelled goodbye, and it auto corrected to goodbye. >> Cool. >> The last kind of cool little tidbit I'll show you is emojis, and you have color support inside the emojis. >> Very nice, and you eventually get support for Emoji 12. >> Whenever that comes out. >> I don't know what number WPF support, but I'm pretty sure it's not 12. >> So, this was an example of just showing you that, yeah, I can create more complex controls, build them up in my WPF code, and essentially, take all that code that's in the XAML's Control gallery or out there on the web free VPN, I can code against it in my WPF app. >> Cool.
>> Let me go ahead and close this. We've finished that. Taking a look at the initialize animations is the same exact code that was in the XAML controls gallery. >> Okay. >> But this is really not the way we encourage you to do this. >> So, let me ask a question. Are you using a XAML Island? Because I haven't seen any XAML yet, necessarily. I have seen you do code. >> Well, in WPF in the Designer, you can't mix UWP XAML and WPF XAML. You can't copy and paste. It just won't parse, right? >> Okay. >> You could if you had UWP XAML in a file or some other or even copy pasted into code, you could use UWP XAML parser, and then you could load that content into this control. >> But XAML Island is the technology that enables you to use the controls from UWP inside a WPF app? >> Correct. >> Okay, because they are both XAML-based, even though you can, of course, create controls and code. >> Right.
>> So, I just wanted to make sure that part was clear. XAML Island is the underlying technology that lets you do that. >> It's the host. It's the inter-app layer between an H Wind and the UWP Core Window hosting technology. >> Got it. Okay. >> Yeah, those UWP controls are created in code. I could not assign those to the WPF window elements. That would fail. I've got to assign them to the XAML host control. >> Right. Okay. >> But as you noticed, I had to write a whole bunch of code to initialize just my UI and that's not a very WPF or XAMLy thing to do. So, what I want to show you next is the approach we're working, the approach that we want you developers to be able to use going forward and how to mix more complex XAML UI with their existing applications. >> Right. >> So, in my second example here, I've got the user control that I've built inside of UWP, right? >> Yeah.
>> I can view the XAML there as well. So, here I can do all the rich XAML copy and paste and build a UserControl just like that. >> Right. >> I can build an object models So, that people can code against this UserControl, of course. >> Yeah. >> What I do in my WPF App is now I can go through and in my XAML host control, I can set the initial type name to that user control. So, it's pretty straightforward once you build that. There are a couple of things you need to do in order to hook it up into the project system. Let me show you the project system right now. Ideally, I've got this ItemGroup commented out right now. Ideally, we want a project reference to just work. There's a couple of bugs we're still working through with .NET Core 3 in the project system, So, you have to directly reference the output of the DLL today. Then, to get XAML to work, you also have to out-reference the XPF file.
But that's some temporary hacks. By the time we get to release .NET Core 3, we'll have worked through these solutions. >> Okay, cool. >> So, that's some tweaks to do the project. You'll notice I'm actually using a NET Core 3 project here. >> Yeah. Okay. Does it have to be? >> The answer is, it depends. >> Okay. >> So, if you remember last time, I mentioned that with XAML Islands, there are some challenges mixing different types of NET Core or .NET. You can't bix.NET Framework to consume a .NET Core component, and so a Managed.NET Core component. So, if I was writing a native control, a C++ UWP control, then, yes, I could use it easily in my.NET Framework apps or my.NET Core apps. If I'm writing a Managed UWP control, those will only work in.NET Core apps. >> So, you have an existing WPF app written full-blown Framework- >> In C-sharp. >> C-sharp.
You have a UWP app written to the version of .NET that is part of UWP. You're saying that you can't create that user control and use it in the WPF app unless the WPF app is migrated to .NET Core 3? >> Let me clarify. >> Okay. >> The UWP control, if it is written in C-sharp, can only work on .NET Core 3 applications. >> Okay. >> If the UWP control is written in C++ Native, it can work in .NET Framework and .NET Core apps. >> Okay. That's important to know. >> So, if you are building a lot of components that you want to reuse in these scenarios, you'll probably want to use C++, if your customers are .NET Framework. >> Assuming you know C++. >> Yeah. All of Windows UI is written in C++. >> Sure. >> So, there's some gaps to close in the end-to-end story, but we want windows UI to be usable through XAML Island and .
NET Framework apps and .NET Core apps. >> Okay. >> But a lot of effort's being put into .NET Core and carrying things forward, and so for right now we were worked focusing UWP to interlock with .NET Core. >> Okay. Good to know. >> So, that's what I have here as a .NET Core 3 application along with the managed 1RT user control. The other thing I have to mention here is the manifest file. Right now I'm an unpackaged application, and XAML Islands has a check in it that says I can only work on certain builds of Windows, and the way you tell that in an unpackaged application is set the max version tested in that manifest. If I took this out, I would get an error. I could comment that out and we could save and we'll see what kind of error we get at runtime.
After letter we build here for a second. But XAML Islands usually pops up a dialogue that says "Yeah". The new version 18226 or higher, all right? >> Got it. >> So, let's stop and go back to our manifest, uncomment out that code, save, and rerun it. Maybe I need to rebuild all. >> Where did you specify? >> I think there's something cached. >> Okay. >> But the version I specified is 295. >> Okay. Then, of course, if somebody tries to run this app on a version less than that, they'll get an error. >> Yeah, the app manifesto will fail to load or force it to not to load. >> You probably put a check in your code to check before you get that far like on the startup of the app or something? >> Well, this isn't the app manifest. So, the OS will actually typically fail at to load, all right? >> All right. >> So, it's a similar application.
You can see that I added the acrylic effect with the XAML Islands, going dark and light depending on the background there as well. In this example, I just had a pop-up menu or fly out here, and I've got this similar the textbox here. >> Apparently, we need to put spell-checking on button texts. >> Very good. Good catch. >> Cool. >> Yeah. So, what I've shown you there is the hard way of adding a whole bunch of contacts to your existing WPF applications, or a simpler way by building a UWP control and adding that to your phone. >> Yeah, that seems a good place to start. You've built some UI in UWP app that you want to be able to reuse in your WPF app. It could be login screen, it could be a particular functionality because you have multiple versions of the app. You just want to be able to have that UI in the WPF app.
You just create a user control out of it and use it. Then you need to be a little bit careful about using some of these animation effects because it's going to be in parts of the app, but not others. You don't want to have a form where the buttons on the top don't do anything but the buttons on the bottom do. The bottom half of the form has the visual effect, but the top half doesn't. So, this is some design carefulness you need to do not make it awkward [inaudible]. >> Because obviously some reusable components as you architect your app and you can pick and choose which ones to replace and get some more depth to your application, more interaction, more focus to the close of the app. Maybe, for simpler forms, replace the whole UI with the music control. >> Yeah, you ultimately decide that you just want the whole UI to be rewritten as a UWP and then just reuse the code.
Let me get to the point where that's asked W or demanded W. >> Yeah. >> Yeah. Cool. >> I did have one more surprise for you. >> Sweet, I love surprises. >> Last time, actually in the first toolbox session, we imported an app to .NET Core, and then I was showing you the packaging project work on .NET Core. But when I hit F5, it failed to build, and I wasn't able to truly show it actually .NET Core app running as a package. >> Okay. >> I've built that here in this solution, fixed the problem. It was actually a UX issue and I misinterpreted the error message and so we just skipped past it, but I've got it up and running now. >> All right. Let's see. >> What we've done here in the code is- >> Should I add a note to that first episode that lets people know towards this episode or is that like breaking into jail and highlighting the fact that we messed up? I want to think about that.
>> That could be. We'll see if they notice and then we can point them that like it does work here. >> Viewers have noticed mistakes before ourselves. We give them credit. >> But what I've done here is just simply add a packaging projects to this solution, and if you remember, there's the application note, it points to my WPF Core 3 application. So, if I set this as my startup, it's going to have to rebuild that application, and then it's going to package that application, and then it's going to deploy that application to the windows, and that's going to launch it in debug that application. So, this pipeline takes a second, but cross your fingers if I have set up everything correctly, we will get an application launched.
>> There it is. >> Magic. So, you can see from the icon down below that it's packaged, this kit time because I don't have the right visual assets hooked up. >> Cool. >> We have a .NET Core 3 application with XAML Islands running in a package. >> Nice. >> So, I could submit this to the store or pass around the MSIX package for side-loading in my enterprise or whatnot. >> Fantastic. All right, cool. >> Okay. >> Cool stuff. All right. I hope you enjoyed this part three of our two-parter, Modernizing your Existing Desktop Apps. Any questions, you know where to reach us, and we will see you next time on Visual Studio Toolbox. [MUSIC].