问题描述
我正在尝试一种新方法,即XAML,以便在xamarin.forms中进行应用.目前,我面临重用具有图像和标签的堆栈布局的问题.我如何使用XAML在不同页面中重用布局.
I am trying a new approach i.e. XAML to make application in xamarin.forms. At this time i am facing an issue to reuse my stack layout which is having a image and label. How i can reuse my layout in different pages using XAML.
推荐答案
您实际上可以在单独的XAML文件中定义自定义组件,然后仅在需要的位置链接该组件.
You can actually define your custom component in a separate XAML file and then just link the component wherever you need it.
例如,带有图像的标签可以在专用的XAML文件中分组在一起:
For example a label with image can be grouped together in a dedicated XAML file:
<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="UserControls.ImageWithTitle"
VerticalOptions="Center" HorizontalOptions="Center" >
<Label HorizontalOptions="Center"
x:Name="TitleLabel" />
<Image Source="noimage.png" />
</StackLayout>
在.cs文件中,我为TitleLabel定义了一个绑定
On the .cs file I've defined a binding for the TitleLabel
public string TitleName
{
get { return TitleLabel.Text; }
set { TitleLabel.Text = value; }
}
因此,当您将组件包括在另一个布局上时,可以直接(或通过绑定)分配标签值:
So when you include the component on another layout, you can assign the label value directly (or via a binding):
<usercontrols:ImageWithTitle TitleName="Home"/>
这篇关于Xamarin.forms中Xaml的可重用性用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!