开发者

NotImplementedException when overriding default phone resources

I'm am adding the following code to the constructor of App.xaml.cs in my WP7 application.

Resources["PhoneBackgroundBrush"] = new ImageBrush
{
    ImageSource = new BitmapImage(new Uri("/images/bg.png", UriKind.Relative))
};

after running it I get NotImplementedException once the applicaion is opened.

Any idea of how can we do this?

What I'm trying to achieve is a single theme application like Facebook that always have white theme regardless of phone's theme.

As a note, getter on the resources always work, so it's not that accessing phone resources is totally bloc开发者_StackOverflow社区ked from code. (I'm using this to determine current theme and accent of phone).

var a = Resources["PhoneBackgroundBrush"]; // this works fine


For simple examples it isn't complicated when you know how, however you shouldn't rename PhoneBackgroundBrush, but create a new key for your resources.

You need to create the resources in App.xaml (or you can pull in MergedDictionaries other places too, but that's more advanced). Test.jpg needs to be a resource file in your project.

In App.xaml:

<Application.Resources>
    <ImageBrush x:Key="MyImageBrush" ImageSource="Test.jpg"/>
</Application.Resources>

In a page for example:

Background="{StaticResource MyImageBrush}"

If you're starting to re-template a control (say a Button), that when you need to crack open Blend and modify the default styles of controls. It's pretty well documented, and builds on these same principles...


Basically the reason you're getting a NotImplementedException is because the setter for the Application.Resources is implemented like this:

set 
{
    throw new NotImplementedException();
}

The reason Facebook is all white, is because they defined their own colour resources, and used them everywhere. I've also made a always-white themed app. It just requires a little extra effort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜