酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

Windows PhoneでRootFrameの背景画像を変更する

RootFrameのBackgroundプロパティにImageBrushのインスタンスを設定することで、背景画像を変更することが出来ます。

private static void SetAppBackground(string imageName)
{
    var app = Application.Current as App;
    if (app == null) return;

    var imageBrush = new ImageBrush
    {
        ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(imageName, UriKind.Relative))
    };
    app.RootFrame.Background = imageBrush;
}

private void button4_Click(object sender, RoutedEventArgs e)
{
    SetAppBackground("SplashScreenImage.jpg");
}

少しサンプルが判りにくいですが、実行前のスクリーンショットです。

f:id:ch3cooh393:20141117215120p:plain

こちらが実行後のスクリーンショットです。

f:id:ch3cooh393:20141117215146p:plain

SplashScreenImage.jpgがプロジェクトに追加されており、ビルドアクションが「Resource」に設定されているのであれば、以下のようにXAMLで背景画像を設定する事も可能です。

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot">
<Grid.Background>
    <ImageBrush ImageSource="SplashScreenImage.jpg" />
</Grid.Background>

参照