Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Create And Use A CustomResource In XAML
WhatsApp
Rachhek Shrestha
9y
15.3k
0
2
100
Article
Example 1
Open a new Visual C# windows project.
Add a new class named say CustomResourceTest.cs in the project folder.
Derive this class from CustomXamlResourceLoader Class(Case sensitive) like below:
You will get a Namespace not found error. Resolve it by using Windows.UI.Xaml.Resources Namespace.
Override the GetResource Member of the parent class as below. Use the intellisense to select the member.
Replace the Code inside the GetResource Method as: (this is just a simple example). We are returning a text. We plan to show this text inside a TextBlock’s Text Property.
Inside the MainPage.cs . Add the following line of code inside the MainPage Constructor to reference the CustomResouceTest.cs Class from the Page’s XAML.
Correct the NameSpace not found error by resolving it.
Now go to the MainPage.xaml Page and Add a TextBlock as follows. Notice the Text property of the TextBlock.
This results in the following output when you save, build and run the project.
What is happening here?
We created a CustomResourceClass where we inherited the Class called CustomXamlResourceLoader.
We override the GetResourceProperty. Don’t focus on the parameters of this method for now.
We replaced the code inside this method by simply returning a text.
To access this CustomResource from XAML we have to define the CustomXamlResourceLoader. Current property to the new instance of the Class we created. We have to do this inside the Constructor of the Codebehind page where we want to use the CustomResource.
We then simply assigned the value of the Text property of the textblock to the CustomResource as seen on Step 8.
Example 2:
Now we will try a different example where we want to display the Text of the TextBlock based on the value we pass on. Change the text of the Mainpage.xaml as:
The 'sayHello' string is passed as a string to the CusomResourceTest
.
cs class as ResourceID parameter of the overridden class. This will be more clear as you see in the next step.
In the CustomResourceTest.cs
class
, change the code as follows:
The thing to understand is how we pass the ResourceID from the Text Property of the TextBlock. It is passed as the resourceID parameter. So, based on the ResourceID, we return the appropriate text we want to display on the output screen.
So now we get output as.
If we change the text property as sayByeBye.
We get the following output:
For More details visit:
CustomXamlResourceLoader
class.
CustomResource
CustomResource in XAML
Use CustomResource in XAML
XAML
Up Next
Ebook Download
View all
Programming XAML
Read by 12.3k people
Download Now!
Learn
View all
Membership not found