question

JesseKnott-2239 avatar image
0 Votes"
JesseKnott-2239 asked ·

Can I overload a template async function without it executing when associated?

I have a series of functions that I use as a base for my ViewModels. These get overloaded in each class that uses the baseviewmodel.cs.

Here's an example.

BaseViewModel.cs

     ....

     public Task InitBase { get; internal set; }
     public Task Initialization { get; internal set; }
     public Task InitInventory { get; internal set; }

     ....

InventoryPageViewModel.cs

 public InventoryPageViewModel : BaseViewModel
    
 public InventoryPageViewModel()
 {
   InitBase  = CustomInitializationFunc(); //This code executes the CustomInitializationFunc
    
 }
    
 public async Task CustomInitializationFunc()
 {
   ... some initialization class ...
 }

The problem I have is this. When the InitBase is assigned, it is running the code in the function being assigned. The function has code that can only be executed after the page has been loaded, I can add code to prevent the function from running unless a flag is set for the page being ready. However It would be easier to be able to run the initialization without having to execute the function as it's being assigned. Later in the code I will use await InitBase; to execute the function, but

I hope this makes sense.

Cheers!





dotnet-xamarinforms-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered ·

Hello,


Welcome to our Microsoft Q&A platform!


The problem I have is this. When the InitBase is assigned, it is running the code in the function being assigned. The function has code that can only be executed after the page has been loaded, I can add code to prevent the function from running unless a flag is set for the page being ready.

When you set the BindingContext for your page,once the page been created, the constructor of your ViewModel will been called at the same time.

If you want some code in the constructor of your ViewModel willn't been executed , you can set some condition(e.g. a bool flag) just as you mentioned.

In addition, you can also add your code in function OnAppearing by override function OnAppearing:

     protected override void OnAppearing()
     {
         base.OnAppearing();
         // add your code here
     }


Best Regards,


Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.



Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Cool, that's more or less what I had expected.
I've gone through and set bool and condition checks to prevent exceptions from member properties not being initialized.

In the end, using the bool flags to check if the initialization is complete is the best way to handle it. I figure it makes sense that the function is run as it's assigned since the compiler has to follow the trace of code when assigning functions to pointer functions.

Thanks for the help!

Cheers!

0 Votes 0 ·