2016-11-16 28 views
0

我試圖在運行時加載存儲在文件中的ResourceDictionary。在C#中,它看起來只是作爲在代碼中加載ResourceDictionary在UWP/C++中

ResourceDictionary resourceDictionary = new ResourceDictionary(); 
resourceDictionary.Source = new Uri("ms-appx:///!UWP/Styles/UWPDictionary.xaml", UriKind.Relative); 
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary); 

,但相同的代碼(在C++/CX)不工作:

auto rd = ref new ResourceDictionary(); 
rd->Source = ref new Uri("ms-appx:///!UWP/Styles/UWPDictionary.xaml"); 
Application::Current->Resources->MergedDictionaries->Append(rd); 

當我得到它,這段代碼也應在App.xaml中執行之後InitializeComponent() .cpp的構造函數。源設置正確(創建URI時沒有任何錯誤)。

最後一行MergedDictionaries->Append(rd)拋出異常:

在異常在wp_UWP.exe 0x7464A6F2(KernelBase.dll)拋出:0x40080201:WinRT的起源錯誤(參數:0x8000FFFF,0x00000016,0x0D30F274)。 wp_UWP.exe中的0x7464A6F2引發異常:Microsoft C++異常:Platform :: COMException ^在內存位置0x0D30F714處。 HRESULT:0x8000FFFF災難性故障 WinRT的信息:災難性故障

未處理的異常在0x0C9E571A(Windows.UI.Xaml.dll)在wp_UWP.exe:0xC000027B:發生了一個應用程序的內部異常(參數:0x00F1CA10,0x00000002)。

如何修復此代碼?我不明白爲什麼會拋出這樣的'災難性失敗'例外。

回答

0

你可以把代碼時,你初始化主網頁或在主要頁面的構造函數,它會運行良好:

void App::OnLaunched 
(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) 
{ 
    auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content); 

    // Do not repeat app initialization when the Window already has content, 
    // just ensure that the window is active 
    if (rootFrame == nullptr) 
    { 
     // Load the dictionary if not already loaded 
     if (!resourcesLoaded) { 
      auto rd = ref new ResourceDictionary(); 
      rd->Source = ref new Uri("ms-appx:///Dictionary.xaml"); 
      Application::Current->Resources->MergedDictionaries->Append(rd); 
      resourcesLoaded = true; 
     } 
     .. 
     .. 
    } 
    .. 
    .. 
} 

看起來像它的實際工作無處不在,除了在應用程序的構造,我不知道爲什麼就是它。

+0

由於OnLaunched()和解決了一些問題的App.xaml中的問題,謝謝 :) –

相關問題