WPF Resources

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys so in this lecture we will understand what our WPF Resources now resources are a concept which is widely used with styles triggers etcetera that is you can store your Styles triggers or themes etc as resources so that you do not need to define those Styles again and again so let's understand the concept of resources and then in our little lectures we will understand how to use these resources as styles or triggers so let's move to our slides first so WPF uses a concept to store data locally for entire window or for entire application this data could be Styles triggers templates etc so as I told you earlier that you need to store your Styles triggers templates either data template or control template as resources so that you don't need to define them again and again so for example if you have an image to be used in as a background of a window we can store it as a resource so let's move to visual studio and understand what do I mean by these whole points so here I have a very simple application which just contains a button ok so the content of this button is my button and the width and height are defined accordingly now let's make the background of this button as red colored background ok so let's define a attribute and then let's make the button as red color butter so as you can see I have defined the red blood as the background of this button but I can also do this with the help of resources so that I do not need to apply this red colored property to each and every button of my application suppose I have 10 or 15 buttons in my application and all the background of those buttons are red color so I don't need to define this in each and every butter so let us define a resource to store this red color and then we can apply that red color to my background property of this button so for that you need to define the window resources so window dot resources and then we define the so brush and to the color property we assign a red color okay so I have to find a solid color brush okay and the property of that solid color brush is red and you can see there is an error which says each dictionary entry must have an Associated key so how my WPF will identify this the source with the help of a key okay so let me first expand it so that you can see it clearly and let me define a key so key is defined like X column and then we need to give a key so let's give a name okey so let me give it a key s red crash okay so I have defined a key and my error is gone okay now instead of this red color hard coding I can defined it a static resource and you can see I have got my red brush T available with me so the moment I declare it my button has become red colored button so no matter how many buttons I have I can always have the background defined with with the help of a static resource markup extension and this is static the source markup extension takes a key and that key is defined in here so I have my windows resources defined and applying those windows resources in here so whichever property of the button takes a color I can apply this resource to that property so let me make the border this is the border property of this button border brush and you can see the border of my button has become red color okay so I can apply this resource to any of the property of the button which takes a color so let me define another resource in here so with me copy this and paste it here and let me define it as blue brush the key is blue brush and let me have the blue color associated with it okay and let me change this property to background okay so you can see all both of these resources are available with me at compile time that is these are static resources so blue brush when I apply my color of the button has become blue and when I use red brush T the color becomes red so these two resources are defined as a static resources similarly we can define styles and triggers templates inside this window resource tag so these resources are defined in window resource so suppose I have a another window inside my application so these resources which won't be accessible to that window so in order to have the application wide resources I need to define these resources inside my app dot zamel file so let me cut it from here and you can see I don't have any resource now so I am getting an error it says a red brush cannot be resolved because I don't have a red brush resource inside my window resource so let me go to AB dot zamel file and you can see here we have application resource markup extension or application resources tag so let me paste my resources and side application resources and let's go back to our main window and you can see my error has gone away and my button has become red again okay so this static resource is accessed via AB dot zamel and now say suppose if I have another window or two or three windows these resources will be accessible to each and every window ok so let let us go back and cut that sources again I am getting an error so these resources could also be defined in the control level so let me remove this so that you do not get confused and let me define a greater resource so great dot resources so this resources are accessible inside this grid so if I define it as a window resource the resources would be accessible inside that window if I define it inside F dot zml so this resources would be accessible across my application I hope you understood the concept so let's move back slides so you can see the Third Point resources are given a key using the X : key attribute which allows us to difference it from other parts of the application by using this key in combination with the static resource markup extension so I have defined a static resource markup extension in here okay and this is except accessible with the help of a key thus we can say resources are nothing more than value stored in a dictionary generally we provide a key and get back some sort of object so same thing we are doing it here we are giving the application as a key and we are getting the value red or blue back so this is the same concept as of a dictionary so you are giving a key and getting the value back so resources could be declared as window resources application resources or controlled resources so this all three examples we have seen okay so now in WPF there are two type of resources a static resource and dynamic resource now the example what I have told you earlier is the example of static resource so static resource is resolved at compile time that is the value object is set when you are working with sam'l so I am working with my sam'l in here and I am changing it to red rash and my button has changed to red color in the compile type itself I do not need to run my application okay so this is called static resource so same concept goes with dynamic resource but it is resolved at runtime so a dynamic resource is resolved at runtime the value of object is evaluated and stored in an expression and the value is substituted at runtime so the value of that resource will be evaluated at compile time itself but it will be substituted at runtime let's quickly see example of dynamic resource so let's move back to visual studio and let me remove this resource okay so I do not need to read dot resources markup extension and let me home at it you can see I do not I am not getting a resource found so I am getting an error here so now let me move this button a bit to the left and let me add two buttons and then I will explain you what I am trying to do so let me paste two buttons you can see I have pasted two buttons in here so one button is change color to red and other is change color to blue so if I click this button my my button will become red and if I click this it will become blue okay so let me generate the click event for both of the buttons and same goes with the blue colored button and this is for the red color and this is for the blue color so let's write this dot resources and let me give a resource key same as we have given in our static resource so it let me name it to dynamic color okay equals new solid color brush colors dot red so what I am doing here I am taking or extracting the red color value from this solid color brush class and I am storing it as a resource and I am giving a resource key in here okay so the moment I will call this dynamic color key this will give me value as red and same I can do it in here and let's change this color to blue this blue okay so same concept goes in here too let me move to Zaman and let me first change the key of this to the source so I have used the same key which is defined in my code behind and let me change this to dynamic resource okay so you can see I am still getting an error it says that is hose dynamic color could not be resolved this is because dynamic color dynamic resource is always resolved at runtime so at compile time my application does not know that this key belongs to which resource ok this is the reason why it is called dynamic resource and it it is associated at runtime so whatever be the value that will be substituted in dynamic resource at the runtime so I am getting an error but I am I will be able to run this application successfully so let us try to run this and you can see my application is running successfully and it's changed the color to red you can see my buttons color is change toward red and it is also working well for change color to blue button so I hope you understood dynamic resource also smoke back to our slides so a dynamic resources is all dat run time and the value object is evaluated and stored in an expression and the value subject substituted at runtime so mostly Styles templates etcetera are declared as static resources dynamic resources are used when an application is running and user wants to change the value without recompiling the application so in my application also you you can see my application is able to use the color of this button without recompiling my application that is at runtime so one of the major use of dynamic resources is localization of languages so I have a UI running in my application in my system right now which is a UI which I have downloaded from internet - just to know for you to make you understand what do I mean by localization of languages so let's look into that UI and you can see it's a chat application and it's just the login window of this chat application and you can see a drop down in here which says English language okay so my whole UI is in English so say suppose I need to change it to some other language say Russian so you can see the application is changed to Russian languages and this concept is called localization of languages okay and this is achieved with the help of dynamic resource so you don't need to have you have to recompile your application and change the UI of your application okay and this is achieved with the help of dynamic resources so I will explain how to achieve this functionality in one of my later videos when we will be developing a simple application so this will be achieved with the help of dynamic resource so let's move back to our slides so you can see the example of localized example is the localization of languages for dynamic resource so these are the slides for you to note it down I hope you understood the concept of static the social dynamic resource in WPF and this is also a most common interview questions that what do you mean by static resource and dynamic rizzo or what is the difference between static resource or dynamic resource in WPF so I hope this video has helped you if you have any doubt please leave a comment below and please do subscribe to my channel thank you so very much
Info
Channel: DotNetSkoool
Views: 56,637
Rating: undefined out of 5
Keywords: C# Training, DotNetSkoool, WPF Training, WPF Resources, Dynamic Resource in WPF
Id: 0rcHiV8qS9g
Channel Id: undefined
Length: 15min 10sec (910 seconds)
Published: Wed May 18 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.