Hi,
I am trying to define a Style for a custom control I have created in the Application.Resources section of my App.xaml.cs file. However the page that references this file raises an exception (StaticResource not found for key).
The sample here :-
https://github.com/xamarin/xamarin-forms-samples
Doesn't use a Key or a Custom Control for it's Application.Resources so I am not exactly sure how to get this working.
Is there any more comprehensive examples available or any pointers as to how to get this working?
Update: The same Style defined in the Page's Resources section, works successfully.
Thanks
Paul Diston
Posts
You have to use a DynamicResource for global styles.
@JMarcus Thanks for that pointer, however I don't seem to be able to get it working. Do you have any examples or code snippets for global styles applied to custom controls?
The reason this wasn't working was due to be omitting the call to InitializeComponent (); in the App constructor which is required when creating the App.xaml and associated code behind.
@PaulDiston Thanks for the find!! This saved me a lot of time.
@PaulDiston can you post a small example of how you got this working? This has been giving me trouble for some time. Thanks!!
Hi,
The App.xaml.cs would look like this :-
public partial class App : Application { public App() { InitializeComponent(); } protected override void OnStart() { // Handle when your app starts } protected override void OnSleep() { // Handle when your app sleeps } protected override void OnResume() { // Handle when your app resumes } }
And App.xaml would look like this :-
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyNamespace.App"> <Application.Resources> <ResourceDictionary> <Style TargetType="Grid"> <Setter Property="Padding" Value="10, 10, 10, 10" /> </Style> </ResourceDictionary> </Application.Resources> </Application>
If you would like a more complete example, please do let me know.
Hope this helps.
Thanks
Paul
You saved me, thanks.
@PaulDiston Thanks, you saved me some time and searching
@PaulDiston one more thanks!
Does it work with StaticResource or you've used DynamicResource as @JMarcus noted before?
@PaulDiston I think you've just saved me hours of frustration. I must have accidentally clobbered that InitializeComponent method call at some point, because I know it was there a few days ago. Thank you!!
@MikeGringauz I'm using it with an (admittedly very simple) StaticResource, and so far so good...
@pauldiston ++ to whatever brownie point system they have here. Much appreciate with the InitializeComponent tip...
@PaulDiston Thanks a lot ! This InitializeComponent tip is mentioned nowhere ! Of course the samples contain it, but who pays that detailed attention when he believes the code from old App.cs is just a copy paste? Thanks again ! I'm searching about it over an hour now.
Thank you, was about to pull my hair out.
I know this was posted a year ago but thanks @PaulDiston great fix spent ages searching for this answer
Ahhhh! Saved me too!
Cheers, awesome spot.
thanks! just went through xamarin university "Resources and Styles" but missed this when implementing my style and got
"StaticResource not found for key"
Me too getting this error while implementing Styles in App.xaml
StaticResource not found for key <keyForStyle>!
And its working fine in another project!
Does anyone have clue?
This is my App.xaml code--
<?xml version="1.0" encoding="utf-8" ?>
<Application.Resources>
And this how i am using it in some Xaml file--
Saved me too, @PaulDiston!
Saved me also @PaulDiston !
You are the Savior ! The Messiah !
Its works for me thanks.
This fixed my issue, but is it a good idea to change all my StaticResources to Dynamic? Won't it be detrimental to performance?
This fixed my issue,This saved me a lot of time.
@PaulDiston said:
This saved me a lot of time. Thanks @PaulDiston
Life saver! Thanks a million
Thanks, this worked for me too. Strange thing was that in debug mode it worked fine, it was only in Release mode that it didn't!