Trouble loading Baml using LoadComponent
Hello,
I'm trying to load some baml from resource assemblies using Application.LoadComponent. As a parameter I use Uri that contains full name of assembly with culture: "en". But LoadComponent gives me IOException, that same assembly, but with culture: "neutral", was not found.
here is a sample that first loads PresentationFramework Luna baml, and then tries to load mainwindow.baml from Sparkle. Luna baml is loading as expected, but Sparkle baml throws exception:
staticreadonlystring PROGRAMFILES=Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
staticreadonlystring PRESENTATION =Path.Combine(PROGRAMFILES,
"Reference Assemblies\\Microsoft\\WinFx\\v3.0\\PresentationFramework.Luna.dll");
staticreadonlystring SPARKLE =Path.Combine(PROGRAMFILES,"Microsoft Expression\\Interactive Designer\\March 2006 CTP\\en\\Microsoft.Expression.Framework.resources.dll");staticvoid Main()
{
StartPresentation();
StartSparkle();
}privatestaticvoid StartSparkle()
{
Start(SPARKLE,"resources/userinterface/mainwindow.baml");
}privatestaticvoid StartPresentation()
{
Start(PRESENTATION,"themes/luna.normalcolor.baml");
}privatestaticvoid Start(string assemblyFile,string resourceName)
{
Assembly assembly =Assembly.LoadFile(assemblyFile);
Uri uri =newUri(assembly.FullName +";component/" + resourceName,UriKind.RelativeOrAbsolute);
object bamlObject =Application.LoadComponent(uri);
}Is it bug? How can I load baml from such assemblies?
Second problem. If baml was build from Application it can't be loaded using Application.LoadComponent. It gives Exception, that Application can have only one instance in AppDomain. The question: how to load baml containing Application?
[4231 byte] By [
NesherHH] at [2007-12-20]
The assembly needs to be declared as a reference assembly in your application project. Further, you need to use a short name for the assembly. I don't think you need to specify the UICulture either. WPF will figure that out and look at the satellite resource assembly if required.
e.g. if Bar.xaml is in Foo.dll which is declared as reference assembly, then passing "/Foo;component/Bar.xaml" to LoadComponent should work. You shouldn't have to specify Bar.baml. WPF knows to load the BAML if you specify XAML. BAML is internal and should be seen as an implementation detail. Please do not take a dependency on it. Instead, please code to XAML. This way if the team decides to change BAML to XYZ (I'm not saying this will happen, but is a risk if you rely on internal formats), your app is not affected because you reference it as XAML.
I'm writing a program, that allows to see resources embeded in assembly. It's like a Resource Viewer. I can't reference assemblies, because this program should open any assembly that has resources. Same thing with XAML-BAML. All XAML code is being compiled in BAML and integrated as resource in assembly. To see the code, the program have to load a BAML.
What if XAML/BAML contains Application.Resources? Application can't be instantiated second time in the same AppDomain.
I can't answer your question directly because I haven't got that much experience with LoadComponent other than that I have it working loading assemblies that are not referenced and extracting the baml.
I would suggest reading this post http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=503651&SiteID=1 as it covers LoadComponent.
Also make sure that in your project/msbuild file you are importing the winfx build target:
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
if you don't have that then you won't get any baml embedded in the assembly.
Hopefully that will help some.
BAML is an internal format and is not supported. Please do not take a dependency on the format or even the existence of BAML. Going forward, we have strong intentions of improving the plumbing and do not want to break applications that depend on BAML.
I understand your scenarios but unfortunately don't have any advice at this time other than to rely on XAML. This will work in most cases: for example, the resource loading mechanism understands a XAML URI and will load the appropriate page for you. This abstraction is intuitive (app authors use and program to XAML) and will continue to be supported even as the underlying mechanism changes.