gpt4 book ai didi

c# - 仅当文件存在时才加载资源字典

转载 作者:行者123 更新时间:2023-11-30 21:45:45 24 4
gpt4 key购买 nike

有没有办法只在资源文件存在的情况下加载资源字典?在下面的例子中,我希望只在文件“Resources/AdditionalStyles.xaml”存在时允许使用资源字典

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles.xaml" />
<ResourceDictionary Source="Resources/AdditionalStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

最佳答案

您可以覆盖 App.xaml.cs 中的 OnStartup 方法,然后检查文件是否存在,如果存在则加载它:

protected override void OnStartup(StartupEventArgs e)
{
var fileName = Environment.CurrentDirectory() + @"\Resources\AdditionalStyles.xaml";

// Check if the AdditionalStyles.xaml file exists
if (File.Exists(fileName)
{
try
{
// try and load the file as a dictionary and add it the dictionaries
var additionalStylesDict = (ResourceDictionary)XamlReader.Load(fs);
Resources.MergedDictionaries.Add(additionalStylesDict);
}
catch (Exception ex)
{
// something went wrong loading the resource file
}
}

// any other stuff on startup

// call the base method
base.OnStartup(e);
}

关于c# - 仅当文件存在时才加载资源字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com