How To Create And Use User Defined Resource To Button Control In UWP With XAML And C#

Introduction

In Visual C# .Net, a number of predefined system resources are available. Local resources are user defined and you can use your local project. In this article, we can understand how to create the default button background and Content local resource and how to use the local resource in button Background and content.

Before reading this article, please go through the following article.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015

Reading this article, you can learn how to create and use user defined resource to button control in Universal Windows apps development with XAML and Visual C#.

The following important tools are required for developing UWP-

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)

Now, we can discuss step by step App development.

Step 1 - Open Visual studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank app -> Give the suitable name for your app (UWPButtonResource)->OK.



Step 2 - Choose the Target and minimum platform version for your Windows Universal Application will support. Afterwards, the project creates App.xaml and MainPage.xaml.



Step 3 - Add the following code for creating Local Resource as background color to set, when creating a new button-

  1. <Page.Resources>  
  2.     <Style TargetType="Button">  
  3.         <Setter Property="Background" Value="GreenYellow"></Setter>  
  4.     </Style>  
  5. </Page.Resources>  


Add TextBlock control and change the name and text property for the title-



Step 4 - Add button control and the background color will set automatically.



Step 5 - Add Title Resource
  1. <x:String x:Key="Title">Click Me</x:String>  


Step 6 - Set the Title Resource to button control.



Step 7 - Deploy of your app in local machine and the output of the UWPButtonResource app is given below-



Summary - Now, you have successfully created and used the user defined resource to button control in Visual C# - UWP environment.