简体   繁体   中英

WPF+MAF - addin referenced libraries are not loaded?

I'm new to Add-in framework. Now I'm trying to create WPF addin that returns UserControl (according to https://msdn.microsoft.com/en-us/library/bb909849(v=vs.110).aspx ).

The problem is that when I try to get user control from addin, Exceptions are thrown:

  1. The first exception was "Cannot find resource named ''. Resource names are case sensitive.". All the resources are stored in separate library and it worked fine when I did the same with MEF (before MAF)

  2. OK, I've removed all StaticResources from my UserControl and then I got another exception: "{"Could not load file or assembly 'Infralution.Localization.Wpf, PublicKeyToken=547ccae517a004b5' or one of its dependencies."}". This lib is used for localization.

Note: all needed libraries are in the same folder as the addin is

Are addin referenced libraries not loaded? Or where was I mistaken?

I've found out that when an addin is activated, its Application.Current is null. Earlier I did not added resources to each window, but every addin lib contained App.xaml file with resources. So, the compilation of project was successfull, VS WPF Designers worked fine, and it worked fine when I used MEF (in one appdomain). But, as I was saying, when I tried MAF, addins has no Application.Current instance, so it has no resources at all.

Now, to make every AddIn use the style of host application, I've created contract:

public interface IThemeContract : IContract
{
    /// <summary>
    /// Theme name
    /// </summary>
    string Name { get; }
    /// <summary>
    /// Theme description
    /// </summary>
    string Description { get; }
    /// <summary>
    /// Array of expected resources dictionaries
    /// </summary>
    IListContract<string> ResourceDictionaries { get; }
}

Host application serializes all its resources; a piece of HostAdapter:

    System.AddIn.Contract.IListContract<string> IThemeContract.ResourceDictionaries
    {
        get
        {
            var list = new List<string>();
            foreach (var s in _view.ResourceDictionaries)
            {
                list.Add(XamlWriter.Save(s));
            }
            return CollectionAdapters.ToIListContract<string>(list);
        }
    }

and sends it to addins; then addins deserializes it; a piece of AddInAdapter:

    public override System.Windows.ResourceDictionary[] ResourceDictionaries
    {
        get
        {
            var strings = CollectionAdapters.ToIList<string>(_contract.ResourceDictionaries);
            var rds = new List<System.Windows.ResourceDictionary>();
            foreach (var s in strings)
            {
                var output = XamlReader.Parse(s);
                if ((output as System.Windows.ResourceDictionary) != null)
                    rds.Add(output as System.Windows.ResourceDictionary);
            }
            return rds.ToArray();
        }
    }

Then the application looks like a single unit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM