If you put a valid uncompiled resource dictionary as a .xaml file in the folder alongside your exe then you can merge it using the same sort of xaml you'd use with a resource dictionary which is compiled into your exe.
Here's app.xaml from the sample I mentioned.
<Application x:Class="Wpf_Dynamic_XAML_FlatResourceDictionary.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="FlatFile.xaml"/>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In the sample FlatFile.xaml is a resource dictionary whose properties are Content and copy if newer.
You could omit that from the solution entirely and copy the resource dictionary manually next to the exe instead and it would still merge.
You may do the equivalent in code.
In the map builder for our game you can choose to merge in an uncompiled .xaml file as a resource dictionary. This then allows you to build content external to the map editor and import into it.
The user picks a file name off a list of files in a specific folder.
The application has no reference to the content at all and a user can add or remove files externally as they wish.
var rdUrl = System.IO.Path.Combine(FilesIO.MapsCustomResourcesFolder, vm.SelectedFile.Name + ".xaml");
try
{
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(rdUrl, UriKind.Absolute);
Application.Current.Resources.MergedDictionaries.Add(rd);
}
catch (Exception ex)
{
// Some user message stuff
}
Bear in mind that if you let the users manually edit a resource dictionary then they could well make changes which break it. An invalid resource dictionary merged in app.xaml will crash your app on load.
Code and error trapping can explicitly tell a user they broke their resource dictionary and allow them to carry on using your app.
You could alternatively use XamlReader.Load() to generate a resource dictionary from a file and merge that resource dictionary.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…