C
C#14mo ago
kurumi

✅ async .NET MAUI binding

Hello, on Xamarin channel I found this code how to update UI with threads and async stuff. https://youtu.be/-LY4ATA8Bgw?si=cRmPMFNjL9-zTxui&t=1212 But in .NET MAUI it says Warning CS0618 'Device.BeginInvokeOnMainThread(Action)' is obsolete: 'Use BindableObject.Dispatcher.Dispatch() instead'. So how can I bind some async data from my API by using it? Leave an example please 😄
using CommunityToolkit.Mvvm.ComponentModel;
using MRSUMobile.MVVM.Model;
using MRSUMobile.Services;

namespace MRSUMobile.MVVM.ViewModel
{
public partial class AppShellViewModel : ObservableObject
{
IMrsuApiService mrsuApi;

[ObservableProperty]
User user = new User();

public AppShellViewModel(IMrsuApiService mrsuApiService)
{
mrsuApi = mrsuApiService;

// async binding of user ?
// user = await mrsuApi.GetMyProfile();
}
}
}
using CommunityToolkit.Mvvm.ComponentModel;
using MRSUMobile.MVVM.Model;
using MRSUMobile.Services;

namespace MRSUMobile.MVVM.ViewModel
{
public partial class AppShellViewModel : ObservableObject
{
IMrsuApiService mrsuApi;

[ObservableProperty]
User user = new User();

public AppShellViewModel(IMrsuApiService mrsuApiService)
{
mrsuApi = mrsuApiService;

// async binding of user ?
// user = await mrsuApi.GetMyProfile();
}
}
}
1 Reply
kurumi
kurumiOP14mo ago
alright, after some expreriments I found this solution
public AppShellViewModel(IMrsuApiService mrsuApiService)
{
mrsuApi = mrsuApiService;

Application.Current.Dispatcher.DispatchAsync(async () =>
{
user = await mrsuApi.GetMyProfile();
});
}
public AppShellViewModel(IMrsuApiService mrsuApiService)
{
mrsuApi = mrsuApiService;

Application.Current.Dispatcher.DispatchAsync(async () =>
{
user = await mrsuApi.GetMyProfile();
});
}
But probably this is not a good way hm... This code only sets property but UI is still not updated The problem was with user = await mrsuApi.GetMyProfile(); it should be User = ...
Want results from more Discord servers?
Add your server