简体   繁体   中英

Using PhoneAccentBrush in WP8.1 Universal app

I have a listview in a page specific to the WP8.1 portion of the solution, with a standard ObservableCollection as the itemssource. I would like to highlight the selected item, which for the moment is using a datatemplate of just a textblock. It seems this feature is supported in the base WP8.1 control, only in the W8.1 version.

Following the solution presented here; How to change color of the selected ListView item [WP8.1] I managed to get it to highlight with a specified colour, but I'd really like to be able to bind to the phone's accent colour, which I'm certain that I was able to bind to this in WP8.0, but the resource isn't available in the page in the universal app.

for the moment I have replaced the direct colour specification of 'red' with

<ColorAnimation Duration="0" Storyboard.TargetName="myback" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="{ThemeResource PhoneChromeColor}"/>

which works to highlight the item, but obviously not the colour I want.

Can I get access to the PhoneAccentBrush in the xaml? Is there a better way to achieve my goal? I am not a professional developer, so some 'user friendly' guidance would be most helpful. Thanks.

The To property of ColorAnimation expects an object of type Color. PhoneAccentBrush is of the SolidColorBrush type.

Here are two suggestions (I haven't tested them, but I think they should work):

  1. Objects of the SolidColorBrush type have a property Color of type Color which you can use like this {Binding Color, Source={StaticResource PhoneColorBrush}} .

  2. You can also use the PhoneAccentColor static resource, which is basically the color of the PhoneAccentBrush brush, like this: {StaticResource PhoneAccentColor} .

Also, if you're not really animating the color, you can just change the whole brush with something like this:

<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="myback" Storyboard.TargetProperty="(Border.Background)">
    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>

I hope some of these would work for you! :)

I want to start by agreeing that @Scoregraphic is correct. Your error message is something that is unique to you. That being said, you should know that the accent brush on Windows is always purple. Using the accent brush has little value because of this.

You can overcome this with device-specific styling. This means you can use accent brush in your Phone app but in your Windows app you add whatever logic you want to have a color that makes sense.

I discuss device-specific styling more here:

http://channel9.msdn.com/series/Developing-Universal-Windows-Apps-with-C-and-XAML/03#time=27m00s

Best of luck!

PhoneAccentBrush可通过{StaticResource PhoneAccentBrush}在XAML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM