2013-11-29 72 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