Author : Admin

WPF Interview questions with answers - part1


What is xmlns in XAML file?

xmlns is stand for XML namespace. For understanding, we know In .Net code file, we need to add the namespaces for using different entities (like classes, enums, constants, functions, attributes etc) and removing the conflict, similarly, we need to use xmlns in XAML file to add different namespaces which help us to use different ui controls and other programming entities and preventing conflicts from occurring.

suppose two developers have using the same name for elements in their code and without namespace, it will create conflict.

Suppose developer1 create ExtendedTextbox for integer purpose and developer2 create ExtendedTextbox for string purpose, so preventing the conflict of using same name classes in a single class. so we can use them like this 
For example, if we had the namespaces below (using a URI to define the namespace):

xmlns:iextctrl=”https://www.gurukultreewpf.com/IExtendedControl”
xmlns:sextctrl=”https://www.gurukultree.com/SExtendedControl”

we can use it like this

<iextctril: ExtendedTextbox>

<sextctril: ExtendedTextbox>

What are Resources in WPF?

In WPF, resources give a method to reuse common objects in different places in your application. for understanding, common examples of resources are brushes and style. Resources can define local for control, windows or globally for the whole application.

Syntax of defining resouce
<elementname x:Key="keyname" propertyname="propertyvalue" />

What is the Scope of Resources?

The scope of the resource is related to where we are defining it. Resource object can be defined as:

App Resources
Resources which is define under Application. Resources is accessible in the whole application.
    <Application.Resources>
        <SolidColorBrush x:Key="lblbgColor" Color="Gray"></SolidColorBrush>//Resource object
    </Application.Resources>

Windows Resources
    <Window.Resources>
            <SolidColorBrush x:Key="lblbgColor" Color="Gray"></SolidColorBrush>
    </Window.Resources>

UserControl Resoures
    <UserControl.Resources>
            <SolidColorBrush x:Key="lblbgColor" Color="Gray"></SolidColorBrush>
    </UserControl.Resources>

Element Resoures
    <Grid>
        <Grid.Resources>
                <SolidColorBrush x:Key="lblbgColor" Color="Gray"></SolidColorBrush>
        </Grid.Resources>
    </Grid>
The keyname in the resource object is the unique name to identify the resource.
            <Label Name="lblName" Margin="20" Background="{StaticResource lblbgcolor}" Height="30" />

Resouces can be referenced by two way, either Static Resource or Dynamic Resource.