在UWP/C+中的代码隐藏中加载ResourceDictionary+;
我试图在运行时加载存储在文件中的ResourceDictionary。在C#中,它看起来就像在UWP/C+中的代码隐藏中加载ResourceDictionary+;,uwp,win-universal-app,c++-cx,Uwp,Win Universal App,C++ Cx,我试图在运行时加载存储在文件中的ResourceDictionary。在C#中,它看起来就像 ResourceDictionary resourceDictionary = new ResourceDictionary(); resourceDictionary.Source = new Uri("ms-appx:///!UWP/Styles/UWPDictionary.xaml", UriKind.Relative); Application.Current.Resources.MergedD
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.cpp的构造函数中的InitializeComponent()之后执行。正确设置了源(执行创建URI时没有任何错误)
最后一行MergedDictionaries->Append(rd)
引发异常:
在wp_UWP.exe中的0x7464A6F2(KernelBase.dll)处引发异常:0x40080201:WinRT原始错误(参数:0x8000FFFF、0x00000016、0x0D30F274)。
在WPUUWP.EXE中0x7464 A6F2中抛出的异常:微软C++异常:平台::COMPUTION ^在内存位置0x0D30F714。HRESULT:0x8000FFFF灾难性故障
WinRT信息:灾难性故障
wp_UWP.exe:0xc00027b中0x0C9E571A(Windows.UI.Xaml.dll)处发生未经处理的异常:发生应用程序内部异常(参数:0x00F1CA10,0x00000002)
如何修复此代码?我不明白它为什么会抛出这样的“灾难性故障”异常。您可以在初始化主页或主页的构造函数时放入代码,它将正常运行:
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;
}
..
..
}
..
..
}
void应用程序::OnLaunched
(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^e)
{
auto rootFrame=dynamic_cast(窗口::当前->内容);
//当窗口已经有内容时,不要重复应用程序初始化,
//只需确保窗口处于活动状态
if(rootFrame==nullptr)
{
//如果尚未加载,请加载字典
如果(!resourcesLoaded){
auto rd=ref new ResourceDictionary();
rd->Source=ref新Uri(“ms-appx:///Dictionary.xaml");
应用程序::当前->资源->合并词典->追加(rd);
resourcesLoaded=true;
}
..
..
}
..
..
}
看起来它实际上在任何地方都能工作,除了应用程序内构造函数,我不知道为什么。结果是,OnLaunched()和修复app.xaml中存在的问题都能工作。谢谢:)