Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4008174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:43:18+00:00 2026-05-20T08:43:18+00:00

I have Unity 2.0 working well within the App.xaml.cs to register and resolve within

  • 0

I have Unity 2.0 working well within the App.xaml.cs to register and resolve within that class.

The question I have is regarding a best practice.

I have a number of User Controls and other classes that also need to resolve some of the same and new Interface <-> implementations. The problem is theres no way to access the Unity container I created in the App.xaml.cs.

I cannot use constructor or property injection to pass on the container reference.

  1. Just too many (its a large project)
  2. The user controls are added via xaml
  3. There are several very loosely
    related “modules” in the project
    that can share the same container
    config.

I would rather not re-create the container from a config file in each object that needs access to the container.

Any best practice suggestions when the same container is needed as a service in various “modules” of the same assembly?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:43:19+00:00Added an answer on May 20, 2026 at 8:43 am

    I believe bringing together Controls and IoC is pain in the … in the code at least. Probably somebody will argue but IMO the best practice to avoid this pain is MVVM. You will have viewModels which you can freely construct using Unity and inject everything you need into them. You will have views with bindings to viewModels with no reason to know anything abound inversion of control.

    UPDATE: Based on comment:

    App.xaml.cs:

        private void HandleStartup(object sender, StartupEventArgs e)
        {
            var container = CreateContainer(); // create IoC container
           var mainViewModel = container.Resolve<MainViewModel>();
            var shell = new Shell { DataContext = mainViewModel }; // main View
            MainWindow = shell;
            shell.Show();
        }
    

    Shell XAML example:

    <UserControl>
         <StackPanel>
              <ContentPresenter Content="{Binding ViewModel1}" />
              <ContentPresenter Content="{Binding ViewModel2}" />
              <ContentPresenter Content="{Binding ViewModel3}" />
         </StackPanel>
    </UserControl>
    

    MainViewModel:

    public class MainViewModel
    {
         public ViewModel1 ViewModel1 { get; private set; }
         public ViewModel2 ViewModel2 { get; private set; }
         public ViewModel3 ViewModel3 { get; private set; }
    
         // this will be handled by IoC container
         public MainViewModel(ViewModel1 viewModel1, ViewModel2 viewModel2, ViewModel3 viewModel3)
        {
            ViewModel1 = viewModel1;
            ViewModel2 = viewModel2;
            ViewModel3 = viewModel3;
        }
    

    In this way your views will be unaware of IoC and everything you want in viewModels will be successfully injected.

    UPDATE2 DataTemplating which brings Views and ViewModels together:

    App.xaml

    <Application.Resources>
        <DataTemplate DataType="{x:Type local:ViewModel1}">
            <View1 />
        </DataTemplate>
    </Application.Resources>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.