棱镜+ MEF:如何将参数正确加载到我的一项服务中?
基本上我有以下场景:
App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
string x = (e.Args.Length > 0) ? e.Args[0];
string y = (e.Args.Length > 1) ? e.Args[1];
Bootstrapper bootstrapper = new MyBootstrapper(x, y);
bootstrapper.Run();
}
MyBootstrapper.cs:
public sealed class MyBootstrapper : MefBootstrapper
{
private string _x;
private string _y;
public MyBootstrapper(string x, string y)
{
_x = x;
_y = y;
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<ClientShell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
FooBarService.cs
public interface IFooBarService
{
string x { get; }
string y { get; }
}
[Export("FooBarService", typeof(IFooBarService))]
public class FooBarService : IFooBarService
{
string x { get; protected set; }
string y { get; protected set; }
}
如何将 x 和 y 加载到我的服务正常吗?另外,如何确保执行此操作时它不会与我的容器等中的其他任何内容发生冲突?
Basically I have the following scenario:
App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
string x = (e.Args.Length > 0) ? e.Args[0];
string y = (e.Args.Length > 1) ? e.Args[1];
Bootstrapper bootstrapper = new MyBootstrapper(x, y);
bootstrapper.Run();
}
MyBootstrapper.cs:
public sealed class MyBootstrapper : MefBootstrapper
{
private string _x;
private string _y;
public MyBootstrapper(string x, string y)
{
_x = x;
_y = y;
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<ClientShell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
FooBarService.cs
public interface IFooBarService
{
string x { get; }
string y { get; }
}
[Export("FooBarService", typeof(IFooBarService))]
public class FooBarService : IFooBarService
{
string x { get; protected set; }
string y { get; protected set; }
}
How do I load x and y into my service properly? Also, how do I ensure that when doing this it doesn't collide with anything else in my Container or the such?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必使用 StartupEventArgs。您的 FooBarService 可以简单地使用 Environment.GetCommandLineArgs ,如下所示:
编辑:我不确定 args[0] 是第一个参数还是程序调用,您必须尝试一下,在这种情况下进一步切换索引!
You don't have to use the StartupEventArgs. Your FooBarService could simply use Environment.GetCommandLineArgs like this:
EDIT: I'm not sure if args[0] is the first parameter or the program call, you have to try that out and in that case switch indexes one further!