xamarin.forms 中 Xaml 的可重用性用户控件 [英] reusability user controls of Xaml in xamarin.forms
问题描述
我正在尝试一种新方法,即 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 的可重用性用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!