用C#[Xamarin表单]创建复杂布局
在我的应用程序中,我需要一个页面来保存用户的个人资料,但我一直在努力用xamarin表单创建复杂的UI 我在xamarin文档中看到了这一点,但我不确定如何使用XAML,而且他们也没有任何关于如何在c#中获得相同外观的示例。任何帮助都将不胜感激 文档页面: XAML代码:用C#[Xamarin表单]创建复杂布局,c#,xml,user-interface,xamarin.forms,C#,Xml,User Interface,Xamarin.forms,在我的应用程序中,我需要一个页面来保存用户的个人资料,但我一直在努力用xamarin表单创建复杂的UI 我在xamarin文档中看到了这一点,但我不确定如何使用XAML,而且他们也没有任何关于如何在c#中获得相同外观的示例。任何帮助都将不胜感激 文档页面: XAML代码: <?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:
<?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="TheBusinessTumble.StackLayoutPage"
BackgroundColor="Maroon"
Title="StackLayouts">
<ContentPage.Content>
<ScrollView>
<StackLayout Spacing="0" Padding="0" BackgroundColor="Maroon">
<BoxView HorizontalOptions="FillAndExpand" HeightRequest="100" VerticalOptions="Start" Color="Gray" />
<Button BorderRadius="30" HeightRequest="60" WidthRequest="60" BackgroundColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
<StackLayout HeightRequest="100" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Spacing="20" BackgroundColor="Maroon">
<Label Text="User Name" FontSize="28" HorizontalOptions="Center" VerticalOptions="Center" FontAttributes="Bold" />
<Entry Text="Bio + Hashtags" TextColor="White"
BackgroundColor="Maroon" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" />
</StackLayout>
<StackLayout Orientation="Horizontal" HeightRequest="50" BackgroundColor="White" Padding="5">
<StackLayout Spacing="0" BackgroundColor="White" Orientation="Horizontal" HorizontalOptions="Start">
<BoxView BackgroundColor="Black" WidthRequest="40" HeightRequest="40" HorizontalOptions="StartAndExpand" VerticalOptions="Center" />
<Label FontSize="14" TextColor="Black" Text="Accent Color" HorizontalOptions="StartAndExpand" VerticalOptions="Center" />
</StackLayout>
<StackLayout Spacing="0" BackgroundColor="White" Orientation="Horizontal" HorizontalOptions="EndAndExpand">
<BoxView BackgroundColor="Maroon" WidthRequest="40" HeightRequest="40" HorizontalOptions="Start" VerticalOptions="Center" />
<Label FontSize="14" TextColor="Black" Text="Primary Color" HorizontalOptions="StartAndExpand" VerticalOptions="Center" />
</StackLayout>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label FontSize="14" Text="Age:" TextColor="White" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="100" />
<Entry HorizontalOptions="FillAndExpand" Text="35" TextColor="White" BackgroundColor="Maroon" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label FontSize="14" Text="Interests:" TextColor="White"
HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="100" />
<Entry HorizontalOptions="FillAndExpand" Text="Xamarin.Forms" TextColor="White" BackgroundColor="Maroon" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label FontSize="14" Text="Ask me about:" TextColor="White" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="100"/>
<Entry HorizontalOptions="FillAndExpand" Text="Xamarin, C#, .NET, Mono..." TextColor="White" BackgroundColor="Maroon" />
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
您可以使用XAML编译。
您只需向类应用一个属性,就可以获得使用C#(即性能)的相同好处,但您将能够轻松地在声明中定义元素(即使用XAML)。如果您刚开始使用Xamarin表单,学习XAML并使用它来描述页面而不是使用代码是非常值得的 您所采用的方法将导致代码难以阅读和维护。您将在同一个文件中同时拥有UI和业务逻辑。您将无法使用MVVM模式,单元测试将变得更加困难 在帮助您精简XAML的系列中。互联网上还有很多其他资源 下面是您上面描述的布局的XAML
<?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="Example.Pages.MainPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0">
<Image Source="image.png"/>
<Label Text="Image Caption"/>
</StackLayout>
</Grid>
</ContentPage>
这就是所谓的XAML.shot!好的,谢谢你的更正,你的问题本质上是“请帮我把这一大块XAML翻译成C”?这不太适合这样做。向我们展示一个您试图构建的UI的模型,并询问有关您遇到问题的部分的具体问题,可能更合适。对不起,我不是故意这样做的。我很难理解如何用c#实现同样的目标。我基本上是想把屏幕分成一个30%的上半部分,剩下的部分是下半部分。在上半部分中,我想将图片居中,并在其下方添加标签。在过去的8个小时里,我一直在讨论这个问题,但一直没有进展:(看,这是一个比你实际问的好得多的问题。我个人会从一个网格开始,第一行是30%,第二行是70%。
<?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="Example.Pages.MainPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0">
<Image Source="image.png"/>
<Label Text="Image Caption"/>
</StackLayout>
</Grid>
</ContentPage>