Skip to main content

Microsoft Silverlight

Answered Question I can't read template generic.xaml file. HEELPPP pleaseRSS Feed

(0)

mustafacakir
mustafac...

Member

Member

36 points

39 Posts

I can't read template generic.xaml file. HEELPPP please

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.xaml

CustomLibrary in: 
 -- MyContollSoft.cs
 --SoftProperty.cs

i am sending sample in down link

http://kontakemlak.com/deneme/CustomDeneme.rar


What am i doing wrong?

sladapter
sladapter

All-Star

All-Star

17427 points

3,171 Posts

Re: I can't read template generic.xaml file. HEELPPP please

 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.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

mustafacakir
mustafac...

Member

Member

36 points

39 Posts

Re: I can't read template generic.xaml file. HEELPPP please

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.xaml

i am sending the sample in down link.

http://kontakemlak.com/deneme/MyControllWithBeta.rar

in fact my problem one more can be xaml file

customer two day laters new style  can add the sample

 

mustafacakir
mustafac...

Member

Member

36 points

39 Posts

Answered Question

Re: I can't read template generic.xaml file. HEELPPP please

I did the sample


Like this:

public MyContollSoft()

{

Assembly assembly = Assembly.GetExecutingAssembly();
// string fooResourceName = "MySoftClass.generic.xaml";    you can read like this
string 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
roarfred

Member

Member

45 points

14 Posts

Re: Re: I can't read template generic.xaml file. HEELPPP please

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 Panja
Somnath ...

Member

Member

118 points

107 Posts

Re: I can't read template generic.xaml file. HEELPPP please

mustafacakir:
public MyContollSoft()

{

Assembly assembly = Assembly.GetExecutingAssembly();
// string fooResourceName = "MySoftClass.generic.xaml";    you can read like this
string 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..

 

 

mustafacakir
mustafac...

Member

Member

36 points

39 Posts

Re: I can't read template generic.xaml file. HEELPPP please

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

Somnath Panja
Somnath ...

Member

Member

118 points

107 Posts

Re: Re: I can't read template generic.xaml file. HEELPPP please

Yes if i make it embedded resource..

It works... thanks...

 

Microsoft Communities