您如何从库中访问通用主机服务?
我有一个WIN UI3项目,该项目在主应用程序中宣布了一个通用主机,用于依赖性注入等。主应用程序将支持许多不同的文档类型,以使其更易于管理,我想将代码和UI分开(每种文档类型的页面XAML和查看模型)中的每个文档类型。
在主应用程序中,页面是由frame.navigate方法实例化的,然后使用依赖项注入(app.host.host.services.getService(typeof(t))作为t; <<<<<
app.host.services.getservice.getService.getServices.getServices.getServices.getServices.getServices.getServices.getServices.getServices. /代码>主机是应用程序中定义的静态属性)。但是,当库中定义帧时,它无法访问主机。
以下作品,但这太可怕了。有更好的方法吗?
var apptype = System.Reflection.Assembly.GetEntryAssembly().GetType("SWFieldData.App");
PropertyInfo propertyInfo = apptype.GetProperty("_host", BindingFlags.NonPublic | BindingFlags.Static);
var _host = (Microsoft.Extensions.Hosting.IHost)propertyInfo.GetValue(null, null);
ViewModel = _host.Services.GetService(typeof(SetupDataViewModel)) as SetupDataViewModel;
I have a Win UI3 project which declares a generic host in the main app which is used for dependency injection, etc. The main application will support many different document types, to keep it more manageable I'd like to separate the code and UI (page XAMLs and View Models) for each document type into a separate libraries.
Within the main app the pages are instantiated by the Frame.Navigate method, then the code behind for that page creates the view model using Dependency Injection (App.Host.Services.GetService(typeof(T)) as T;
where Host is a static property defined in the app). However, when the frame is defined in a library it doesn't have access to the Host.
The following works, but it's horrendous. Is there a better method?
var apptype = System.Reflection.Assembly.GetEntryAssembly().GetType("SWFieldData.App");
PropertyInfo propertyInfo = apptype.GetProperty("_host", BindingFlags.NonPublic | BindingFlags.Static);
var _host = (Microsoft.Extensions.Hosting.IHost)propertyInfo.GetValue(null, null);
ViewModel = _host.Services.GetService(typeof(SetupDataViewModel)) as SetupDataViewModel;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然后,您应该将服务的主机移至库本身,或者从库中引用的另一个库,该图书馆需要能够解决服务。
Then you should move the host of the services to the library itself, or another library that is referenced from the library which needs to be able to resolve the services.