0

私の目標は、各ページの背景画像を設定することです。この構造を考えてみてください...

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

私の最初の試みは、このサイトの他の場所で提案されているように、ルートフレームに設定することでした... ;)

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には Build Action がありContentます。

以下のコードは問題なく動作します。

MainPage.xaml

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

しかし、それは私が望むものではありません。各ページに設定したくありません...

私が何を台無しにしているのか誰にも分かりますか?;)

4

2 に答える 2

1

くだらない。間違った場所; 早くしました。コンストラクターでは、設定を上書きするだけ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;
}
于 2012-07-19T20:12:09.550 に答える
-1

XAML で各ページの背景を個別に設定する際の問題は何ですか? 背景を動的にする必要がある場合は、状態フラグを実装できます。これにより、アプリケーションに現在の状態が通知されるため、どの壁紙を読み込むかがわかります。

これをコンバーターにバインドされた背景 (状態を に変換ImageBrush) と組み合わせると、解決策が得られます。

于 2012-07-19T20:04:58.947 に答える