- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 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>
关于xaml - 如何在Xamarin中调用API在UI上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51079625/
我想创建一个包含一堆其他 XAML 文件的主 XAML 文件。这可能吗? 最佳答案 在您的主 Xaml 中,使用 项目控件 balise 和 Xaml 组合器,您必须使用 定义您的项目用户控件 代替
如何在 xaml 中更改 ToggleSwitch 中实际开关的大小?我可以更改 OnContent 和 OffContent 的文本大小,但滑动开关永远不会更改大小。 这些都不会改变开关本身的大小,
我有一个文本 block 列表,其中可能包括里面的 url,比如: 构建失败,请在此处查看更多信息:http://... 构建成功 应用 http://myapp/无法启动,请在此处查看更多信息:ht
我想做的事 我最近一直在探索 XAML 资源字典。它们非常强大,但为了减少(甚至进一步)为适应任何修改而需要进行的更改,我想使用一些基本的算术运算来更改 HeightRequest Entry 的属性
我使用以下代码创建了一个弹出窗口,但我不知道如何将它居中 我试图在运行时自动更改边距,但我不知道该怎么做,但是有人知道如何将弹出窗口居中吗? 它没有标准维度,因为我需要全局化我的程序
哪种类型的命名更适合在 XAML 中使用: xmlns:inventoryControls="clr-namespace:Inventory.Components.Controls;assembly=
我正在尝试格式化我的 string每 3 位有逗号,如果不是整数,则为小数。我检查了大约 20 个示例,这是我最接近的示例: 但我得到一个 The property 'StringFormat' w
在 Windows Phone 8 中,我想将一个图像放在一个圆圈中。是否有类似网格的圆形容器?我知道有椭圆位它不是容器 最佳答案 这是我如何做到的。
有没有办法在 XAML 中检查逻辑非逻辑。我试图绑定(bind)到广泛使用的属性,但是对于这些新的绑定(bind),它的方式是错误的。我不想修改属性,那么有没有办法在 xaml 中反转 bool 结果
在 html 中可以说 width="20%"。这在 XAML 中当然是不允许的。有谁知道为什么会这样或者有没有办法在 XAML 中获得百分比值支持? 最佳答案 网格列定义和行定义允许比例单位(除了固
我可以动态创建 XAML 并将其弹出到我的应用程序中吗?该怎么做呢? 最佳答案 是的,这很简单: XmlReader tXml = XmlReader.Create(myXamlString); UI
简短版: 如果不使用 xamlc 预编译 xaml,应用程序中捆绑了什么以在运行时编译 xaml?它是否取决于 xamarin.forms nugets 之外的应用构建时的当前工具集? 长版: 我们有
我知道如何显示内容页面,但如何根据方向选择正确的布局。我想编写 xaml 文件:一个用于垂直方向,另一个用于水平方向。方向改变时是否可以切换xaml文件 最佳答案 Xamarin.Forms does
我是一名 Android 开发者,我是 Windows Phone 8 的新手 最近我做了一个 android 应用程序..所以我想在 Windows Phone 8 中开发具有相同 View 和相同
在库中编译 XAML 时出现以下错误: Themes\Generic.xaml(35,12): XamlCompiler error WMC0610: XBF generation error cod
我需要在几个不同的 XAML 布局文件中包含一个通用的 XAML。 我不想在其他 XAML 文件中对重复的 XAML 部分进行硬编码。 怎么做包含? 最佳答案 你能把共享代码变成UserControl
我想像这样添加一个符号: 这行不通,所以我尝试用\分隔,但也行不通。有人对我如何做到这一点有任何建议吗? 最佳答案 其实这个问题不是关于XAML的,它只是一个纯XML的问题。 XML中有些字符需要转
我在 Xaml 中定义了这样的列表。 #3599B8 #374649 #FD625E #F2C80F
在向 Microsoft 报告之前,我想在这里问一下。我有一个问题,我无法看到我的 WinUI 3 应用程序的实时可视化树。我什至看不到应用程序内工具栏。我可以在 WPF 和 UWP 应用程序中看到,
我已经定义了一个动画作为一个资源,当通过这样的 EventTrigger 调用时它可以工作: 然后
我是一名优秀的程序员,十分优秀!