2016-11-16 81 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中的问题,谢谢 :) –