将背景更改为图像

时间:2013-11-29 02:51:30

标签: c# image background windows-store-apps

我正在尝试更改Windows应用商店应用的背景。这是我正在使用的代码,它不会抛出错误但由于某种原因它不起作用。有人知道如何更改主页的背景吗?

string path = "ms-appx:///Assets/rainySky.png";

ImageBrush image = new ImageBrush();
image.ImageSource = new BitmapImage(new Uri(path));

Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Background = image;

2 个答案:

答案 0 :(得分:1)

如果要在启动应用程序时设置背景图像,可以在OnLaunched事件中使用以下代码:

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();

        rootFrame.Background = new ImageBrush
        {
            Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
            ImageSource = new BitmapImage { UriSource = new Uri("ms-appx:///Assets/Image.jpg") }
        };

        if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application
        }

        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }

    if (rootFrame.Content == null)
    {
        // When the navigation stack isn't restored navigate to the first page,
        // configuring the new page by passing required information as a navigation
        // parameter
        if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
        {
            throw new Exception("Failed to create initial page");
        }
    }
    // Ensure the current window is active
    Window.Current.Activate();
}

答案 1 :(得分:0)

this.RootFrame.Background= new ImageBrush
{
    ImageSource = new BitmapImage(new Uri("/Assets/rainySky.png", UriKind.Relative))
};