如果主题更改,则更改 WP7 应用程序的背景 [英] Change Background of WP7 Application if Theme is altered

查看:27
本文介绍了如果主题更改,则更改 WP7 应用程序的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户将主题从 "Light" 更改为 "Dark" 或在后面的代码中反之,我需要更改我的应用程序的背景图像.我希望这些应该在 Page Loded 事件

I need to change the Background Image of my Application if user changes theme from "Light" to "Dark" or vice-vesa in code behind. I hope these should be done in Page Loded event

推荐答案

@TimDams 向您指出了一种检测现在设置什么主题的好方法,但我没有注意到有任何关于如何在应用程序运行时检测主题的变化.用户可以启动您的应用,然后前进到菜单,更改主题,然后返回到您的应用.虽然您可能认为您的应用会被墓碑删除,然后重新启动并在所有页面加载的完整周期内重新导航到您的页面 - 但并非 100% 正确.

@TimDams pointed you to one of the nice ways to detect what-theme-is-now-set, but I didn't notice there any information how to detect a change to the theme during the application runtime. The user could start your app, then bump forward to the menu, change the theme, and get back to your app. While you may think that your app will be tombstoned and then restarted and renavigated to your page with full cycle with all pageloads -- it is not 100% true.

首先,PageLoaded 不是进行初始检查和设置样式的好地方,因为如果您调用该事件,那么页面可能已经呈现一次.如果我没记错的话,PageLoaded 是在第一次渲染之后调用的.如果这是真的,那么您将必须更早地检测颜色,例如在 LayoutUpdated 中(警告:这个事件是一个很好的垃圾邮件发送者.我的意思是,它被称为无数次.附加一个单次处理程序,你知道,例如这将在第一次调用时立即 unattach-self).也许您可以在 Page 的 .ctor 中完成,就在 InitializeComponent 之后.或者在 OnApplyTemplate 或 MeasureOverride 中,或者至少在ArrangeOverride 中——视觉效果应该大部分/完全可用.

Firstly, the PageLoaded is NOT a good place to do the initial check-and-set-styles, because, if you get that event called, then the page, probably, has already rendered once. If I remember well, the PageLoaded is invoked just after the first render. If this is true, then you will have to detect the colors earlier, for example in the LayoutUpdated (warning: this event is a great spammer. I mean, it gets called gazillions times. Attach a single-shot handler, you know, such that will instantly unattach-self on first invocation). Maybe you will be able to do it in the Page's .ctor, just after InitializeComponent. Or in the OnApplyTemplate or MeasureOverride, or at least ArrangeOverride -- the visuals should be mostly/fully available there.

Buuut.我故意将初始"一词加粗".在 Mango 中,一些多任务处理变得越来越普遍,但即使是 Mango 7.0 之前的版本也不能保证你的应用程序会被墓碑化.例如,根据我在 7.0 早期的观察,从 WebBrowser 组件启动 MediaPlayer 不会墓碑化您的应用程序:) 如果您有时间阅读,请查看 WP7 从墓碑恢复并返回页面,了解有关暂停"与墓碑"的详细信息.

Buuuut. I've intentionally 'bolded-out' the word "initial". With Mango, there's some multitasking getting more and more common, but even the pre-Mango 7.0 version does not guarantee that your app will be tombstoned. From my observations in early 7.0, for example, starting the MediaPlayer from WebBrowser component does not tombstone your app:) If you have some time to read, check WP7 recover from Tombstone and return to page for details on the "pause" vs "tombstone".

无论如何,如果您的应用被暂停"并且用户在此期间切换主题,我认为(我没有检查过)您的页面(在大多数情况下)只是暂时隐藏并返回屏幕时,它可能不会被重新创建,也不会被重新(页面)加载.如果这是真的,那么您将没有那么容易解决的问题,因为您的应用程序可能会暂停,操作系统重新主题化,并且您的应用程序几乎在任何时候都没有暂停,同时您将获得的唯一事件是 .... App.Deactivated 和 App.Activated 的全局事件.有可能完全没有每页事件会触发 [但我没有检查过 - 在你做我下面建议的任何事情之前,检查一下].

Anyways, if your app gets "paused" and the user switches themes in the meantime, I think (I've NOT checked) that your page will (in most of the cases) be just temporarily hidden and upon returning to the screen, it will probably not be re-created and will not be re-(Page)Loaded. If it is true, then you will have not so easy problem to solve, because your app may be paused, the OS rethemed, and your app unpased virtually in any moment of time and the only events you will get in the mean time are .... global events of App.Deactivated and App.Activated. It is possible that completely none of per-page events will fire [but I've not checked - before you do anything I suggest below, CHECK IT].

如果这种悲观的观点确实属实,那么在这些事件中,您将不得不检测当前主题(->Tim 的帖子),然后以某种方式通知您当前的主页主题已更改 - 或未更改.如果您的 ViewModel 与应用程序的其余部分至少分开一点(应该是:)),您有一个简单的选择来做到这一点:在该 ViewModel 中创建一组属性(dp 或 inotif),如画笔背景,Brush Foreground、Brush Hightlight 和其他您需要的,而不是在 XAML 中对颜色进行硬编码 - 绑定到这些属性.您可能想要为所有这些画笔和其他样式创建一个单独的类,比如说发布类 MyCurrentAppTheme"并将该道具保留在那里,并从 ViewModel 公开此类对象 - 无论如何.只需将您的颜色绑定到任何东西——但任何东西都是逻辑上全局的",并且可以从 App.Acticated 事件处理程序轻松访问.完成后,在 App.Activated 中,检测当前主题以及是否更改,因此通过保留在 VM 中的所有颜色并适当地设置它们.瞧,整个你的应用程序都得到了正确的重新着色.

If this pessimistic view is really true, than in those events, you will have to detect the current theme (->Tim's post), then somehow inform your current Page that themes changed - or not. If you have your ViewModel at least a bit separated from the rest of the app (as it should be:) ) you have an easy option to do it: create in that ViewModel a set of properties (dp or inotif) like Brush Background, Brush Foreground, Brush Hightlight, and other that you need, and instead of harcoding colours in your XAML - bind to those properties. You may event want to create a separate class for all those many Brushes and other Styles, let's say "pub class MyCurrentAppTheme" and keep that props there, and expose such object from ViewModel - whatever. Just Bind your colors to whatever -- but whatever that will be "logically global" and that will be easily accessible from the App.Acticated event handler. Having that done, in the App.Activated, detect the current theme and if changed, so through all the colors kept in VM and set them appropriatelly. Voil'a, whole your App gets recoloured properly.

但请注意——在渲染缓存的旧主题、刷新数据绑定对象和重绘新主题之间可能会有一些短暂的闪烁.我希望不会,但我感觉它可能会发生,尤其是从快速切换工具(长按后退)返回时:我认为设备会在后台缓冲区中捕获应用程序的最后一个屏幕截图",并始终使用它应用程序被最小化"以进行转换动画,显示快速应用程序切换概述等等......同样,我没有检查过,但我怀疑在这种动画期间页面内容是实时"的,它可能非常要求 CPU/GPU 资源.有人对此有所了解吗?在页面上放置一些循环动画,然后切换并检查快速切换概览,无论动画是移动还是停止,这可能是一个很好的测试!:)

But mind that still - there might be some transient blinks and flashes between the rendering of cached old theme, refreshing databound objects, and redrawing fresh theme. I hope not, but I sense it may occur, especially when returning from fast-switch tool (long back press): I think that the device captures the 'last screenshot' of your app in the backbuffer and uses that throughout all the time the app is 'minimalized' to do transitions animations, to show the fast-app-switch overview and so on.. again, I've not checked, but I doubt that during such animations the pagecontents are 'live', it could be very demaning on CPU/GPU resources. Any one knows anything on that? It could be a nice test to have some looping animation on the page and then to switch out and check in the fast-switch overview, whether the animation moves or is halted!:)

这篇关于如果主题更改,则更改 WP7 应用程序的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆