I am trying to show main window after successful login.I used interface as parameter to constructor in main window. Now when I try showing main window, I get an error because I cannot pass interface as parameter to main window.I saw many posts like mine but I thought it is quite different from them.
This is my main window constructor:
public Home_Page(IGetAllItemClass clas)
{
InitializeComponent();
_allClass = clas;
}
IGetAllItemClass _allClass;
My code in login Window from where I need to show main form:
Home_Page h = new Home_Page();
h.ShowDialog();
My 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>
This is my 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();
}
}
This is my interface:
namespace Cafe_WPF.Interface
{
public interface IGetAllItemClass
{
DataTable item_class(string business_info_id, string rvc_id);
}
}
And my class service implementing interface is:
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;
}
}
}
I am trying to use dependency injection.May be I am missing something. Can anyone help me on this ? I am stuck on this.
You need to pass the dependency with the constructor
var home = new HomePage(myDependencyInterface);
If you don't have that dependency instantiated within your login page then you have to resolve it with your Unity container first:
var myDependencyInterface = container.Resolve<myDependencyInterface>();
Also as asked in the comments of your question, are you using Prism?
Because if so you just need to use NavigateAsync(nameof(HomePage));
and Unity will inject the dependency