gpt4 book ai didi

xaml - 如何在Xamarin中调用API在UI上显示数据

转载 作者:行者123 更新时间:2023-12-02 07:50:10 25 4
gpt4 key购买 nike

我对 Xamarin 有点陌生,正在开发 iOS 应用程序,我需要调用 API 并绑定(bind)响应数据以查看已使用的 MVVM 模式。

这是我的ViewModel代码:

public class PersonalDetailModel : BaseViewModel
{
private PersonalDetails personalDetails { get; set; }
public Command LoadCommand;

public PersonalDetailModel()
{
LoadCommand = new Command(async () => await GetPersonalDetais());

}
public String City
{
get
{
return personalDetails.city;
}
set
{
personalDetails.city = value;
}
}
public string Phone
{
get { return personalDetails.phone; }
set
{
personalDetails.phone = value;

}
}
public string Email
{
get { return personalDetails.email; }
set
{
personalDetails.email = value;

}
}

public async Task<PersonalDetails> GetPersonalDetais()
{
var ApiRequest = RestService.For<IUserService>(Constants.BaseUrl);

PersonalDetailsResponse ApiResponse = await ApiRequest.showProfile(Constants.token);

PersonalDetailsResponse response = ApiResponse;

this.personalDetails = response.result;
personalDetails = new PersonalDetails
{
city = personalDetails.city,
phone = personalDetails.phone,
email = personalDetails.email
};

return this.personalDetails;
}


}

GetPersonalDetais() 中,我在调试过程中从 API 获取值,但无法在 UI 中绑定(bind)该值。

XML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BarberCustomer.Views.PersonalDetails"
>
<ContentPage.Content>
<StackLayout Orientation="Vertical" Padding="4,5" BackgroundColor="#efeff4" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Frame HasShadow="True" Padding="8,5,8,12" CornerRadius="2" Margin="1,1,0,0" HorizontalOptions="FillAndExpand"
VerticalOptions="Start">
<StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Label Text="Personal Details" HorizontalOptions="Start" VerticalOptions="FillAndExpand" LineBreakMode="WordWrap" FontSize="12" TextColor="#ad0e0e">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>SFProDisplay-Bold.ttf#SF-Pro-Display-Bold</OnPlatform.Android>
<OnPlatform.iOS>SFProDisplay-Bold</OnPlatform.iOS>
</OnPlatform>
</Label.FontFamily>
</Label>
<StackLayout Margin="0,1,0,1" HorizontalOptions="FillAndExpand" HeightRequest="1" BackgroundColor="#efeff4" VerticalOptions="EndAndExpand" Padding="0">
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="Center">
<Image Source="location.png" WidthRequest="8" HeightRequest="11" VerticalOptions="Center" Aspect="AspectFit" ></Image>
<Label Text="{Binding City}" Margin="4,0,0,0"
HorizontalOptions="Start" VerticalOptions="FillAndExpand" LineBreakMode="WordWrap" FontSize="9" TextColor="#253045">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>SFProDisplay-Medium.ttf#SFProDisplay-Medium</OnPlatform.Android>
<OnPlatform.iOS>SFProDisplay-Medium</OnPlatform.iOS>
</OnPlatform>
</Label.FontFamily>
</Label>

</StackLayout>

<StackLayout HorizontalOptions="FillAndExpand" HeightRequest="1" BackgroundColor="#eeeeee" VerticalOptions="EndAndExpand" Padding="0" Margin="17,1,0,1">

</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="Center">
<Image Source="phone.png" WidthRequest="8" HeightRequest="11" VerticalOptions="Center" Aspect="AspectFit" ></Image>
<Label Text="{Binding Phone}" Margin="4,0,0,0"
HorizontalOptions="Start" VerticalOptions="FillAndExpand" LineBreakMode="WordWrap" FontSize="9" TextColor="#253045">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>SFProDisplay-Medium.ttf#SFProDisplay-Medium</OnPlatform.Android>
<OnPlatform.iOS>SFProDisplay-Medium</OnPlatform.iOS>
</OnPlatform>
</Label.FontFamily>
</Label>
</StackLayout>
<StackLayout HorizontalOptions="FillAndExpand" HeightRequest="1" BackgroundColor="#eeeeee" VerticalOptions="EndAndExpand" Padding="0" Margin="17,1,0,1">

</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="Center">
<Image Source="mail.png" WidthRequest="11" HeightRequest="12" VerticalOptions="Center" Aspect="AspectFit" ></Image>
<Label Text="{Binding Email}" Margin="4,0,0,0"
HorizontalOptions="Start" VerticalOptions="FillAndExpand" LineBreakMode="WordWrap" FontSize="9" TextColor="#253045">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>SFProDisplay-Medium.ttf#SFProDisplay-Medium</OnPlatform.Android>
<OnPlatform.iOS>SFProDisplay-Medium</OnPlatform.iOS>
</OnPlatform>
</Label.FontFamily>
</Label>
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>
</ContentPage.Content>
</ContentPage>

xml.cs:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PersonalDetails : ContentPage
{
PersonalDetailModel viewModel;

public PersonalDetails()
{
InitializeComponent();

viewModel = new PersonalDetailModel();

BindingContext = viewModel;



}

protected async override void OnAppearing()
{
base.OnAppearing();
viewModel.LoadCommand.Execute(null);

}
}

我们将不胜感激每一个回复和建议!

最佳答案

您应该将 BindingContext 放入 xaml.cs 或 xaml 中,以将 ViewModel 与 View 绑定(bind)。

有多种方法可以将数据绑定(bind)到 View 。

ViewModel.cs

public class PettyCashListViewModel : BaseNavigationViewModel

Page.Xaml.cs

public partial class PettyCashListPage : ContentPage
{
protected PettyCashListViewModel ViewModel => BindingContext as PettyCashListViewModel;

或在页面构造函数内

this.BindingContext = ViewModel;

或在您的 page.xaml 内

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:XamarinPOC.ViewModel; assembly=XamarinPOC.ViewModel"
x:Class="XamarinPOC.Summary"
Title="Summary List">
<ContentPage.BindingContext>
<viewModels:SummaryViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="{Binding test}"/>
</StackLayout>
</ContentPage>

引用:Set BindingContext to ViewModel in XAML on Xamarin.Forms

关于xaml - 如何在Xamarin中调用API在UI上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51079625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com