WPF 使用依赖注入和 IoC。

huangapple go评论82阅读模式
英文:

WPF with Dependency Injection using Ioc

问题

I am currently learning how to code in WPF desktop application, and I am following this show a super old tutorial on youtube on the subject:

I have trouble using Dependency injection with multiple projects using the Ninject framework. I created a public static class called IocContainer that binds all of the required view models based on a class named application view models and gets the service of the IOC of the specific target. Snipped of the overall class is shown.

/// <summary>
/// Sets up the Container, binds all information required and is ready for use
/// NOTE: must be called as soon as your application starts up to ensure all services can be called.
/// </summary>
public static void Setup()
{
    // Binds all required view models
    BindViewModels();
}

/// <summary>
/// Binds all singleton view models
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private static void BindViewModels()
{
    // Binds to a single instance of ApplicationViewModel
    Kernel.Bind<ApplicationViewModel>().ToConstant(new ApplicationViewModel());
}

#endregion

/// <summary>
/// Get a service of the IOC of the specific target 
/// </summary>
/// <typeparam name="T">Type to get</typeparam>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static object Get<T>()
{
    return Kernel.Get<T>();
}

The IocContainer gets called on an override OnStartup function of the WPF to set up the IOC, and the application class has a Public enum variable named current age. However, when I attempt to change to a different page when running a task, it says that ApplicationViewModel the object does not contain the definition even though it does exist. ApplicationViewModel is located in a different project named Project.Core. And I attempt to change the current page value by doing the following:

// Located in the Application view model
public ApplicationPage CurrentPage { get; set; }
// attempt to change the current page inside my page view model
IocContainer.Get<ApplicationViewModel>().CurrentPage = ApplicationPage.Connection;

Error code: Severity Code Description Project File Line Suppression State Error CS1061 'object' does not contain a definition for 'CurrentPage' and no accessible extension method 'CurrentPage' accepting a first argument of type 'object' could be found

Any ideas or suggestions?
Thanks,
Jorge Jurado-Garcia

英文:

I am currently learning how to code in WPF desktop application, and I am following this show a super old tutorial on youtube on the subject: https://www.youtube.com/watch?v=w5kAUCFDRy4&amp;list=PLrW43fNmjaQVYF4zgsD0oL9Iv6u23PI6M&amp;index=11

I have trouble using Dependency injection with multiple projects using the Ninject framework. I created a public static class called IocContainer that binds all of the required view models based on a class named application view models and gets the service of the IOC of the specific target. Snipped of the overall class is shown.

    /// &lt;summary&gt;
    /// Sets up the Container, binds all information required and is ready for use
    /// NOTE: must be called as soon as your application starts up to ensure all services can
    /// be called.
    /// &lt;/summary&gt;
    public static void Setup()
    {
        //Binds all required view models
        BindViewModels();
    }

    /// &lt;summary&gt;
    /// Binds all signeleton view models
    /// &lt;/summary&gt;
    /// &lt;exception cref=&quot;NotImplementedException&quot;&gt;&lt;/exception&gt;
    private static void BindViewModels()
    {
        //Binds to a signle instance of Application View Model
        Kernel.Bind&lt;ApplicationViewModel&gt;().ToConstant(new ApplicationViewModel());
    }

    #endregion

    /// &lt;summary&gt;
    /// Get a serice of the IOC of the specific target 
    /// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;T&quot;&gt; ttype to get&lt;/typeparam&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    /// &lt;exception cref=&quot;NotImplementedException&quot;&gt;&lt;/exception&gt;
    public static object Get&lt;T&gt;()
    {
        return Kernel.Get&lt;T&gt;();
    }

The IocContainer gets called on a overrides Onstartup function of the WPF to set up the IOC, and the application class has a Public enum variable named current age. However, when I attempt to change to a different page when running a task, it says that ApplicationViewModel
the object does not contain the definition even though it does exist. ApplicationViewModel is located in a different project named Project.Core. And I attempt to change the current page value by doing the following:

//Located in the Application view model
public ApplicationPage CurrentPage { get; set; }
//attempt to change the current page inside my page view model
IocContainer.Get&lt;ApplicationViewModel&gt;().CurrentPage = ApplicationPage.Connection;

Error code: Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'CurrentPage' and no accessible extension method 'CurrentPage' accepting a first argument of type 'object' could be found

Any ideas or suggestions?
Thanks,
Jorge Jurado-Garcia

答案1

得分: 0

您的 服务定位器 类的 Get&lt;T&gt;() 方法返回了一个 object 类型而不是请求的类型。

它应该返回 T

public static T Get&lt;T&gt;()
{
    return Kernel.Get&lt;T&gt;();
}
英文:

Your service locator class Get&lt;T&gt;() method returns an object type and not the requested type.

It should return T.

public static T Get&lt;T&gt;()
{
    return Kernel.Get&lt;T&gt;();
}

huangapple
  • 本文由 发表于 2023年7月7日 03:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定