我的應用程序中出現了一些奇怪的黑魔法。ImageBrush在不調試應用程序時呈現白色
我已經在樣式字典中定義的圖像刷:
<classes:MultiResImageChooser x:Key="MultiResImageChooser"/>
<ImageBrush x:Name="SplashScreenImageBrush"
ImageSource="{Binding SplashScreenResolutionImage, Source={StaticResource MultiResImageChooser}}"
Stretch="Fill" />`
的MultiResImageChooser類有一個簡單的屬性:
public class MultiResImageChooser
{
public BitmapImage SplashScreenResolutionImage
{
get
{
switch (ResolutionHelper.CurrentResolution)
{
case Resolutions.HD720p:
return new BitmapImage(new Uri("/Images/SplashScreenImage.Screen-720p.jpg", UriKind.Relative));
case Resolutions.WXGA:
return new BitmapImage(new Uri("/Images/SplashScreenImage.Screen-WXGA.jpg", UriKind.Relative));
case Resolutions.WVGA:
return new BitmapImage(new Uri("/Images/SplashScreenImage.Screen-WVGA.jpg", UriKind.Relative));
default:
throw new InvalidOperationException("Unknown resolution type");
}
}
}
}
SplashScreenImageBrush被綁定到一個Border元素的背景屬性:
<Border x:Name="SplashScreen"
Background="{StaticResource SplashScreenImageBrush}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
所以,問題是,當我調試應用程序在WP8仿真器或WP8設備上的一切工作正常。 在未經調試的情況下啓動應用程序時,Border background屬性呈現爲白色。 圖像文件包含在項目中,並將「生成操作」設置爲「內容」。
此外,如果我將ImageSource直接設置爲圖像路徑,則一切正常。
所以,這個問題似乎是MultiResImageChooser,但我不知道什麼可能是錯的。
任何形式的幫助或提示將不勝感激。
順便說一句,這個問題沒有得到再現W上WP7.1設備和仿真器。
我敢打賭:'ResolutionHelper.CurrentResolution'由於某種原因(計時問題?)無法正常工作,所以執行了交換機的「默認」分支。因此,您的綁定失敗,畫筆不會被初始化,而是會變爲白色。從那裏開始,我會首先確認「默認」分支的執行情況,例如通過放置特定圖像而不是拋出異常。然後,如果我的理論是正確的,請查看「ResolutionHelper」以瞭解發生了什麼。 – 2013-05-06 14:39:54
@KooKiz謝謝!你的投注確實讓我指向了正確的方向。 ResolutionHelper實際上是崩潰。我修好了,現在一切正常。謝謝! – Mike 2013-05-06 19:18:26