2013-11-29 3 views
1

Я пытаюсь изменить фон моего приложения для магазина 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; 
+0

, где вы запустите код? Вы уверены, что «путь» указывает на какое-то допустимое изображение? –

+0

Я написал функцию под названием changeBackground, и я вызываю ее, когда нажимаю кнопку на главной странице. Я знаю, что изображение находится в папке моих ресурсов. – Mirimari

ответ

0
this.RootFrame.Background= new ImageBrush 
{ 
    ImageSource = new BitmapImage(new Uri("/Assets/rainySky.png", UriKind.Relative)) 
}; 
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(); 
} 
+0

Что делать, если я хочу изменить его после запуска? – Mirimari

Смежные вопросы