如何在 Xamarin 中绑定来自 ViewModel 的结果
How to Binding Result from ViewModel in Xamarin
我这里有 1 个问题:How to get data from ViewModel in Xamarin,但我仍然没有解决问题。我创建了一个新的 post 并进行了一些更改。
我有:
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 客户
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;
}
}
}
我得到了结果CustomerInfos.Add(customerList);
但是<Label Text="{Binding Customer.Address}" />
没有得到结果
请再帮我解答清楚。谢谢。
更新
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>
我们需要调用OnPropertyChanged
方法来通知setter
方法中属性的变化。
private Customer customerGets;
public Customer CustomerGets {
get { return customerGets; }
set {
customerGets = value;
NotifyPropertyChanged(); //the method is declared in BaseCustomerViewModel
}
}
确保 BaseCustomerViewModel
已经实现 INotifyPropertyChanged
,类似的东西
public class BaseCustomerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我这里有 1 个问题:How to get data from ViewModel in Xamarin,但我仍然没有解决问题。我创建了一个新的 post 并进行了一些更改。
我有:
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 客户
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; } } }
我得到了结果CustomerInfos.Add(customerList);
但是<Label Text="{Binding Customer.Address}" />
没有得到结果
请再帮我解答清楚。谢谢。
更新
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>
我们需要调用OnPropertyChanged
方法来通知setter
方法中属性的变化。
private Customer customerGets;
public Customer CustomerGets {
get { return customerGets; }
set {
customerGets = value;
NotifyPropertyChanged(); //the method is declared in BaseCustomerViewModel
}
}
确保 BaseCustomerViewModel
已经实现 INotifyPropertyChanged
,类似的东西
public class BaseCustomerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}