2010-11-15 34 views
1

我一直在尝试重新组合工作,但没有运气......我尝试了很多次和许多批评 - 没有运气......任何人都可以指出我的错误?我希望以后我滴个新的.dll文件到插件目录中的发件人集合将自动与新的东西重新填充...mef - 如何自动重构工作?

//exported classes 
[Export(typeof(ISender))] 
public class SMTP : ISender 
{ 
    public string Name 
    { 
     get { return "SMTP plugin"; } 
    } 

    public void Send(string msg) 
    { 

    } 
} 

[Export(typeof(ISender))] 
public class Exchange : ISender 
{ 
    public string Name 
    { 
     get { return "Exchange plugin"; } 
    } 

    public void Send(string msg) 
    { 
     // .. blah 
    } 
} 

/---------------- -------------------------------------------------- ---

/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    private const string STR_Pugins = ".\\plugins"; 


    [ImportMany(typeof(ISender), AllowRecomposition = true)] 
    private List<ISender> Senders; 

    private DirectoryCatalog d; 
    CompositionContainer c; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     listBox1.DisplayMemberPath = "Name"; 

     ConfigPlugins(); 
     bindSenders(); 
    } 

    private void ConfigPlugins() 
    { 
     DirectoryInfo dir = new DirectoryInfo(STR_Pugins); 

     if (!dir.Exists) 
      dir.Create(); 

     d = new DirectoryCatalog(STR_Pugins); 
     d.Changed += new EventHandler<ComposablePartCatalogChangeEventArgs>(d_Changed); 
     c = new CompositionContainer(d); 
     c.ExportsChanged += new EventHandler<ExportsChangeEventArgs>(c_ExportsChanged); 

     c.ComposeParts(this); 
    } 

    void d_Changed(object sender, ComposablePartCatalogChangeEventArgs e) 
    { 
     bindSenders(); 
     MessageBox.Show("d_Changed " + (Senders == null ? 0 : Senders.Count)); 
    } 

    private void bindSenders() 
    { 
     listBox1.ItemsSource = Senders; 
    } 

    void c_ExportsChanged(object sender, ExportsChangeEventArgs e) 
    { 
     bindSenders(); 
     MessageBox.Show("c_ExportsChanged "+ (Senders == null ? 0 : Senders.Count)); 
    } 
} 





响应后 好吧,我已经添加了刷新,但我仍然不知道为什么在列表框不会与新的数据填充...

public partial class MainWindow : Window 
{ 
    private const string STR_Pugins = ".\\plugins"; 


    [ImportMany(typeof(ISender), AllowRecomposition = true)] 
    private List<ISender> Senders; 

    DirectoryCatalog d; 
    CompositionContainer c; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     listBox1.DisplayMemberPath = "Name"; 

     ConfigPlugins(); 
     bindSenders(); 
    } 

    private void ConfigPlugins() 
    { 
     DirectoryInfo dir = new DirectoryInfo(STR_Pugins); 

     if (!dir.Exists) 
      dir.Create(); 

     d = new DirectoryCatalog(STR_Pugins); 
     c = new CompositionContainer(d); 

     c.ComposeParts(this); 
    } 

    private void bindSenders() 
    { 
     label1.DataContext = Senders; 
     listBox1.ItemsSource = Senders; 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     d.Refresh(); 
     bindSenders(); 
    } 
} 

回答

4

您必须自己拨打Refresh。如果你想要的话,你可以使用一个FileSystemWatcher对象在目录内容改变时得到通知。

+0

你可以看到我的编辑 - 所以WHYYYY仍然不会填充?! – argh 2010-11-15 19:20:37

1

它不会重新填充,因为当字段更新时,全新列表被设置为该字段。现有的集合未被修改。您必须将其设置为属性而不是字段(您仍然可以将其设置为受保护或专用),然后在调用“set”时更新listBox1.ItemsSource和label1.DataContext。