官术网_书友最值得收藏!

XAML markup extensions

Up until now, when we created XAML views, we resorted to several markup extensions that are supported either by the Xamarin.Forms framework or the XAML namespace itself. Some of these extensions are as follows:

  • x:Reference: Used to refer to another view on the same page
  • Binding: Used throughout the view model implementations
  • StaticResource: Used to refer to styles

These are all markup extensions that are resolved by the associated service implementation within the Xamarin.Forms framework.

For specific needs in your application, custom markup extensions can be implemented to create a more maintainable XAML structure. In order to create a markup extension, the IMarkupExtension<T> class needs to be implemented. This depends on the type that needs to be provided.

For instance, in our previous example, the error label and the field descriptors were hard coded into the XAML view. This would create issues if the application needs to support multiple localizations. This can be resolved by doing the following:

  1. First, we need to create a markup extension that will translate the associated text values:
 [ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension<string>
{
public string Text { get; set; }

public string ProvideValue(IServiceProvider serviceProvider)
{
// TODO:
}

object IMarkupExtension.ProvideValue(IServiceProvider
serviceProvider)
{
return (this as
IMarkupExtension<string>).ProvideValue(serviceProvider);
}
}
  1. Note that the Text property is set as ContentProperty, which allows developers to provide a value for this extension simply by adding a value for the extension. Let's incorporate it into the XAML structure:
<Label Text="{behaviors:Translate LblUsername}" />
<Entry x:Name="usernameEntry" Placeholder="username"
Text="{Binding UserName, Mode=OneWayToSource}" >
<Entry.Behaviors>
<behaviors:ValidationBehavior x:Name="UserNameValidation" ValidationRule="{Binding BindingContext.UserNameValidation, Source={x:Reference RootView}}" />
</Entry.Behaviors>
</Entry>
<Label Text="{behaviors:Translate LblRequiredError}"
FontSize="12" TextColor="Gray"
IsVisible="{Binding HasError, Source={x:Reference UserNameValidation}}"/>
  1. ProvideValue method would therefore need to translate the LblUsername and LblRequiredError keys:
 public string ProvideValue(IServiceProvider serviceProvider)
{
switch (Text)
{
case "LblRequiredError":
return "This a required field";
case "LblUsername":
return "Username";
default:
return Text;
}
}

This completes the quadrant II customizations. Now, we will move on to quadrant III and the customize native controls.

主站蜘蛛池模板: 芜湖市| 邵东县| 驻马店市| 牙克石市| 二连浩特市| 鄂伦春自治旗| 安吉县| 多伦县| 长宁县| 建宁县| 民和| 抚州市| 鄂托克前旗| 潼关县| 胶南市| 汉中市| 太湖县| 册亨县| 应城市| 顺平县| 博罗县| 宁晋县| 永安市| 邵武市| 正安县| 上蔡县| 沙雅县| 吉林市| 吴江市| 开平市| 贵定县| 财经| 日照市| 绵阳市| 昌吉市| 阿克陶县| 巴林左旗| 岢岚县| 普洱| 伊通| 广饶县|