Live Coding C# & WPF - Principal Eng Manager at Microsoft
Let's do some live coding in C#!
View Transcript
got to get Tik Tock going too let's see that should be starting okay well looks like we're online here I want to make sure that I can get chat from LinkedIn that's important for me but uh I don't know until someone starts talking probably it's weird when I set these up and they're not pre-scheduled for some reason I can't see the LinkedIn event it doesn't make any sense so it will stream to LinkedIn but then it's like nope like we're not going to show you so today I'm going to be coding which is hopefully it's never going to go smoothly but hopefully not too not too rough it's going to be wpfs we're going to be looking at C and WPF together um I had a stream on this not too long ago I wanted to do this this morning but I was just too
tired I I had to to give in and sleep in a little bit so I could have a good day at work sometimes it happens so I'm going to get going I'm going to pull up visual studio um let's see where I'm at with this stuff though that's not the thing I want to get I'm going to share my screen in just a second I want to get stuff kind of set up so that I can share um I want to spend a little bit of time talking through uh bindings and and value converters in WPF um this is going to be probably I said it shouldn't be too rough it's probably going to be kind of rough uh I haven't had to deal with uh either of these in a long time and that's okay so let me go ahead and get my IDE
set up here I'm probably going to use uh one of the projects I did before and this is going to end up on YouTube so not not just this live stream being recorded but I am going to uh try to chop up parts of this and redo them so that I can put them on YouTube so if you if you're watching through this you're like hey this is pretty cool I'd like to see a more like succinct version and not just Nick fumbling around then you can check those out when they're on YouTube so okay got to get this thing opened think this is the one yeah this should do okay let me get my screen shared here uh and then I got to bring myself back there I am ises that not sh on my screen what the heck's going on oh you know
what I switched some of my stuff around let me go back for one sec this is annoying um actually man I let me explain what's going on I changed my setup uh for my KVM to be able to go between my setups and it now does not know um what screen to capture cuz technically it's a different screen so I need to adjust this it should be pretty easy though um let's see yeah that's the stuff cool okay sorry about that let me get my stuff set up over here cool it's never that smooth is it okay so what we're going to be doing is looking at WPF code so this is from one of the sample apps that I already put up on YouTube and I want to jump over to the main window here and we're going to talk about bindings a little
bit so let me get this collaps a little bit more um when we have bindings the ones that I did in the the demonstration on YouTube and the stream before these bindings are very simple one-way bindings so kind of boring right so the way that they work is that if you look at either the title of the window or this content property for this label they are bound so there's a binding to the custom label or custom title and you might be asking okay well where are those things I think I put the I made a a view model so if you're not familiar with how that stuff works this is a view model we set it to the data context on the window and that way we can bind to things but it's one way so that means that we're only reading things from
The View model now we can do more than that so we can have uh like the ability to write um the fact that we have bindings means that we can change things on The View model itself and as they change they can uh trigger a notification to the view to say hey you need to go refresh this property so you can reshow the new value there so we can go do that and then value converters just to explain briefly what that is before we start going into to mucking around with some of this stuff a value converter you can see that on these properties I have a return type and it's a string right in both cases there are situations where you may be working with a different type on your view model right so in this case it's a string maybe you have an
enum right and you have a some state so you want to be able to map um some enum value to a string value in the user interface or to a different uh user interface State So like um I think visibility is what we call it in WPF let me just double check yeah visibility and you can see visibility here is collapsed hidden or visible so it is an enum with three different uh states that you could have inside of it so the the interesting thing is if you were making a view model you might want to basically just have like uh visible and it's true or false and actually that's one of in my opinion like the most common thing that we see uh with bindings is like uh visibility uh instead of being Tri State like you see here we just want to have
it is a Boolean and that way if you were to have your view view model my goodness speaking is difficult if you could have your view model set its property uh like is visible is set to false then you would need a value converter to be able to translate that Boolean value into one of these three things because the binding on its own just won't work because Boolean is not the same type as these so um it's going to be I'm probably going to have to hop on the old Google to figure this stuff out but you would do something like binding and then you would be able to uh we don't have a property for that yet but we could say like uh label is visible again this is this is going to be very much a demo and not just uh like how
I would recommend naming things but uh you would make this property as a Boolean on your data context or your view model and then we would be able to say converter and I think the part that's going to be a little bit confusing for me is that um to get resources properly I'm not remembering how that's going to work so I'm going to have to look that up but you would have a uh do they have this one built in I can't remember there I think there's like a Boolean to visibility converter um and I might as well try searching that right now WPF Boolean to visibility converter yeah so it's a thing right so we'll pull that up of course this page is kind of useless it's not really showing uh the use the use of it so one sec it just says hey
yeah this thing exists um converters and WPF cool I'm just pulling up a an example because like I said it's been a little while I want to see how to reference it in the zaml cuz I can't remember anymore the zaml part window resources okay so we need and there's different ways uh kind of jogging my memory here there's different ways you can have these resources like you can have a resource dictionary that you import from other places so you don't necessarily need to have all of this and here's co-pilot saving the day like it already knows what I'm interested in um and it's already saying bullet of visibility converter is not found is that what it's called oh bu into visibility converter really no where is it located I'm trying to I'm scanning on the internet at the same time but that's the name
of it so I wonder because I don't have local maybe I can there we go uh local you can see up here there's this name space it's being imported called local or being assigned I guess so when we had local here it's trying to look inside of this name space this is why I was getting confused um my friends how open use dis vs code I don't can you Reas your question on YouTube there I'm not sure I I understand are you asking how to open WPF things in Visual Studio code if so I'm not exactly sure cuz I only use Visual Studio code for things that aren't csharp because Visual Studio has a Community Edition and I just would recommend that for C things um sorry I don't have the answer to that though um so the name space was conflicting because we have
this local name space literally being assigned to uh our our own application namespace and we don't have Boolean to visibility converter defin there so the the sample thing that I was copying and pasting code from was getting us to Define one of these and we are going to be doing that so we are going to make our own one of these I'm going to use a different example that's not Boolean to visibility um but we will go ahead and start with this one for now just to show it off oh and then that's not how it works is it see I still need to look at the tutorials for this it's the muscle memory is completely gone for me um this you spend so long doing this stuff and then stop doing it and then it's gone so how do we actually reference it static
resource there it is so you can see that when I put this uh EXT this is why like WPF stuff the the syntax is pretty wonky but once you build the muscle memory it comes pretty quick so we have The Binding we have the property that we want to bind to on the data context this is the property on the control and then the converter when we want to reference which converter we're dealing with we use static resource to refer back up to this dictionary that we have here and then we can say like it'll complete for us automatically it knows right so bull to visibility converter and this is just a name you can call it right we just give it a name there we go what's oh and then it's complaining that I don't have uh this on the data context do I
have too many I think that syntax is right I'm just double checking do we got one too many of these there we go wow okay yay we have a value converter attached we do not have this Boolean though so I'm going to go ahead go back to here we're going to set this up and this this is still just a one-way binding okay so it's not uh not fancy I'm going to set it to true to start with so when we go run this we should literally see nothing different we should just see the title uh on the window and a label in the middle of it because by default that Boolean to visibility converter should allow the value true to map to visible okay so that's good news I should be checking the output when I'm running just to make sure that I'm not
messing anything up and then going to embarrass myself in just a moment because it will tell you if the binding is all messed up you'll get a bunch of bunch of red printed to the console but I think if we put this to false we go run this thing now didn't work oh no okay so that's not working but I wonder if the output let's see console output is there no let me rerun this because it might not have had console output yet I don't know why that's not working that's no no bueno I called it the same thing right label is visible oh I wonder no that should work Boolean to visibility did I do it backwards so here's the other weird thing because this isn't checked at compile time this is worth addressing um it's not checked at compile time so you could
go slap any converter you want onto here and then at runtime it'll go oh like that's not working um I'm pretty sure Boolean to visibility converter Maps the Boolean value on a data context to visibility for your view pretty confident now you could write a converter that goes the exact opposite way and does the mapping the other way but this one should do it for us so I don't know why that's not working necessarily I'm just double checking I don't think I need to set these other things but let's go let's go double check and I apologize because binding syntax is absolutely awful um I've never had a good way to like go um make this stuff not look terrible uh but we can do mode one way I don't think we need that no so that's still not working so I'm trying to see
like if if there's something wrong with the binding and I'm actually surprised that the output window doesn't have any errors because if the binding was messed up we should see that printing out here and we don't in the bottom so that's not happening now this is kind of interesting I'm when I hover over it's saying no data contexts found for binding whatever so what am I I might be maybe it's this I wasn't expecting to get stumped this early on mode by the way I'm if when I'm looking down I'm scanning an article to try and and copy stuff over so here I'll just pull this up I I need more screen real estate because I'm using the other parts of my screen to to read at the same time so I'm just going to walk you through what they have going on in this
one right so they have they wrote their own converter right they they wrote this boole into visibility converter and this is what I wanted to get to is to write our own you can see that converter code looks pretty nasty um no offense to the person that wrote this one they're all pretty nasty uh it's just how they look so I'm not a huge fan of all the things going on here but you can see that this window we have right same idea so they just have these resources where we can list and the Boolean visibility converter one is built in I didn't add it it's totally visible um it better be maybe that's what's happening no it completed for me I'm almost certain of it right when I type I just type Boo and you can see Boolean to visibility converter already popped up
like it knows so here's the code for it converts a Boolean value right this is what we want it to do so we're using the right thing good start um let me pull this article back up right so what we need to be able to do not this one okay so you can see on this one this text block right they I'm scrolling along here this is what I mean like zaml code when you put it on a huge line like this soon as you have to scroll the read code horizontally like no thanks but you can see visibility binding is mapped to is visible checked and it our code can I get these maybe side by side it's going to be pretty rough we'll try okay sorry this is It's brutal but I can get my label binding on the screen here and we
can we can double check across right so uh where was it oh it's up here no it's up here sorry that's what happens when I start moving Windows around so we can see binding is visible checked I had label is visible that is on the um the view model label is visible it's public so that's good I'm going to take off path again because we're going to try to make it look just like this one uh I then comma converter is static resource to the key name um which we did you can see up here they have bu viz converter and then they say bu viz converter down here so that key is mapped to here that's all good comma mode they have two-way we don't need two-way though uh oh is it one way it's okay so we don't want one way to Source
One Way To Source would be that our view can basically update the view model not the other way around um so it should be one way we are only reading it now they do have more on here but this is important especially when you have um uh like two way bindings because you need to know when to trigger the update so I don't think we need that stuff so I'm it's all kind of suspicious why why it's not working and obviously it's because I'm live streaming and I didn't practice before I went on camera but now you can you know you know I'm guilty um update Source trigger we can try property changed but there is no property change for that one it's just by default so again I don't really think that's going to fix it so yeah I don't know now what we
can do is we can go make our own and we can put a break point in there and see what's going on because if we put a break point in there and it's not being triggered then we know it has something to do with the binding trigger so let's go do that let's go make our own and we're going to start by using theirs because that's just easier but I don't really like again I'm not no offense I'm not a huge fan of uh all that being collapsed like into one spot so here we go co-pilot or visual studio just on its own pace of this and oh no it's still all crappy and munged in here okay that's fine so what do we got they have a try catch around here I'm not exactly sure why they would do that um especially because this
is worth mentioning again I'm not picking on the person that made this value converter um in fact their converter and WPF works and mine doesn't so who am I to say anything about this code but what we got going on here is a TR catch and you can see that all they're doing is rethrowing it so maybe they just wanted to be able to debug things and put a break point there I don't know but what you don't want to do uh almost all of the time is this um when you catch and you throw a new exception you're basically recreating a new exception with a new stack trace and you've lost the stack trace of the inner exception so if you did need to create your own exception that you were going to throw like this if you did need to you would want
to do something like this but most of the time if you just want to Bubble Up the exception and keep the information like the stack trace and everything you just type the word throw and it will throw the exception that's currently been caught so if you wanted to do logging you could do that and then do throw and it will just allow it to keep bubbling up um that is almost always the pattern I recommend unless you need to wrap exceptions with your own exception types add some extra details in there but that's when you want to pass in the exception if you just do like exception. message you you've lost like a lot of the context of the original exception there are some cases where you want to do that um probably warrants a video it's a uh the context of that is when
I published a video and said like basically what I just told you like you basically never do that there are a couple people that said actually they have some use cases for that hello Collins and the use case that they had was that they have basically like an API boundary so they're making a library or something and they don't want inner exception details of their library to go past the boundary of their API for their library and that's because some of that context is kind of useless for the callers right they might say like why would someone using my nougat package care about some exception that's like 40 method calls deep they don't like they can't do anything with it some people might argue well okay well if they have a bug they have to file with you then they can send that information there's
lots of different reasons why you might or might not want to but generally um you don't want to do this or throw a new exception and just have the message without wrapping it so just a heads up but we don't need that at all we're just going to get rid of that um I very much like having curly braces explicitly um it only takes a couple times or one time when you mess up and you miss curly braces and we can probably transform this into a nice Turner it's a handy shortcut um so if you want to know what I did there uh in Visual Studio Alt Enter gets you this little context menu and you get some refactoring options and I just did convert to conditional expression so this is what's called a tary operator I generally like these for this type of situation
once you start chaining them together it gets pretty gnarly you can do it but I it's not my not my favorite thing to recommend to people um so there we go we just cleaned up that code a lot in my opinion and we can go do the same thing down here this is just what's going on here there's a lot of they're casting Okay the reason so the convert and convert back methods are on the value converter itself right so they have to implement the them and these things are designed to basically work with any data coming in and and going back out that's why you see object used here because we can basically assign anything to an object and then in our the context of our converter what we need to be able to do is cast it to being something usable because just
object if you're not familiar with C right so if we have value here like you can't do much this is the base type that all objects in C have you can get the type of it you can check if it's equal to something that's about it so we need to be able to cast it and that means we have to make assumptions about what's coming in so like you can see here they're saying we know if someone set this up properly we know that value will be of tight visibility so there's directly casting it which is fine um if you want to be defensive you could you know use is or the as operators and check it and maybe explicitly throw an exception saying hey you have this converter in the wrong spot you can do all sorts of things um but I'm just trying
to see what we got here um and we can just basically return not this if my Boolean logic checks out so this says if it's not collapsed then we return a true is that what we want though I don't know why don't we just say if it's visible like I think that's what we want could be wrong uh I don't know if these needed to be updated because the and a lot more like modern. net you'll see that they have nullable on here so I don't know if value coming in can be null if that's the case we will be casting a null value and doing equals which will throw an exception so I like to reverse this type of stuff around so give me one sec that's going to look like this and that way this part is never going to be null we
got there's too many here we go too many parentheses just like people you know wrapping parentheses around everything so this should work I think um now what that means though is we want to give this a different name so we know we're we're working with it I'm going to put some break points in here hello X coding from Twitter good to see you thanks for joining again there familiar faces that's so cool I haven't been streaming for long so it's nice to see people coming back so let's call this n cool converter okay we're going to go back to our window and just to call it out again I did put break points here so that when we go to run this we can see if this is not triggering we know something is suspicious with our binding if it is triggering I don't know
what we're going to do after that'll be interesting to see why it's not so okay this is where this thing up here we're going to change that it can't find it but now again just to show you what I'm doing you can see the squiggly that's under here it's saying nyx's cool converter is not supported is not a supported Windows presentation Foundation project like it doesn't it doesn't know what this is but if I do alt and enter again it's the same thing as going rightclick and then quick actions and refactorings so it's saying hey I I think you should try and like use this name space and that way up here remember what I was saying before you might not if you just joined the stream but when it had this local name space up here it can't find the Boolean one but now
it can oh and it's saying it can't even find it there give me a break is it not in here yes it is I don't believe you it's totally there oh is it that no this is right let's see if we can do something else um this doesn't feel right um WP WPF playground I'm just going to put a name space around this oh man okay give me a break so I'm just pulling it out you can't mix the file scoped ones like that's why it's complaining because it had a file scope one and then a name space around it it's like hey like you got to pick one or the other so we're picking this one uh X coding we are working or I am in embarrassing myself trying to use uh bindings and converters in WPF should be something that's very trivial and
it's just not working so you guys get to see me struggle through this um and what we're doing is making our own converter I just copied some code off the internet modified it a little bit to hopefully make it a little bit more readable uh let me go put this stuff onto new lines so it's more vertical that way there's less chance of stuff getting cut off but yeah we have this code here that should be able to convert between booleans and visibility and we can see it's not finding it even though it told me to go do that so what I'm going to do is converters and then I got to put converters here well I guess so yeah there's no embarrassing en coding Yes except for that I did this for many many many years that's the embarrassing part is like this is
something I did for a large part of my career and I'm like why isn't this working so no it's okay I'm not I'm just I'm just joking around not not being too hard on myself but we should be set up again this is making me feel nervous I don't know why do I just have to build it oh my goodness okay I know what's wrong um someone's G to call me out on this this is so this is this part's embarrassing because it's even worse than WPF not working okay so here's the context this is hilarious um I can laugh myself now so this is a code project from my plug-in YouTube video I'm working with plugins right now and I'm running the sample application and I'm not rebuilding the plugin so this entire time every time I've run the code it was not it
was never building the plugin so what I just did so two things triggered in my head I said I think I need to rebuild this because it's weird that it's not picking this up right but so I rebuilt it and then it said okay yeah that's good like it found it and then I said wait a second I've been running this thing when I press F5 and run it it should build it if if it's supposed to but there's no dependency on the plugin so normally this wouldn't be an issue for me because I always use I shouldn't say always I generally have a bit of a cheat when I'm making plug-in style applications and that cheat is that if I don't need to do anything complicated in my build system because if you're making an application that's plug-in based usually you want to be
able to configure which plugins you want to have dropped in say in your build pipeline so you can dynamically configure f it or you could have it such that you can deploy those separately so many different circumstances but the point is they're generally Dynamic and you're not building them all in Visual Studio but the reality is for a lot of the things I develop like if I'm working alone especially and I still just want to have plugins to physically separate the pieces I'm building I do this little cheat that I'll show you and i' I've made videos on this by the way so if you want to see those you can just search for plugins on my YouTube channel and you'll see a video that talks about this I basically reference the projects directly which you're going to say Nick well that's really dumb doesn't
that break all the rules the answer is yes it does but it still gives me that nice physical boundary that I'm looking for but the beautiful part about this is that visual studio just handles all of your dependencies for building and putting them in the right spot automatically so to prove it to you I'm going to undo everything I just did and show you that this worked from the beginning okay so ju I I want to make a point of this because it's it's so funny to me um I'm going to comment out that converter boole to visibility converter okay we're still going to call it NX cool converter we don't need that uh converter's name space up there so the converter we just made is not going to get used at all yet now we're still going to reference Nick's cool converter here and
by the way if this doesn't work now I'm I'm going to be like triple embarrassed but and I don't even think we need these because I think like I was saying when I was walking through it I think that's a lot more important when you're doing like two-way binding and we're not there yet so we got this label we already had a binding to this custom label property on our data context and then we added this other label is visible property now we're slapping a convert onto there it's called nck cool converter because it's this key from this resource dictionary up here and it's using the builtin Boolean to visibility converter to prove it I'm pressing F12 you can see that we've gone to the definition of it okay Moment of Truth and let me do one more thing to to prove it here okay
I'm going to set this to true that's all I had to do the first time by the way if I would have changed this and it didn't take effect I would have said wait a second so let's run this we should see that it's visible and the text changed so we know the plug-in is now updated in the bin directory good to go let's go ahead and make it not visible come on come on there we go we have succeeded in making usage of a value converter we've done it sorry for wasting I don't know how long have we been streaming now sorry for wasting almost 40 minutes of your time but I think there's a good lesson here right and we got to debug stuff so if we can reflect on what was going on right the point was that I was changing if
you sorry this is kind of small on the screen I assume but in the solution Explorer my sample application is my core application we were changing stuff inside of plugin one plug-in one was never being built until just now when I went to show you guys so because it was never being built it didn't matter if we were changing code we're never going to run it so now we got that let's go use our custom one let's try it out okay so we have NYX cool converter whoa that was weird what just happened so strange okay I'm going to comment out this one oops and put this one in so this should do the same thing hopefully but um the thing that I want to show you is that I put break points in here and I want to show you inside of the converter
so let's go run this and you can see already we hit a break point as soon as this control gets shown and it needs to figure out what state to have it's going to ask the converter I have a value sorry this is so small on the screen but uh the value says false when I hover over it so we know it's a Boolean right that's why here we can safely cast so if I step over it The Bu value now is false because obviously we just casted it but then we can go make a decision about what to do right so that's why we say if the bu value is true we're going to make it visible otherwise we're going to collapse it and like I said it's TR State you could go do you could change this to be hidden if you wanted
but in my experience collapse is almost always what we use and I think that h i could be wrong on this because it's been so long but I think when you have collapsed it actually allows the uh the layout to to restructure because you're basically making the control like essentially infinitely small versus just uh hiding it so I think that it allows a lay out to change might be wrong on that though so we have that and we run and it does what we want right so it returned uh collapsed because the state was false but that's how you go make your own converter so that's kind of neat but what I wanted to basically do in this stream is get to something a little bit more advanced it just took way too long to do the most simple part so um someone had asked
and this is going to go like I said it's going to go into a YouTube video so um I wanted to talk about like the the reason I'm picking up WPF to walk through some of this stuff is that I want to show dependency injection plugins that kind of stuff in in different things related toet been showing it a lot more in asp.net core because tons of people are focused on that it's great um but there's still people building a WPF I'm not going to Maui yet um I will end up doing that at some point but I'm I'm going to be building something in WPF as a starting point and I wanted to show that some of the patterns that we get set up with in WPF in my opinion don't lend themselves well to dependency injection I find this pretty typical with like
a lot of UI Frameworks for some reason um just seems like I'm kind of working against them a lot of the time and that's because I'm being stubborn but that's okay so what we're going to do is make another converter um this time we're going to change it to be uh we're going to go we're going to make one that does numbers to Strings or we could maybe we could do it the other way but let's do numbers to Strings and or we could do maybe we could do a a two-way binding do we want to try that that could be fun might need a text box though I don't know let's go ahead and stick with the one way for now because I want to get to the point where we try doing some dependency injection with this stuff because it's not it's not
that simple unfortunately so we have Nick's cool converter I'm just going to copy and paste this one and I'm going to call this one just visibility converter but nyx's cool converter is going to do something different now um it's going to take integers actually we'll make it take because this might be more interesting we can make it take in floating Point numbers so we'll make them doubles okay so when we go to convert we will get a new numeric value that comes in and then we're just going to return a formatted string oops look at that co-pilot can we do maybe with some decimal places is that how we do it I can't remember anymore why isn't co-pilot helping me with that sure let's try it um I think that's how you do it it if you're watching this and you know how to do
string formatting for decimal places let me know we're not going to do convert back though we're going to get rid of this okay so it's a one way we don't need to have a convert back just don't need it so we're not going to use it now what we're going to do instead is go back to main window we're not have label as visible anymore we're going to have we're going to have a cool level I'm just making stuff up I'm sorry I don't know uh 42.1 337 and that's going to be the cool level and if you recall if the formatting I did works properly and it probably won't because I can never remember the string format stuff um we should see 42 do 13 and it will have the the string in front of it I think it said like this value is
so that's cool but now we got to get cool level on um and actually we don't need custom label anymore so custom lab oh I got rid of the wrong thing we don't need custom label anymore because we're going to use that label to do that for us so n cool converter we're going to get rid of the visibility so we're not changing that around we're just going to leave it visible by default and The Binding will go to nyx's what do we call it cool level and then converter remember the syntax static makes cool converter so this should in theory work and the idea is that we're going to use Nick's cool converter which is the one I just updated to do double value use being formatted to Strings and then we will set that in The Binding right here and because I did
that Visual Studio trick to make it a dependency it builds and you can see it worked the first time so again the value converters allow you to bind to things and you can use them with two-way bindings as well but they allow you to bind to things and then convert the data type that's on your view model to the thing that your control needs so hopefully that makes sense they're used all the time and that's because a lot of the time I shouldn't say a lot of the time depends on your preference I guess um you might say like why doesn't your view model for the visibility thing instead of BU is visible right like why don't you just have visibility why don't you just bind to that directly you can you can absolutely do this um the reason I don't do this um I
got to be careful how I say this because I I actually I do this but it's it's another technique so I when I was building WPF applications I actually separated things where this is going to sound really bizarre but you could basically have the entire user interface without showing a user interface like it was all done in code so so we had essentially separation between the zaml views and their view models so that was an entirely different layer that we could put onto it but we could literally go run the user interface headless which sounds like that doesn't even make sense and the patterns that we used basically meant that we could we could swap the entire front end which meant that you can't have like system Windows visibility in there like you can't have that bleeding through into uh the backend part I shouldn't
use back end because I don't mean like a server but like to the lower levels that we had abstracted out um and yeah like was it overly complicated maybe but um we did this the context here is important we did this because we were we were mixing Wind forms and WPF and this gave us the ability to to literally build out the user interface uh we could test it headless which was super cool that was one of the biggest things we could test it headless and then we could mix Wind forms in WPF in a way that we didn't have to think about it so there were reasons for doing it I know as I'm saying it you're probably like why would you ever that's way more complication that you ever need to think about but there's reasons for stuff so that's I would end
up having basically like two view models and converters between them so this is the direction I'm going to start heading in here to explain why some weird stuff happens with dependency injection and value converters and all this fun stuff okay in the YouTube video I made I explained that one of the tricky things a lot of the time in WPF is that you don't have this type of pattern that I'm highlighting this is pretty typical like in a lot of C code where you make something and you pass the dependencies in through the Constructor but a lot of the time controls in WPF they're anticipating that you have a parameterless Constructor and then you can mutate them and what I mean by that is just they're mutable you can assign values to the data context and you would do this kind of thing on the
outside so you would go make the window and then assign a data context to it I just don't like doing that I like having all my dependencies pass into through the Constructor and having things be as immutable as possible that's generally a goal I have in a design philosophy again doesn't mean it's the right way to do it and anything else is wrong it's it's a design philosophy that I like to have and I generally will fight against Frameworks that don't allow me to do it I do the same thing um some of my earliest YouTube videos were on Unity like the C platform for making games and the same thing there in unity it basically makes you fight to go have dependency injection done in what I would consider nice ways so it's doable but like you feel like you're fighting the system so
we're about to see how that's going to happen here um okay so I'm going to make this very contrived so I apologize but it's going to be to prove a point so we're going to get rid of the visibility one um what we're going to look at is that we need to have a dependency here so our converter needs to depend on some other class or interface and that's going to make things complicated because it doesn't work uh but I need to come up with a good example and maybe I shouldn't even say good that's setting the bar High I need to come up with a example that will allow us to to see that we need a dependency in here so um Let's do let's do this this is going to be really silly but I'm going to have public sealed class string formatting
helper okay and I'm going to do public string format double int decimal places oh was co-pilot going to do that for us come on co-pilot wow that's super weird cool I guess so we got that and we're going to say that this thing needs this dependency okay so private read only um shortcut that I'm doing here this is really handy sometimes if you know the dependencies you want you just uh list them and then you can do this kind of thing generate Constructor so again to show you what I'm doing there just over the field uh and actually can you do it up here too yeah let's do this one so I have a field it's not assigned if I go up to my class name I'm just doing the refactoring menu right so Alt Enter and then you can see there's generate Constructor dot
dot dot press that and you can see it's like hey you got these fields builds they're not assigned do you want to make a Constructor with that and I'm like heck yeah I do so there we go um when you have one it's not so bad but if you know you have like five dependencies and you want to go make a Constructor really quick that's a good way to do it um there are I'm just going to show you guys because I think some people lose their mind over this stuff but uh I am very keen on primary Constructors but only only when I am doing dependency injection which we're going to do so it's a good good opportunity to do it people don't like this because it's not readon and I agree like that makes me kind of upset but what we're not going
to see in this code is this if someone were trying to reassign one of these like that would be a like what the heck are you doing kind of situation because it's just a dependency that we don't own so the fact that you can write that because it's not read only I understand like that doesn't feel good it's just the least of my concerns right now I'd rather have that declared on one line versus how many is it three spots right one two three so I'd rather have it just like that oh it's funny um I press the keyboard shortcut and my the Tik Tock live studio on desktop steals your keys by the way I don't know if if folks watching this have ever if you stream on Tik Tok or anything like that um I pressed control I pressed something in visual studio
and Tik Tok was like hey are you trying to make a poll and like stole my focus like I'm not trying to make a poll in fact I'm not even using I'm not I'm not using your application why are you taking my keystrokes suspicious okay so we have this converter set up we have a dependency that we're passing in let's we're going to see pretty quickly it's not going to be too great so VAR uh formatted do that come on one sec apparently my Tik Tok audience doesn't love my streams so Tik tok's like hey are you like you okay yeah I'm okay send some viewers over um so we have oh did I I'm silly no one in the chat called this out guys I need I need the value that needs a format hey co-pilot here we go so many so many little
embarrassing moments for me today so now you can see we're passing the numeric value into here I'm saying two decimal places to change it up let's make it three so we can see a difference but the problem is that it can't make this when it goes to run it and just to kind of I want to get you to think through this right like we have a converter that we just gave a Constructor parameter and actually I don't know if uh the tooling is more advanced and or we used to get this before but have a look at what this tool tip says type NYX cool converter is not usable as an object element because it's not public or does not define a public parameterless Constructor it knows it's like you can't can't do this we don't have what we need don't do this it's
not going to work and we're like no like screw you we're gonna make it happen um hello on Twitch hi so yeah you can see object NE reference not set to an instance of an object we can't we can't get the value converter um if I bring this up a little bit oh there's no good trace there that's kind of silly but um can we see it here not that it really matters but okay I just wanted to see if we could see the stack trace for it and you would see like it's like I can't do this the value converter is not set so it's unable to make it therefore it's null therefore when we go to assign it to The Binding it's like nope we can't put a null here so that's too bad but that means for this type of thing we're
out of luck it doesn't work so your options are you don't do it which sucks and maybe you do something like this right this in this example this works this is simple this could have been a static class then you don't have to worry about this at all like it's a contrived example I'm just trying to show you some of the limitations but if you imagine that you needed a dependency passed in you can't do it I just made the most horrific programming language tier list that's completely biased on my personal preference well as long as C's at the top and it's the only language it's not such a bad list um so this is an option but and again like in this case I know it's very contrived so we don't really have to worry this totally solves the problem but if this wasn't
good enough for you based on your dependencies that you needed to work with like one situation might be that you're like hey I need to pass in something where I can swap the implementation I want to be able to unit test that I need to put a mock in here for for whatever reason this is maybe not a great reason for it but if your dependency needed it this doesn't help you because now you have the real C is far away from being on the top well you might might be on the wrong stream um so you can't pass in through the Constructor which is unfortunate but what can we do right so if we're thinking about dependency injection like I was saying you would probably want to have it on the Constructor we just saw that we can't do it it needs to be
parameterless so what you could do is and this I'm going to show you this it's not what I like so it's not what I like but this will work I've had to do this in zamarin before when zamarin was still a thing and it's a it basically we're going to do a service locator type of thing and it's going to use static I don't like it but there are situations depending on where you need to do this where you're like this works on my application so I'm not here to tell you no I'm just showing you options I will explain why I don't like this as well but I want to show you that it does work okay so you could do something like this okay so we're going to have this kind of thing going on um we can do NYX cool converter with
a parameterless Constructor now what we need to do and where this gets really ugly is a static service locator so we'll use the service provider and this needs to be static those who said c is far away from being on the top don't use Microsoft product bro yeah I mean to each their own like you can use whatever language you'd like I'm not here to tell you otherwise but it's it might feel kind of awkward that you're on a c stream it's okay I don't mind um you're welcome to hang out if you want to chat about other languages that's cool too but that's not what I'm showing you so we need to make this public static so there's a whole bunch of stuff going on here that I already don't like this word in particular I don't like using this word this static word
it's not that it's wrong to use it's just that I see it get used in so many places where I'm like oh maybe that's not what you want it to do um it starts to you can start to go down a slippery slope when you start putting static everywhere and it's because people start attaching state to static and then it's like it's just a it's dangerous so it's and sorry I should I should add more context there it gets dangerous because some things don't know where the state is being modified and because it's static and anyone can go touch it you have instances of things that can see the static State and it just happens to change whenever someone wants to change it like it's not it can get pretty messy pretty fast so we're going to do this I'm sorry uh we could do
like if you wanted to make this better you could do internal maybe maybe that helps um so we're going to have someone set this from the outside but then we're going to do come on co-pilot thank you very much what why does that work oh do I not have um come on now I think I need to have this included here one sec I keep forgetting that when we're not in asp.net core we don't have this kind of stuff alongside it so what I'm doing is I'm just adding the uh dependency injection extensions onto the plugin project because that's where we're working right so now you can see this does work so I was just missing a nougat package reference that gave us this extension method so let's talk about this because I don't like it but it's uh it's one way you can solve
this and to make it a little bit better throw withn okay so you can do this do we even need that name of I think you can do that so we can have a little bit of protection like hey you got to go set this thing but it it means like there's a whole bunch of stuff that's not great right so we can now resolve this dependency from a container so if we go put this thing and actually because we're I'm going to give you some more context we're using screw Tor and I service collection so combined this thing will get registered and we will be able to resolve it automatically which is super cool uh if you're using autofac or other dependency injection Frameworks even just I service collection without screw tour um you would need to go register this class somewhere so that
we knew that we could res resolve it but this will get handled automatically now uh just to briefly show you where that is because that was in a YouTube video this code here will scan all the assemblies that we have from our bin directory so that's what this codee's doing it will register all of those classes from those assemblies as the classes themselves as well as the M uh the interfaces that they Implement and it will give them a Singleton lifetime so they will only create one instance for the lifetime of the application uh wi Wicky I have experienced coding in C from unity and I want to try using Advanced C built through zand WPF or maybe asp.net but still confusing with interface can you give me some tips about that uh yeah can you um when you say it's still confusing with interface
can you elaborate a little bit more because I I saw that you mentioned um that you know through zamon WPF or asp.net so it kind of like there's a handful of different text Stacks you could look at but it's still confusing with interface do you mean like when I'm when I'm declaring an interface like like something like this like is this the confusing part for you or um can you clarify a little bit more in the chat and I will try to answer as best I can just let me know um so that thing is going to get set up coming back to here right this is going to get registered and in fact Nick's cool converter is also going to get registered uh not only as a I value converter but as the class itself so both these things get set up what's missing
though is that in order for this to work we need to be able to do I don't even want to do it here like I don't know a good spot to do this and it's going to be cheating as soon as I do this let me explain though Nick cool converter what oh it's internal I protected myself okay jumping around a lot here so don't tell my boss I should be working but I'm watching you well that's okay I appreciate it um plus this is this is education right so it's good for work yeah okay good uh oh like using I service collection or value converter how can I know when I'm using that uh I'm still not totally sure I know how to answer your question with Wiki so let me I'll try to I'll explain maybe something very basic okay so let me
come back over here when we're making objects and stuff in C uh I'll start just very basic so you make a class I'll go even more basic you start with an access modifier so public means that we can see it from within our own uh project that we're working in and people that have our project can also see this so you can do public there's private there's internal there's a whole bunch of different access modifiers so you start with one of those or you can just go right to class class is the keyword that lets you define your own type and it will be of an object so NYX Nyx object so this is like the most basic class that you can create in C okay so this is your own new type great doesn't do anything though um but an interface if we go
do um right this the difference between these two things is this is an implementation so what's a good way to say this uh let me scroll up a little I can make a new instance of one of these okay I can make a new one because I can say this is an object and I want a new one of these implementations so that's what I am able to do but what I cannot do I can't can't do this so the way that I'd like you to think about this and I'm going to expand on these a little bit more in just a second but the way that I'd like you to think about these things is this is an implementation of an object and this is just the API now you have a great question do I need to create an interface for every class
like headers and C++ absolutely not which is great so to give you an example we're going to do uh public void cool method and it's going to write to the console okay so we can do this okay so you can define an uh your own type with an implementation for methods and stuff like that so this is like you don't need this interface at all at all doesn't have to exist we can still have our object you can still call the methods on it so you don't need headers or anything like that but what's what's different is that the interface gives you an API that you want to call so you can't make instances of the interface but the perspective that you want to take with an interface is like I don't care who made this thing right I don't care what it is I
just need to know that it has these method calls on on it hello trec if that's how you pronounce your name good to see you here so you don't need to think about instantiating these you just from the perspective of using an interface you're just saying as long as it has the method call available I don't care what it what the implementation is and to show you to continue this example what I'm going to do is I'm going to say we have cool method on the internet face good morning rambling geek good to see you back thanks for joining so what we can do is have this right so now we're saying that this NYX object implements this interface right and if we took this code out it's going to complain it's like hey no no no you said if you're implementing this interface you
must have all of the methods the properties whatever you define you need to have those as part of your implementation so you have to do that so now what we could do up here is like this part stays the same but what's kind of cool is I can do this or I could do right so when you're reading this code you know that object 2 is equal to new NYX object like literally you can read it here but when you're looking at object 2 it's just INX interface all that we know is that we can access those things so public void some other right so I got to put something on here okay so if we look up here we can see some other method on here right so these two methods literally exist right here we can see both of them but we said
INX interface only has this one method so from the perspective of this thing you're like I don't care what the implementation is I just want to work with an interface it's just an API contract you can put whatever you want behind the scenes and that's why we can't actually see some other method here you can see that the autocomplete the Intellis sense is like there's nothing that has that method and that's because object two is just INX interface now to go one step further before I go back to the the other stuff is like I want to get your username so we're going to put it in the in the video so WID Wiki okay so I want to get just some other method name on here so WID Wiki also implements INX interface and again the only thing we care about from INX interface
is that it has cool method it's the only requirement so if we go back up here right we have that uh Let's do let's just call this the interface just to make it a little bit more explicit for what's going on so and we'll call this we're going to do the same thing down here thanks co-pilot you always know what I want okay so we can put Nick's object here see how this code still works to make it even more obvious uh let's change this to I want to give them more unique name so it's less confusing but I realize as I'm jumping around it might be confusing so so we have this method right we can do something with interface and we can pass in NYX object right this code only cares about something it doesn't matter matter about the concrete implementation it doesn't
care if it's Nick's object it doesn't care if it's WID Wiki object literally doesn't care as long as it meets this API contract and that API contract is that it has a void method with no parameters that's all it cares about now I'm losing my voice keep it together Nick okay so up here though we can pass this in and it's still works so okay so using interfaces limit the object method to be exposed that's one you can use it for that for sure um the the way that I like to think about it is if you think about the context of this method right so if I know that I just want to call something maybe we can come up with something better than cool method um what's a good way to do this if I start giving these things better names it becomes
more and more obvious okay so let me do this Tik Tok saying I don't have audio there we go it's lying um and we'll do these better names one sec oh no okay so gave these some better names now let's look at this code so this stuff is very obviously like calling the concrete things when I say concrete I mean like the specific implementation sorry I'm realizing my terminology might not be helpful but if you think about this code here do something with a database right it's amazing what naming can do to try try and clarify examples so you can see this code here is like I literally don't care what type of data base you're talking about I don't care it doesn't matter for the work that I have to do so your your question or comment was like is interface to limit the
methods being exposed that's one thing you can do but I also look at it primarily as I'm writing code that only cares about working with a certain API contract and I don't care about the implementation details and that means like in this example one day you could go head and swap from MySQL to postgress and see how this code didn't have to change like that that's the power of interfaces in my opinion you're absolutely right though you can use it to scope down things sometimes I will chain together like a readon version with a mutable version of objects and you can limit scope so there's lots of things you can do that way but I hope this example was helpful to see that like we can swap literally I pass in a different implementation but this thing doesn't know as long as you can connect
to the database I don't care what database you are let me know if that makes sense though amending that example so it better make sense you get a multiple classes implement the interface which then Implement different methods of for different databases yeah you could do that for sure so to repeat that then you can have multiple classes that implement the interface which then Implement different methods for different databases yeah so the the example that I just deleted because I was moving on uh no it's it's good to to kind of round that out but yeah you can have uh generally you would have more than just the one method and you might implement the the pieces of that specifically to postgress or specifically to mySQL a logger is another good example even though it's simple I want to log to where the console I want
to log to Azure I want to log to um I don't know pick any other service that exists but like you don't every time you go to log you don't want to write code that says like log specifically to Azure do this funky thing um you just want to say like log this stuff like I don't care where it's going someone who's configuring the app will go set it up with the right implementation but everywhere else just has an ey logger someone will give me the right one that's not my job to know someone will give me the right one so hopefully that helps thanks rambling geek for for jumping in there too okay so the thing that we were looking at before that good question thank you for asking that the whole point of me streaming is to answer stuff not just blab at
you guys so um the challenge is that we got to set this thing and I for folks I got a disclaimer for folks that are joining the stream I don't want to do this just showing you one way that can work with a service locator pattern it is an anti- pattern it is an anti- pattern but we cannot pass things in through a Constructor on a converter it's just not allowed you cannot construct these things with parameters hi John Murphy welcome so what we're trying to do is say that look like so yes anti patterns you know try to avoid them you may have situations where you're like look this will solve my problem and if you understand the limitations and you know the constraints like then do it like it's there's no one you know if the code works the code works like it's
hard to argue with working code but you need to be able to understand the tradeoff so I want to get this working kind of walk through where the challenge is the challenge that we're facing right now though is that we got be able to assign this thing and because I'm doing plugins this is really not going to work and I so I apologize that this is a very very bad example to try and make this thing happen the reason it's not going to work is that for us to be able to ever use the cool converter what we must do I think I found a solution I might come back to this but what we must do is we have to set this static property we have to say to enable the service locator pattern and I'm giving you the thing that lets you locate
services and we're doing it with this nasty static property but we need a spot to do that because this is internal and because it's within a plugin right it's really hard to see the text but WPF playground plugin one we're in a plugin our core application should not see this thing directly it could because I'm cheating and we added a direct reference to it but I don't want to break my rule trying to keep these things separated for a reason so how do we make this work now I don't know you can do this in autofac and I don't know if you can do this with I service collection but but what we could do is maybe extend the service locator pattern and I know it's gross I know it's gross but hear me out um we could do this this might be an option
and then we would say I just so what I don't know with I service collection and I want to talk about autofac for a moment I used autofac extensively for my dependency injection uh it does allow you to basically locate the container like the container itself is registered onto the container so you can ask for the container from the container to do things with the container yo dog I heard you like you know the meme right so I don't know if we can do this we're going to find out super quick if it just blows up then we know we can't oh oh we can do it very cool so you can see or maybe you can't because the font is very tiny let me get this out of the way too that's a lot of noise service provider is populated with an instance which
means we can set the service provider which means our converter works so I I'm just going to be honest with you guys I hate this uh I don't like this and I I want to explain why yes it's working it's like it's just breaking a lot of like principles that I'm I'm not a huge fan of um I am a fan of working code though so if we were in a pinch and I was like I don't have a better way to do this and we have to ship to customers I would say hey this works get it out the door and let's brainstorm after before we go too far with this pattern because if we're going ah we don't like this ah it works though customers don't know they want working stuff it's up to us to come up with better Solutions so that
might be an opportunity where I would say this is going to be conscious Tech debt that we take on okay so this could be one iteration we just prove that we could basically do service locator pattern effectively resolve this is the difference we're not injecting dependencies into the converter we are resolving them by pulling them in it's it's kind of weird to think about but instead of having the thing automatically get resolved and passed in the dependencies through the container we're literally inverting that pattern and we're saying hey I'm new here and I need these dependencies give me them like let me pull them in instead of being passed in so not not a great pattern in fact it's an anti pattern so this does work and so to talk about the things I don't like one is that not only did we have to
go do this and set up the service provider if you had more converters right not just NYX converter we got the the John Murphy converter right we got the the rambling geek converter and any converter we add that needs dependencies are we going to have to do this kind of thing like basically I like thinking about things if it's a oneoff and I can live with it sure but converters with dependencies the the likelihood that you can have more than one of those means that I need to come up with a pattern that's going to let me extend that to basically n and not be like I hate everything I'm typing that's the goal we don't want to hate everything we're typing if we can help it so in this particular case I feel like I'm going to end up hating everything I'm typing if
I have to keep doing this and the fact that now my main window has the responsibility to go wire these things up like it it doesn't feel good it just doesn't feel good to me um it's kind of like we're we're breaking a rule with you know doing the service locator and then we're like breaking it even more just it's not great um what's another way that we could do this though I know I don't know if um let me try to get my train of thought here autofac has build registration callbacks and we could in autofac we could say hey the container has been built go run this logic now that it's been built and then we could basically wire these things up to do that basically assign the container to allow us to do the the not so great service locator pattern so
that's one way but I think there's a way that we can avoid this all together and when I say I think there is I mean I know there's a way we can avoid this all together but I'm probably about to go embarrass myself again because it's been so long so this all comes back to WPF being a pain in the butt from some design decisions and when I say that I mean the people that put WPF together are obviously extremely smart people and they made tons and tons of decisions that they had to debate so I'm not I'm not saying they did the wrong thing like I am not I am not the right person to criticize all of the work and effort and thought that went into WPF so clearly they've done an amazing job but the problem all comes back to this stuff
the more things and this is like this is sort of like a meta point that I'm trying to get across in some of these videos I'm putting together um the The Meta point is that the more stuff that you're putting into zaml the more of a pain in the butt you're going to have if I can say it that way it's because zaml is making stuff easy for you it's like magic right the fact that it could do the thing with the converters to get them made for you like that's magic but it's magic because it's using reflection and code generation behind the scenes and it has limitations like it needs a parameterless Constructor and then it falls apart as soon as you need to deviate from the constraints so for me telling me hey you can't have dependencies passed into something I'm immediately like
don't tell me what to do like I I pass my dependencies in and you're not going to stop me so it all comes back like I said to having stuff to clar it in here now what we can do and this is again I'm probably going to butcher this I think we can do away with this okay so we're not going to have resources for the converter and we might even do away with this so we'll get rid of this I think if I recall correctly we can stick with this B in but we have to do something else now we have to give this control a name and what I'm going to do is go back to this code uh yeah it's in main window I'm also going to get rid of that and I think if we do cool label cuz now we
can we we've given the control name we can access it in the code behind can I do there set binding is there no get binding oh get binding expression oh man oh man it's been a long time okay let me I'm GNA bring up my my trusty friend Google again okay so what I want to search for is WPF um modify binding in code because that's what we need to do and I am searching for it right now so I apologize this one this example is telling us not to modify The Binding but basically just to go make it so maybe let's let's take that advice okay so there will be no minding oh you know what we might be able to do it but okay I'm I'm remembering dependency properties now because this is a weird thing dependency properties are the thing the properties
on the controls that support binding in the first place so um I'm just copying some code here you could have asked co-pilot in line yeah I guess I could have I could have asked co-pilot oh you know what though I can't um I forgot this from another stream so I've been streaming and making YouTube videos from my gaming rig because my laptop uh this is a big uh flop of Alienware my Alienware laptop which costs too much money apparently has a problem just in general with the audio card just it just is bad and it's like a known thing apparently with these laptops and I spent like I don't know about eight plus months like fighting with my microphone setup and stuff I was spending tons of money on Amazon trying things out just turns out it's a crappy audio card so on this machine
I I forgot I don't have uh like co-pilot chat set up I don't think I did it no so when I hit the shortcut it's not coming up so um but maybe I can do it with comments this might be fun let's see if co- pilot can do it co-pilot come on this is the old way of doing things like what that's not that's not what I want is that it wow really rambling geek I don't have uh I don't have it in my my menu and it's because I don't I just don't have the extension installed on this on this instance of Visual Studio but fortunately it just did it all for us which is cool because now we can make this a dependency that we pass in who thinks it's going to work the first time I don't so um I don't but
hopefully let's have a little look here so we're saying that we want to uh the parameter here is it's called the path of the binding so we want it mapped to the cool level which is here give it a converter the uh the parameter here is called the dependency property so content is the labels property so it's a static thing pardon me um so you can see like dependency property is an object it's not even we're not saying just to be clear what we're not doing is saying like we're not doing that we need an instance of a dependency uh dependency property and then we give it the binding so doing this what this should enable us to do is resolve this thing from the the dependency container which means which means we can go back we can get rid of this we can go
use those nasty primary Constructors and it's glorious now because we can break this rule we can pass in things into the Constructor because we're not getting WPF to go build it for us come on please work oh oh no oh oh yes one second this is actually just me being silly order of operations we actually have to build the user interface before we can use it and it works Redemption for the 40 minutes in the beginning where I butchered this but the muscle memory's got to come back so so what we were able to do just to clarify is we avoid waited using zaml to do this binding and you might say well Nick doesn't that defeat the whole point of using zaml I mean maybe not the whole point but um it's a tradeoff if you don't need to do fancy things like this
you don't care about dependency injection this stuff doesn't come up for you then like don't worry about it that's totally fine but I found in a lot of my WPF development I wanted to dependency injection wherever I could use it because that's how we were building the rest of everything else was heavily with dependency injection so suddenly you start working with a UI framework and it's like you can only use it in a couple of spots and like not really everywhere else like it's just not a good fit starts to feel bad so we would go looking for ways that we could do this and a lot of the time and as I make more YouTube videos on this you'll see the pattern is that I start pulling more and more stuff at a zaml unfortunately so the zaml just ends up becoming like a
pretty lightweight shell of things and then the code behind is very view specific very view specific so I end up doing code behind but I'm not doing business logic and stuff like that it's just view specific I I don't know if I want to read your your full username out but hello Maximus Decimus hello welcome thank you for joining so again what we got out of this was that we skipped the zaml part that had restrictions around no Constructor parameters we could pass in these things because we can do dependency injection on the main window based on how we Things based on how we have things set up and then we have to do the binding ourselves kind of crappy to go write this code but some benefits that are worth talking about I can test The Binding working this way which is kind of
cool I can test my converter in all of its Glory with its dependencies so I can start I can start treating these WPF things like like just other code and that was honestly one of the biggest uh struggles working with WPF for so long was like the WPF things just don't feel like they're the rest of C code because they have restrictions around how you use them so rambling geek and this is a really good question so I assume you will wire up controls and code behind and the layout also kind of so I there's a mix of things so this is how I would explain it so when I think about the different controls I'm working with by the way I should preface that I don't do like any front-end web development like terrible at it and I shouldn't do the negative selft talk
I am very inexperienced at front-end web development but I have years and years and years of experience doing desktop applications so win forms and WPF it's just been a long time but that's that's where like almost all of my programming experience probably I don't know of the last 21 years like what like 17 of those years were building desktop applications so this is where I come from um when I think about controls there are going to be pieces of those controls that um they have like I don't have a better word like components of the controls that that in my opinion like by definition they need to be there so to give you an example if I go back to the main window on my main window I'm if I were making an application I might have like a a menu bar and the menu
bar might be something I want to swap in and out so I will leave a placeholder for the menu bar but I need some type of layout on my form or my my window in this case to say like this is a spot where we will populate a menu bar I might have some type of left navigation or some type of like wizard layout where you can click through and I will say look like this is a spot where I need to have navigation this is a spot where I need to show content so I will basically have a lot of like layout controls and then have placeholders to go drop things into and then rambling geek to your point will I use code behind for doing that the answer is yes a lot of the time I wire things up in code but there's
a couple of other things and I want to make some videos on this but I came up with a pattern and I don't I haven't seen it anywhere else so I don't want to say like I don't want to claim like I invented it but I mean I invented it for myself I guess I came up with a pattern that lets me combine views and you can mix Wind forms and WPF and technically anything that already integrates but when you do it in the code behind you can make it look like you're just joining the views together without having to think in terms of WPF or Wind forms or anything else so it was basically a level of abstraction in the code behind where you could do something like um I want to give it a good name CU I have to come up with
a more unique name let's call it um you'd have like view view Stitcher and you could say like Stitch and you would say um this dot like main navigation so you'd give it I can't spell main navigation you'd give it like the container like where you want to put stuff and then you would say like um Pretend This was like another control but like you'd have a can I do a button you wouldn't do it for these basic controls basic controls would probably almost always just be on the control you were dealing with but pretend this was a more like uh complex composite control and you would do something like this and it would basically put the button into the main navigation so you would just write code that did this if you needed layout properties you could be like um like layout maximiz can't
can't type it basically would give you syntax that look like this and then you could basically I said basically too many times here you could effectively build out your controls and stuff and dock things however you want it in code behind in a very simple way instead of trying to write out like basically all of the initialized component logic so hope that helps answer but to your point yes start it's going more in the direction of code behind for setting things up so zaml becomes very lightweight in terms of just layout for the most part it's like a shell and then the code behind goes ah now that we have this shell I will use dependency injection to ask for the pieces that I need and I will go put them into the right spot so the control itself is still responsible for knowing where
things go that's the important part it knows where things go on itself but it's saying uh and I even had interfaces and stuff for a lot of these things so it would be like hey look I don't even know or care about the implementation of the control so it would literally be like INX navigation and you could do this kind of thing so like this isn't a control it's not a WPF control not by the interface at least but it would be a WPF it would be a WPF control that met some interface then I inject those things in and then the control itself knows not about the implementation but it says I know that I need these things that meet these interfaces I will put them in the right spot but it uses some more like custom mapping behind this nice little uh API
in the syntax to to make it a little cleaner rambling geek been an IT support for subject matter experts for 28 years 47 nice until about 14 months ago moved over to looking after Wind forms apps yay that's with codes since the Spectrum and c64 is cool yeah yeah like there's a I'm trying to think like not still not maybe around half my half my 20 21 years of programming have been in Wind forms like that's that's a long time um and then WPF came around at some point you know partway through that and you know spent a lot of time in WPF but I had to maintain applications that were in both we literally started one of our applications in WPF sorry other way around in Wind forms and as we were building it other parts of our um development teams I was prototyping
something in the product was taking off our other development teams were building in WPF they said like this is we're we're building a new user interface like we're starting with this Tech and we said well like it's not going to be good if we're going in One Direction and they're going in another one so we immediately started replacing in our UI we just started peac mealing parts of it so I wrote some type of Technology like this I'm calling it technology it's not really that fancy it's just a Sy tactic sugar to to be able to combine those things so it worked with Wind forms and WPF together but I think that might be it for the stream it's a little bit past my bedtime but this is a good uh almost two hours of streaming that's pretty cool so what you can expect in
the videos that will be coming up and I'm taking a vacation for oh I don't know if it's the first time this year taking a vacation over the weekend so Friday Saturday Sunday and Monday will be out I want to get some videos out to my Editor to have them ready for this Friday and Monday um but I'm probably going to walk through some binding stuff just to give you a little heads up so I'm not going to butcher The Binding I'm going to show how to do a really simple binding I'll probably do one and two-way binding then I'm going to do a simple example with the the value converter and then I'm going to do a third video that talks about the dependency injection stuff we saw so the service loc can't speak service locator pattern that we looked at first and then
we will go over to the um the more like dependency injection style I might even split those that video into two depending on how much I ramble um I'm trying to get a just for context because if you have opinions about this I'd love to hear but I am trying to do a mix of shorter and long form videos um sometimes the short ones do really well other times people are like hey like please do more longer in-depth videos it's kind of hard to to get everyone happy so I think if I do a mix it's not it's at least appealing to more people so I might split everything I just said into four videos they'll be pretty quick but I hope this was helpful um I will not be live streaming on Monday like normal because as I said I'm going to be on
vacation but if you are interested in general aside from this Monday uh Monday nights 9:30 p.m. PST I do a live stream that's not coding but I generally talk about uh it's historically been mostly like the topics from my newsletter that I put out but it's an AMA style so I talk about General software engineering things if you want to come check that out be great to to chat with you um so things about like career advice so people will join especially if they're like you know aspiring software Engineers or like just graduated or just finished boot camp like what do I do how do I get hired um it's very challenging right now obviously so um if you're not familiar with me who I am I am a principal software engineering manager at Microsoft um I don't like saying it even though you hear
me say it a lot but people were like hey you really have to say that a lot online so people know that there's some Authority behind what you're saying uh I would rather just demonstrate to you the thing I know like how I apparently can't do binding in WPF um but I would rather just show you the things but um people have suggest that I remind people um so if you would like advice from me some of my career I'm happy to share those experiences and I'm happy to answer questions for you that's why I'm doing it so yeah won't see you guys this Monday coming up but hopefully going forward the next Monday you can tune in uh if you're watching this right now you can can expect that stream on the same platform so if you're like hey I kind of just came
across you here but I like watching stuff on other platforms it's basically everywhere so kick Instagram Tik Tok twitch YouTube LinkedIn did I say Twitter Facebook it's everywhere um so if you want to watch on a different platform you're welcome to do that but it will be on the same platform that you're watching on as well so good to see you folks thanks for for coming back guys it's great to see uh returning faces and stuff it's kind of cool so rambling geek good to see you again I know I'm scrolling back up X coding for sure Nick Hodge if you're still here thank you and folks thanks again we'll see you next time
Frequently Asked Questions
What topics will you cover in the upcoming videos after this live stream?
In the upcoming videos, I plan to cover a few key topics. First, I'll do a simple binding demonstration, followed by a video on two-way binding. Then, I'll create a video focusing on value converters, and finally, I'll discuss the dependency injection patterns we explored during this stream. I might split some of these into shorter videos to cater to different viewer preferences.
Why did you choose to use WPF for this coding session?
I chose to use WPF because I wanted to demonstrate some of the patterns and techniques that can be applied in desktop application development. Many developers are still building applications with WPF, and I think it's important to showcase how to effectively use it, especially in the context of dependency injection and value converters.
What should I do if I encounter issues with bindings in WPF like you did during the stream?
If you encounter issues with bindings in WPF, I recommend checking a few things. First, ensure that your data context is set correctly and that the properties you're binding to are public. Also, look for any errors in the output window, as WPF will often provide helpful messages there. Lastly, don't hesitate to consult documentation or online resources for examples and troubleshooting tips.
These FAQs were generated by AI from the video transcript.