Issue
I have 1 question here: How to get data from ViewModel in Xamarin, however I still haven't solved the problem. I created a new post with some changes.
I have:
PageOne.xaml
<StackLayout> <RefreshView x:DataType="locals:ViewCustomerViewModel" Command="{Binding LoadUserinfoCommand}" IsRefreshing="{Binding IsBusy, Mode=OneWay}"> <Label Text="{Binding Customer.Address}" /> </RefreshView> </StackLayout>
PageOne.xaml.cs
ViewCustomerViewModel viewCustomerViewModel; public Customer CustomerGet { get; set; } public PageOne() { InitializeComponent(); BindingContext = viewCustomerViewModel = new ViewCustomerViewModel(); viewCustomerViewModel.OnAppearing(); }
Class Customer
public class Customer { public string Address{ get; set; } ........ }
ViewCustomerViewModel
public class ViewCustomerViewModel:BaseCustomerViewModel { ApiServiceUserinfo apiServiceUserinfo = new ApiServiceUserinfo(); public Command LoadUserinfoCommand { get; } public ObservableCollection<Customer> CustomerInfos { get; set; } public ViewCustomerViewModel() { LoadUserinfoCommand = new Command(async () => await ExecuteLoadUserinfoCommand()); CustomerInfos = new ObservableCollection<Customer>(); } public void OnAppearing() { IsBusy = true; } async Task ExecuteLoadUserinfoCommand() { string userget = "1"; IsBusy = true; try { CustomerInfos.Clear(); var customerList = await apiServiceUserinfo.GetCustomersInfo(userget); CustomerInfos.Add(customerList); } catch (Exception) { throw; } finally { IsBusy = false; } } }
And I got the result CustomerInfos.Add(customerList);
However <Label Text="{Binding Customer.Address}" />
does not get results
Please help me again clearly in the answer. Thank you.
Update
ViewCustomerViewModel
public class ViewCustomerViewModel:BaseCustomerViewModel { ApiServiceUserinfo apiServiceUserinfo = new ApiServiceUserinfo(); public Command LoadUserinfoCommand { get; set;} public Customer CustomerGets { get; set;}--> update public ViewCustomerViewModel() { LoadUserinfoCommand = new Command(async () => await ExecuteLoadUserinfoCommand()); //CustomerGets = new Customer(); } public void OnAppearing() { IsBusy = true; } async Task ExecuteLoadUserinfoCommand() { string userget = "1"; IsBusy = true; try { var customerList = await apiServiceUserinfo.GetCustomersInfo(userget); CustomerGets = customerList; } catch (Exception) { throw; } finally { IsBusy = false; } } }
PageOne.xaml
<StackLayout> <RefreshView x:DataType="locals:ViewCustomerViewModel" Command="{Binding LoadUserinfoCommand}" IsRefreshing="{Binding IsBusy, Mode=OneWay}"> <Label Text="{Binding CustomerGets.Address}" /> </RefreshView> </StackLayout>
Solution
We need to call the OnPropertyChanged
method to notify the change in setter
method of the property .
private Customer customerGets;
public Customer CustomerGets {
get { return customerGets; }
set {
customerGets = value;
NotifyPropertyChanged(); //the method is declared in BaseCustomerViewModel
}
}
Ensure that BaseCustomerViewModel
has implemented INotifyPropertyChanged
, something like that
public class BaseCustomerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Answered By - ColeX - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.