Closed vsfeedback closed 1 week ago
Reattaching the image.
Use Dependency Injection:
public partial class YourPage : ContentPage
{
public YourPage(YourViewModel model)
{
InitializeComponent();
BindingContext = model;
}
// an example how to retrieve view model from BindingContext
protected override OnAppearing()
{
base.OnAppearing();
if(BindingContext is YourViewModel model)
{
model.Whatever();
}
}
}
builder.Services
.AddTransient<YourViewModel>()
.AddTransient<YourPage>();
Hi @vsfeedback. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Hi @vsfeedback. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md
This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Hi @vsfeedback. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
We can't infer when/how you set the BindingContext (are you using a 3rd party component ?). please provide a small repro project for this
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
This issue has been automatically marked as stale because it has been marked as requiring author feedback to reproduce the issue but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
This issue has been moved from a ticket on Developer Community.
Hi, we are migrating from xamarin to MAUI.
Using MVVM pattern since the xamarin version we met this code ( red arrow)
To pass the bindingcontext and process our logic...
the big problem is that the variable paginaInizialeViewModel is null when the bindingcontext instruction is processed... and so, calling the viewmodel methods goes in error....
Do MAUI changed this approach?
thank you very much Angelo
Original Comments
Elijah Hilliker on 9/17/2024, 08:42 AM:
It would be best practice to change your app over to used dependency injection. Hopefully this article helps
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/
Feedback Bot on 9/17/2024, 06:52 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.