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

XAML Standard

Both Xamarin.Forms and UWP can utilize declarative UI pages with the EXtensible Application Markup Language (XAML). It was initially introduced as part of Windows Presentation Foundation and has been extensively used in .NET applications, starting with .NET 3.0.

While both development platforms offer similar UI elements, they use a slightly different set of controls and layouts, which might cause UI inconsistencies while you are creating cross-platform applications that target iOS/Android (with Xamarin.Forms) and UWP.

Let's take a look at the layout structures:

 

Similar to layouts are controls. By looking at the controls that are used on these platforms, you can easily spot the subtle differences:

 

If we were to recreate the MainPage XAML on UWP, the difference between the two platforms would be apparent.

On Xamarin.Forms, we have the following XAML structure:

<StackLayout>
<Label Text="{Binding Platform, StringFormat='Welcome to {0}!'}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>

This would have to be translated to the following (using a new page that we created in the UWP application  MainPageAlternative.xaml):

<StackPanel VerticalAlignment="Center">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Platform,
Converter={StaticResource FormatConverter},
ConverterParameter='Welcome to {0}!'}" />
</StackPanel>

Notice that StackLayout was translated into StackPanel, while TextBlock was translated into TextBlock. Additionally, the HorizontalOptions and VerticalOptions attributes were changed to the HorizontalAlignment and VerticalAlignment attributes. Another big difference is the fact that the StringFormat property of the Binding markup extension is not supported on UWP. In order to support the string formatting, we would need to create our own value converter.

The Window Community Toolkit is a collection of helper functions and custom controls that simplify a developer's tasks in regards to creating UWP apps. The toolkit already contains commonly used converters, one of which is called  StringFormatConverter.

Now, if we set the DataContext of MainPageAlternative to MainPageViewModel and assign this page as the first navigation page instead of the MainPage that was used to harness Xamarin.Forms, the resulting view would be the same:

rootFrame.Navigate(typeof(MainPageAlternative), e.Arguments);

In order to diminish the subtle differences between the two platforms, a new standardization initiative was introduced that aims to create a XAML standard, which can then be used on multiple development platforms (Xamarin.Forms and UWP). 

Our current control mappings are as follows:

 

At the moment, in order to use the XAML standard, you can introduce the Xamarin.Forms.Alias preview module for Xamarin.Forms and use the XAML Standard references while creating your views. After this introduction, our MainPage (we created a copy called MainPageAlternative) in the Xamarin.Forms project would look like this:

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="FirstXamarinFormsApplication.MainPageAlternative"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:alias="clr-namespace:Xamarin.Forms.Alias;assembly=Xamarin.Forms.Alias"
xmlns:local="clr-namespace:FirstXamarinFormsApplication">
<alias:StackPanel>
<!-- Place new controls here -->
<alias:TextBlock
HorizontalOptions="Center"
Text="{Binding Platform, StringFormat='Welcome to {0}!'}"
VerticalOptions="CenterAndExpand" />
</alias:StackPanel>
</ContentPage>

Following this initial phase of implementation and alignment, UWP and Xamarin.Forms are expected to continue to converge on both control and attribute levels.

主站蜘蛛池模板: 九寨沟县| 齐齐哈尔市| 阿鲁科尔沁旗| 长岭县| 泗阳县| 邻水| 灯塔市| 阳山县| 塔城市| 徐州市| 从化市| 九寨沟县| 洱源县| 彰武县| 寿阳县| 睢宁县| 新绛县| 鄯善县| 楚雄市| 仁寿县| 平罗县| 乐安县| 连江县| 克山县| 连南| 酒泉市| 灌云县| 凤山市| 丰顺县| 石楼县| 临高县| 巴彦淖尔市| 抚宁县| 溧阳市| 绵阳市| 崇仁县| 华坪县| 绥芬河市| 雅安市| 花莲县| 商都县|