显示包含接口的表单作为构造函数 WPF 的参数
showing form containing interface as parameter to a constructor WPF
在成功 login.I 将接口用作 main window 中构造函数的参数后,我正在尝试显示 main window。现在,当我尝试显示 main window 时,出现错误,因为我无法将接口作为参数传递给 main window.I 看到很多像我的帖子,但我认为它与它们完全不同。
这是我的主要 window 构造函数:
public Home_Page(IGetAllItemClass clas)
{
InitializeComponent();
_allClass = clas;
}
IGetAllItemClass _allClass;
我的登录代码 Window 我需要显示主表单的地方:
Home_Page h = new Home_Page();
h.ShowDialog();
我的app.xaml:
<Application x:Class="Cafe_WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Cafe_WPF"
Startup="App_Startup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="scroll_style.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
这是我的 App.cs :
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
#region login_dependencies
var container = new UnityContainer();
container.RegisterType<IGetIP, get_ip_address>();
container.RegisterType<IUserDetails, get_user_details>();
container.RegisterType<IgetBusinessDetailsFromPosId, get_business_info_from_pos>();
#endregion
#region home_page_dependencies
var home = new UnityContainer();
container.RegisterType<IGetAllItemClass, get_all_item_class>();
Home_Page hm = home.Resolve<Home_Page>();
#endregion
Login_Window lg = container.Resolve<Login_Window>();
lg.ShowDialog();
}
}
这是我的界面:
namespace Cafe_WPF.Interface
{
public interface IGetAllItemClass
{
DataTable item_class(string business_info_id, string rvc_id);
}
}
而我的 class 服务实现接口是:
class get_all_item_class : IGetAllItemClass
{
public DataTable item_class(string business_info_id, string rvc_id)
{
try
{
string sql = //query
return CafePOS.Library.DataAccessLayer.Instance.ExecuteQuery(sql);
}
catch (Exception ex)
{
throw ex;
}
}
}
我正在尝试使用依赖性 injection.May 因为我遗漏了一些东西。谁可以帮我这个事 ?我坚持这个。
你需要用构造函数传递依赖
var home = new HomePage(myDependencyInterface);
如果您没有在登录页面中实例化该依赖项,那么您必须先使用 Unity 容器解决它:
var myDependencyInterface = container.Resolve<myDependencyInterface>();
另外,正如您在问题的评论中所问的那样,您在使用 Prism 吗?
因为如果是这样你只需要使用 NavigateAsync(nameof(HomePage));
并且 Unity 将注入依赖项
在成功 login.I 将接口用作 main window 中构造函数的参数后,我正在尝试显示 main window。现在,当我尝试显示 main window 时,出现错误,因为我无法将接口作为参数传递给 main window.I 看到很多像我的帖子,但我认为它与它们完全不同。
这是我的主要 window 构造函数:
public Home_Page(IGetAllItemClass clas)
{
InitializeComponent();
_allClass = clas;
}
IGetAllItemClass _allClass;
我的登录代码 Window 我需要显示主表单的地方:
Home_Page h = new Home_Page();
h.ShowDialog();
我的app.xaml:
<Application x:Class="Cafe_WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Cafe_WPF"
Startup="App_Startup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="scroll_style.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
这是我的 App.cs :
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
#region login_dependencies
var container = new UnityContainer();
container.RegisterType<IGetIP, get_ip_address>();
container.RegisterType<IUserDetails, get_user_details>();
container.RegisterType<IgetBusinessDetailsFromPosId, get_business_info_from_pos>();
#endregion
#region home_page_dependencies
var home = new UnityContainer();
container.RegisterType<IGetAllItemClass, get_all_item_class>();
Home_Page hm = home.Resolve<Home_Page>();
#endregion
Login_Window lg = container.Resolve<Login_Window>();
lg.ShowDialog();
}
}
这是我的界面:
namespace Cafe_WPF.Interface
{
public interface IGetAllItemClass
{
DataTable item_class(string business_info_id, string rvc_id);
}
}
而我的 class 服务实现接口是:
class get_all_item_class : IGetAllItemClass
{
public DataTable item_class(string business_info_id, string rvc_id)
{
try
{
string sql = //query
return CafePOS.Library.DataAccessLayer.Instance.ExecuteQuery(sql);
}
catch (Exception ex)
{
throw ex;
}
}
}
我正在尝试使用依赖性 injection.May 因为我遗漏了一些东西。谁可以帮我这个事 ?我坚持这个。
你需要用构造函数传递依赖
var home = new HomePage(myDependencyInterface);
如果您没有在登录页面中实例化该依赖项,那么您必须先使用 Unity 容器解决它:
var myDependencyInterface = container.Resolve<myDependencyInterface>();
另外,正如您在问题的评论中所问的那样,您在使用 Prism 吗?
因为如果是这样你只需要使用 NavigateAsync(nameof(HomePage));
并且 Unity 将注入依赖项