locked
how to Add resource dictionaries by code in metro

    Question

  • Hi

    i need to add  themes\generic.xaml  to resource dictionary by code

    in silverlight i applied the following code and works fine

     

    ResourceDictionary dic1 = new ResourceDictionary();

    dic1.Source =

    new Uri("/SilverlightApplication1;component/Themes/generic.xaml", UriKind.Relative);

    now in metro how can i do that?, i tried the same code in silverlight but unfortunatly not successed, then i tried the following

    also not successed

      

     

     

    ResourceDictionary dic1 = new ResourceDictionary

    ();

    dic1.Source =

     

    new Uri("ms-resource:Themes/generic.xaml", UriKind.RelativeOrAbsolute);

    Any Help??

     
    Monday, November 14, 2011 7:50 AM

All replies

  • The Source URI needs to be absolute, not relative:

                ResourceDictionary dic1 = new ResourceDictionary();
                dic1.Source = new Uri(BaseUri, "themes/
    generic.xaml"); 
    

    --Rob

    Monday, November 14, 2011 9:31 PM
    Moderator
  • thanks Rob

    i can get now the uri of generic file

    i need to set the theme of my custom button by code , i want to load the theme from generic.xaml by code which contains the following

     

    <

    ResourceDictionary

     

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

     

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     

     

    xmlns:custom="using:Test">
     

     

    <Style TargetType

    ="custom:CustomButton">

     

    <Setter Property

    ="Template">

     

    <Setter.Value

    >

     

    <ControlTemplate TargetType="custom:CustomButton" x:Key

    ="CustomControlTemplate">

     

    <Grid x:Name

    ="RootElement">

     

    <Rectangle x:Name="BodyElement" Width="200" Height

    ="100"

     

    Fill="Red" Stroke="Purple" RadiusX="16" RadiusY="16" />
     

     

    <TextBlock Text="Click Me" HorizontalAlignment

    ="Center"

     

    VerticalAlignment="Center" />
     

     

    </Grid

    >

     

    </ControlTemplate

    >

     

    </Setter.Value

    >

     

    </Setter

    >

     

    </Style

    >

    </

    ResourceDictionary>

     

    i created the control by code and tried to load its theme but the following exception thrown when i set control template ('Error HRESULT E_FAIL has been returned from a call to a COM component.')

    the following is my code

     

    CustomButton b = new CustomButton

    ();

     

    ResourceDictionary dic1 = new ResourceDictionary

    ();

    dic1.Source =

    new Uri(BaseUri, "themes/generic.xaml"

    );

     

    ResourceDictionary dic = new ResourceDictionary

    ();

    dic.MergedDictionaries.Add(dic1);

    Resources = dic;

    b.Template = (

    ControlTemplate)Resources["CustomControlTemplate"

    ]; // exception thrown here

    Can you Help Me Please?

     

     

    Tuesday, November 15, 2011 6:31 AM