Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit I can't read template generic.xaml file. HEELPPP please
7 replies. Latest Post by Somnath Panja on August 11, 2008.
(0)
mustafac...
Member
36 points
39 Posts
06-24-2008 4:05 AM |
Hi: i created a CustomControl.like this:CustomDeneme in: --generic.xaml i defined listbox template. i wanna read template in generic.xaml file --app.xaml --Page.xamlCustomLibrary in: -- MyContollSoft.cs --SoftProperty.csi am sending sample in down linkhttp://kontakemlak.com/deneme/CustomDeneme.rarWhat am i doing wrong?
sladapter
All-Star
17427 points
3,171 Posts
06-24-2008 11:01 AM |
Many things are wrong.
1) generic.xaml is a file for you to define custom controls's default template. Not for define the style for existing control. If you want to define a style for a exsiting control, do it in App.xaml or on the UserControl.Resources section.
2) generic.xaml must be at the same project as your custom control. So if you want to define template for your MyContollSoft costom control, move the geneic.xaml file to your CustomLibrary project.
3) I saw you only define a style for ListBox not for MyContollSoft. Move that style to App.xaml and the correct syntax to do this is:
<Style x:Key="HeaderPresenter1" TargetType="ListBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBox">
<!-- Put your Listbox template here -->
</ControlTemplate>
</Setter.Value>
</Setter Property>
</Style>
4) Once the style for ListBox is in App.xaml, you do not need Xaml reader to read it. You can just set it this way:
listBox.Style = Application.Resources[YourStyleKey] as Style;
5) If you have template defined for your "MyContollSoft" in the generic.xaml, you need to add a constructor for your custom control:
public MyContollSoft() { this.DefaultStyleKey = typeof(MyContollSoft); // make sure you have this line if you have a template for this control. }
this way OnApplyTemplate will be called by the framework code. You do not need to call it.
06-25-2008 4:08 AM |
thank you for answer;i did one more correction but it didnt work again. i want to read style in ResourceTemplates.xaml or App.xaml not generic.xamli am sending the sample in down link.http://kontakemlak.com/deneme/MyControllWithBeta.rarin fact my problem one more can be xaml file customer two day laters new style can add the sample
06-25-2008 4:58 AM |
I did the sampleLike this:public MyContollSoft() { Assembly assembly = Assembly.GetExecutingAssembly();// string fooResourceName = "MySoftClass.generic.xaml"; you can read like thisstring fooResourceName = "MySoftClass.ResourceTemplates.xaml";using (Stream s = assembly.GetManifestResourceStream(fooResourceName)){StreamReader reader = new StreamReader(s);string xaml = reader.ReadToEnd();ResourceDictionary rdd = (ResourceDictionary)XamlReader.Load(xaml);Style myStyle = rdd["myStyle"] as Style;if (myStyle != null){this.Style = myStyle;}reader.Close();s.Close();}DefaultStyleKey = typeof(MyContollSoft);}
{
roarfred
45 points
14 Posts
07-06-2008 2:25 PM |
Thanks, sladapter! This ended my night-long search for a similar problem, and got me back on track.
I was unable to get a style template working for my control, and setting the DefaultStyleKey was the solution. My journey for a solution was heading towards the idea of putting the style in the app.xaml file, and having to refer to the StaticResource from xaml. That kind of worked, but I lost design-time preview of the containing page/control in VS. Anyways, I was looking for a solution for that problem when I see your post and the sun shines so bright again.
again, thanks!
Regards,
Roar
Somnath ...
118 points
107 Posts
08-07-2008 3:05 PM |
mustafacakir:public MyContollSoft() { Assembly assembly = Assembly.GetExecutingAssembly();// string fooResourceName = "MySoftClass.generic.xaml"; you can read like thisstring fooResourceName = "MySoftClass.ResourceTemplates.xaml";using (Stream s = assembly.GetManifestResourceStream(fooResourceName)){StreamReader reader = new StreamReader(s);string xaml = reader.ReadToEnd();ResourceDictionary rdd = (ResourceDictionary)XamlReader.Load(xaml);Style myStyle = rdd["myStyle"] as Style;if (myStyle != null){this.Style = myStyle;}reader.Close();s.Close();}DefaultStyleKey = typeof(MyContollSoft);}
Assembly assembly = Assembly.GetExecutingAssembly();// string fooResourceName = "MySoftClass.generic.xaml"; you can read like thisstring fooResourceName = "MySoftClass.ResourceTemplates.xaml";using (Stream s = assembly.GetManifestResourceStream(fooResourceName)){StreamReader reader = new StreamReader(s);string xaml = reader.ReadToEnd();ResourceDictionary rdd = (ResourceDictionary)XamlReader.Load(xaml);Style myStyle = rdd["myStyle"] as Style;if (myStyle != null){this.Style = myStyle;}reader.Close();s.Close();}DefaultStyleKey = typeof(MyContollSoft);}
HI mustafacakir,
I also always get "s" as null stream.
My Assembly of my custom control is: SLControl.Games.dll
Default name space is: Control.Game
I have Style.xaml file included in custom control project, Build Action is: Resource
string fooResourceName = What will be right path for Style.xaml?using (Stream s = assembly.GetManifestResourceStream(fooResourceName))
I was trying like this string fooResourceName = "SLControl.Games.Style.xaml";
But Stream "s" is always null.
please help..
08-09-2008 1:53 PM |
hi;
i wanna ask you something;
did you mark as Embedded Resource the style.xaml file.if style.xaml file dosen't Embedded . dosent work
08-11-2008 12:08 AM |
Yes if i make it embedded resource..
It works... thanks...