注入WPF类的接口 [英] Injecting interfaces to class WPF

查看:108
本文介绍了注入WPF类的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

    <pre>public Home_Page(IGetAllItemClass clas)
            {
                InitializeComponent();
                _allClass = clas;
            }
            IGetAllItemClass _allClass;





登录窗口中的我的代码我需要显示主窗体:< br $>




My code in login Window from where I need to show main form:

Home_Page h = new Home_Page();
     h.ShowDialog();



我的app.xaml:




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>





这是我的App.cs:







This is my App.cs :


<pre>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.





What I have tried:

Above codes are what I tried.May be I am taking architecture the wrong way

推荐答案

Quote:

Home_Page h = new Home_Page();



您无法调用它,因为 Home_Page 类没有无参数构造函数。您需要传入 IGetAllItemClass 界面的实例。



最简单的解决方案是导入 Home_Page 实例作为 Login_Window 类的构造函数的参数:


You can't call that, because the Home_Page class doesn't have a parameterless constructor. You need to pass in an instance of the IGetAllItemClass interface.

The simplest solution is to import the Home_Page instance as a parameter to the constructor of your Login_Window class:

private readonly Home_Page _homePage;

public Login_Window(..., Home_Page homePage)
{
    ...
    _homePage = homePage;
}

public void ShowHomePage()
{
    _homePage.ShowDialog();
}



这样,您使用Unity容器创建的实例,而不是尝试手动创建新实例。


That way, you're using the instance created by the Unity container, rather than trying to create a new instance manually.


这篇关于注入WPF类的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆