dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

BindingContext is returning null #25210

Closed vsfeedback closed 1 week ago

vsfeedback commented 3 weeks ago

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)

mvvm.jpg

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.

suugbut commented 3 weeks ago

Reattaching the image.

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>();
dotnet-policy-service[bot] commented 3 weeks ago

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.

dotnet-policy-service[bot] commented 3 weeks ago

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.

dotnet-policy-service[bot] commented 3 weeks ago

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.

StephaneDelcroix commented 3 weeks ago

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

dotnet-policy-service[bot] commented 2 weeks ago

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.

dotnet-policy-service[bot] commented 2 weeks ago

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.