簡體   English   中英

WP7-在所有頁面上設置背景圖像

[英]WP7 - Set background image on all pages

我的目標是為每頁設置背景圖片。 考慮這種結構...

/Images/AppBackground.jpg
/App.xaml
/MainPage.xaml
/Page2.xaml

我的第一次嘗試是按照本網站其他地方的建議將其設置在rootframe上。

App.xaml.cs

ImageBrush brush = new ImageBrush
{
    ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/AppBackground.jpg", UriKind.Relative)),
    Opacity = 0.5d
};
this.RootFrame.Background = brush;

這將給我一個錯誤: A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

我嘗試了不帶/嘗試,嘗試了UriKind.Absolute ,並且沒有UriType參數,所有操作都會給我同樣的錯誤。 圖像AppBackground.jpg具有構建動作Content

下面的代碼將正常工作。

MainPage.xaml

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <ImageBrush ImageSource="/Images/AppBackground.jpg"></ImageBrush>
    </Grid.Background>
    ...

但這不是我想要的,我不想為每個頁面設置它...

有人知道我搞砸了嗎? ;)

廢話 錯誤的地方; 我來得早。 在構造函數中,它將簡單地覆蓋Background設置...

如果我稍后執行它,它將很好用... :)

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
    ImageBrush brush = new ImageBrush
    {
        ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/AppBackground.jpg", UriKind.Relative)),
        Opacity = 0.5d
    };
    this.RootFrame.Background = brush;
}

在XAML中分別為每個頁面設置背景有什么問題? 如果背景是動態的,則可以實現一個狀態標志,該標志將告訴應用程序處於何種狀態,從而知道要加載的牆紙。

再加上綁定到轉換器的背景(將狀態轉換為ImageBrush ),您便可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM