Comments (6)
A more detailed sample:
dotnet new mauiapp -o NavApp -dp Hierarchical -mvvm
from dotnet-maui-templates.
I don't understand your request. Please elaborate.
from dotnet-maui-templates.
Here is an example of using dependency injection in a MAUI app:
First, in the startup class of the MAUI app, we need to create and configure a DI container. We can use the ServiceCollection class provided by the Microsoft.Extensions.DependencyInjection NuGet package:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
namespace MyMauiApp
{
public class Startup : IStartup
{
public void Configure(IAppHostBuilder appBuilder)
{
appBuilder
.UseMauiApp<MyApp>()
.ConfigureServices(services =>
{
// Register your services here
services.AddTransient<IMyService, MyService>();
});
}
}
}
In the above example, we register the IMyService interface as an instance of the MyService class, so we can use IMyService to retrieve an instance of MyService throughout the application.
Next, we can use the service in other parts of the application. For example, we can inject the IMyService service into the page constructor, as shown below:
public partial class MyPage : ContentPage
{
private readonly IMyService _myService;
public MyPage(IMyService myService)
{
InitializeComponent();
_myService = myService;
}
}
Now, in the code of the MyPage page, we can use the _myService instance to call methods defined in the IMyService interface.
This is just a simple example, but it demonstrates how to use dependency injection in a MAUI app. By using dependency injection, we can decouple different parts of the application and write more easily testable and maintainable code.
from dotnet-maui-templates.
You can do that right now using the MauiProgram.cs source file.
Register your dependencies like:
builder.Services.AddTransient<IMyService, MyService>();
// Register the Page with the appropriate scope
builder.Services.AddTransient<MyPage>();
Then inject the dependency into the page as described in your code snippet.
The only catch is that your page has to be resolved from the DI container so that its dependencies are auto-resolved (IMyService is a dependency for MyPage).
For example:
public partial class App : Application
{
public App(MyPage myPage)
{
MainPage = myPage;
}
}
If you prefer the ConfigureServices approach, it's also available in my MAUI Toolkit here, add the NuGet package as a reference, and then include it in the .NET MAUI startup pipeline.
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>()
.ConfigureServices(services =>
{
// Do all the stuff with the "services" parameter
});
return builder.Build();
from dotnet-maui-templates.
To get a glimpse of how it works, create a .NET MAUI project from the All-in-One template with the MVVM option.
dotnet new mauiapp -o MvvmApp -mvvm
The ViewModel is injected as a dependency and resolved from the DI container.
from dotnet-maui-templates.
Since this is covered in the de facto implementation of .NET MAUI, closing this issue.
from dotnet-maui-templates.
Related Issues (20)
- Build fails for .NET MAUI class library with .NET MAUI Essentials targeting the Windows platform HOT 2
- can't create project in vs2022-preview version 17.0.0prview 2.1 HOT 4
- The partial class template allows a class that is both static and abstract. HOT 1
- [3.2.0-preview.1] Project node is not closed properly in the soln file while creating a Blazor Hybrid with RCL option from CLI HOT 1
- Consider adding a parameter named blank to create a new .NET MAUI App from the ground up in the chosen design pattern
- _Imports.razor file getting excluded while creating a project in the Razor design pattern HOT 1
- Can we get template for MAUI Class library HOT 5
- Generic Item Templates for Visual Studio HOT 1
- Add support for .NET MAUI on .NET 9 Previews HOT 1
- Incorrect type name generation in the ContentPage with BlazorWebView item templates HOT 1
- Support for adding .NET MAUI Community Toolkit namespace while defining base types with that particular `xmlns` scope (mct) HOT 2
- Shell Page Support for PR8 HOT 2
- Support for Maui preview 13 HOT 4
- Gesture issue in Tab template HOT 1
- Compile error with RC1 HOT 2
- Add option to include Microsoft.Toolkit.Mvvm HOT 4
- Installation on VS 2022 failed HOT 3
- Support for .NET MAUI Templates on .NET 7 HOT 3
- How can I use Hybrid in Shell Design Pattern? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dotnet-maui-templates.