Tuesday, November 3, 2009

How to load the WPF controls dynamically at Runtime

In order to load the WPF controls dynamically at Runtime,  please do the following steps.

1. Create a dummy XAML content file (Dynamic_Content.xaml) with Controls details.
<StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <Button x:Name="buttonStart" IsEnabled="True" Content="Start" Height="25" Width="100" Margin="5" />
    <Button x:Name="buttonStop" IsEnabled="False" Content="Stop" Height="25" Width="100" Margin="5" />


</StackPanel>

2. Select the file (Dynamic_Content.xaml) in Solution Explorer and  go to the properties.

3. Set “Build Action” as “Content” and “Copy to Output Directory” as “Copy always”.

4. Then you can use XamlReader class for reading the XAML content which we have the controls.

5. Please use the following code to read and load controls from XAML content file.



private void tab_TabItemAdded(object sender, TabItemEventArgs e)
{
    FileStream stream = new FileStream(@"Dynamic_Content.xaml", FileMode.Open, FileAccess.Read);
    StackPanel panel = XamlReader.Load(stream) as StackPanel;

    e.TabItem.Content = panel;
    e.TabItem.Header = string.Format("Tab Item - {0}", tab.Items.Count);
    stream.Close();

    Button buttonStart = panel.Children[0] as Button;
    buttonStart.Click += new RoutedEventHandler buttonStart_Click);           
}


void buttonStart_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Start button invloked");
}

Friday, October 30, 2009

How to push certificate to "Trusted root certificate authorities" using Installer

In order to run your Full Trust enabled WPF XBAP application, you have to use “Self signed certificate authority file” for running and deploying XBAP application. The certificate should pushed it into “Trusted Publisher” and “Authority Root” certificate storages. You can use X509Certificate2 and X509Store classes in your installer assembly for pushing the certificate to certificate storage. Please use the following code snippet.


string certPath = string.Format(@"{0}\Aaa.cer", Environment.GetFolderPath(Environment.SpecialFolder.System));
X509Certificate2 certificate = new X509Certificate2(certPath);
X509Store trustedPublisherStore = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
X509Store trustedAuthorityRootStore = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine);

try
{
     trustedPublisherStore.Open(OpenFlags.ReadWrite);
     trustedPublisherStore.Add(certificate);
     trustedAuthorityRootStore.Open(OpenFlags.ReadWrite);
     trustedAuthorityRootStore.Add(certificate);
}
catch (Exception ex)
{
     MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
}
finally
{
     trustedPublisherStore.Close();
     trustedAuthorityRootStore.Close();
}


Please refer the following link to know more about “WPF - XBAP as Full Trust Application

Friday, September 25, 2009

Reading file from local system in Silverlight application

OpenFileDialog class will help us to read the one or more files from local system. The
following code snippet will help us to read the file from local machine.
 

OpenFileDialog file = new OpenFileDialog();
file.Filter = "Text files (*.txt)|*.txt|All Files(*.*)|*.*";

if (file.ShowDialog() == true)
{
     using (StreamReader reader = file.File.OpenText())
     {
          this.txtFileContent.Text = reader.ReadToEnd();
          reader.Close();
     }
}

Sunday, May 10, 2009

Signing assembly

Sy:-
sn [-q-quiet]
Options:-
-c []
Set/reset the name of the CSP to use for MSCORSN operations.
-d
Delete key container named .
-D
Verify and differ only by signature.
-e
Extract public key from into .
-i
Install key pair from into a key container named .
-k []
Generate a new key pair of the specified size and write it into .
-m [yn]
Enable (y), disable (n) or check (no parameter) whether key containers
are machine specific (rather than user specific).
-o []
Convert public key in to text file with comma separated
list of decimal byte values.
If is omitted, text is copied to clipboard instead.
-p
Extract public key from key pair in and export to .
-pc
Extract public key from key pair in and export to .
-Pb [yn]
Enable (y), disable (n) or check (no parameters) the CLR policy allowing
trusted applications to bypass strong name signature verification on their
assemblies.
-q
Quiet mode. This option must be first on the command line and will suppress
any output other than error messages.
-R[a]
Re-sign signed or partially signed assembly with the key pair in .
If -Ra is used, hashes are recomputed for all files in the assembly.
-Rc[a]
Re-sign signed or partially signed assembly with the key pair in the key
container named .
If -Rca is used, hashes are recomputed for all files in the assembly.
-Rh
Re-compute hashes for all files in the assembly.
-t[p]
Display token for public key in (together with the public key
itself if -tp is used).
-T[p]
Display token for public key of (together with the public key
itself if -Tp is used).
-TS
Test-sign signed or partially signed assembly with the key pair in
.
-TSc
Test-sign signed or partially signed assembly with the key pair in the key
container named .
-v[f]
Verify for strong name signature self consistency. If -vf is
specified, force verification even if disabled in the registry.
-Vl
List current settings for strong name verification on this machine.
-Vr [] []
Register for verification skipping (with an optional, comma
separated list of usernames for which this will take effect and an
optional test public key in ).
can be specified as * to indicate all assemblies or *, to
indicate that all assemblies with the given public key token. Public key
tokens should be specified as a string of hex digits.
-Vu
Unregister for verification skipping. The same rules for
naming are followed as for -Vr.
-Vx
Remove all verification skipping entries.
-?
-h
Displays this help text.

Ex:-
sn -R "aeembly name.dll" "keyfile.snk"

For more Info:-
http://msdn.microsoft.com/en-us/library/k5b5tt23.aspx

Monday, January 7, 2008

Thursday, September 27, 2007

Thursday, August 23, 2007