开发者

MEF: Imported field value not updating

I am trying to export a field called Settings from my Main form to a Plugin as shown in the code below. I am using the Main Form function called CreateSettings() to update the private _settings field. But, when I do that, the imported Plugin Settings never changes and is always the original initialized values "defaultname" and "defaultpass". I am not sure what is going on?

Main Form:

public partial class Form1 : Form
{
    [Export(typeof(ISettings))]
    private Settings _settings = new Settings("defaultname", "defaultpass");

    private void CreateSettings(name, password)
    {
        _settings = new Settings(name, password);
    }
}

Plugin Control:

[Export(typeof(IPlugin))]
public partial class MyPlugin : UserControl, IPlugin
{       
    [Import(typeof(ISettings))]
    private Settin开发者_如何学JAVAgs _settings;         
}

Settings Class:

public class Settings : ISettings
{
    public string Name { get; set; }
    public string Password { get; set; }

    public Settings()
    {
    }

    public Settings(string name, string pass)
    {
        Name = name;
        Password = pass;
    }
}


Once the import has been resolved, changing the original export to a new instance will not update the importing classes. If you need to actually change the instance reference, one option is to wrap it up in some other object whose reference won't change and Import that reference.

Alternately, you can perform dynamic recomposition, using a technique as outlined here. I think it's cleaner to simply import a context 'service' which exposes the mutable settings instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜