If the location of an application is changed by the end user, the application is not running well. To overcome this problem in .Net 3.5. the PackUri is used to embed resource to the current assembly. So that if the location or name of the application programm folder is changed the application runs well. This is the main benefit for embedding the resources.
In this I explain both technique for embedding resources to assembly. Using .XAML and code behind file i.e XAML.cs
1) Embedding resources Using .XAML code
The Following steps are required for embedding resource to assembly using XAML code:-
Step:-1. Create a ListView Control the .XAML code. Or create the control to which you want to bind it using XMLDataProvider . In this example I use ListView Control.
Step:-2. Create an XML file for data. In this example i create data.xml file;
Step:-3 Open the Solution Explorer
Step:-4 Select the .xml file.
Step:-5 Right Click the mouse button and select Properties.
Step:-6 Set its Build Action to Resource, if not set.
The Following code helps you to embed .XML file
<Window.Resources>
<XmlDataProvider x:Key="empData" Source="pack://application:,,,/data.xml" XPath="Customers">
</Window.Resources>
Note:- If you want to bind the resources Compile time use the "pack://application:,,,/resources.xaml"
If you want to bind the resources Run time use the "pack://siteoforigin:,,,/resources.xaml"
Note:- If the resources is in subfolder mention the subfolder name like this
"pack://application:,,,/subfolderName/resources.xaml"
Note Create data.xml File:-
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer>
<Code>1234</Code>
<Name>Amit Kumar</Name>
<Country>Sesame Street</Country>
</Customer>
<Customer>
<Code>3234Code>
<Name>Anil SinghName>
<Country>United KingdomCountry>
</Customer>
<Customer>
<Code>3344</Code>
<Name>Kimar Alok</Name>
<Country>Spain</Country>
</Customer>
<Customer>
<Code>4321</Code>
<Name>Harmeet Singh</Name>
<Country>Mars</Country>
</Customer>
<Customer>
<Code>4322</Code>
<Name>Jhon s.</Name>
<Country>Mars</Country>
</Customer>
<Customer>
<Code>4323</Code>
<Name>Sachin</Name>
<Country>U.K.</Country>
</Customer>
<Customer>
<Code>4324</Code>
<Name>Rajesh</Name>
<Country>U.S.A.</Country>
</Customer>
<Customer>
<Code>4325</Code>
<Name>Govind Verma</Name>
<Country>Mars</Country>
</Customer>
<Customer>
<Code>4326</Code>
<Name>Sachin Sharma</Name>
<Country>India</Country>
</Customer>
</Customers>
Note:- Here is the complete code for embed .xml file, and bind it to the ListView Control.
<Window x:Class="ListViewControlAndXmlDataFile.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListView Demo" Height="280" Width="500" Loaded="Window_Loaded">
<Window.Resources>
<XmlDataProvider x:Key="empData" Source="pack://application:,,,/data.xml"
XPath="Customers">
</XmlDataProvider>
<Style x:Key="MyItemContainerStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="0.5,1">
<GradientStop Offset="1.0" Color="#F4F3F3"></GradientStop>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#FF0E0101"></Setter>
<Setter Property="FontSize" Value="14"></Setter>
</Style>
<Style x:Key="MyColumnHeaderContainerStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="FontSize" Value="15"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF223B84" Offset="1"/>
<GradientStop Color="#FF57A0F4" Offset="0.5"/>
<GradientStop Color="#FF4B94EC" Offset="0.5"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="DarkBlue"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Name="mainGrid" Background="Black" Width="680">
<ListView Name="myListView" ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Source={StaticResource empData}, XPath=Customer}"
ItemContainerStyle="{DynamicResource MyItemContainerStyle}">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{DynamicResource MyColumnHeaderContainerStyle}">
<GridViewColumn Header="Code" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding XPath=Code }"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Employee Name" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding XPath=Name}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Country Name" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate x:Name="countryDataTemplate">
<TextBlock TextWrapping="Wrap" Text="{Binding XPath=Country}">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
XAML.cs code for embedding resource:-
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("pack://application:,,,/ListViewControlAndXmlDataFile/ListViewControlAndXmlDataFile/date.xml");
String dataFile = uri.LocalPath;
DataSet ds = new DataSet("Table");//Create a Dataset object
ds.ReadXml(file);// Read the xml file and store it in dataset object ds;
ds.ReadXml(dataFile);
myListView.DataContext = ds.Tables[0].DefaultView; //Update the listView by setting the dataContext
}
Used Namespace;
using System.Data;
using System.IO.Packaging;
Thanking U.
No comments:
Post a Comment