Xamarin 的跨平台執行不同程式

  • 104
  • 0
  • 2017-01-29

Xamarin 的跨平台執行不同程式

沒智能感知真的很痛苦,尤其是OnPlatform 打成OnPlatForm

TypeArguments 打成 TypeArgument

雖然可以打開預編譯但還是有智能感知還是方便些

希望 未來能盡快改善..

<?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="App1.MainPage">
    <StackLayout x:Name="MainStackLayout" >
        <!--TODO Step2 Code insert here-->
        <StackLayout.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <OnPlatform.Android>
                    0, 0, 0, 0
                </OnPlatform.Android>
                <OnPlatform.iOS>
                    0, 20, 0, 0
                </OnPlatform.iOS>
                <!--UWP也適用OnPlatform.WinPhone-->
                <OnPlatform.WinPhone>
                    0, 0, 0, 0
                </OnPlatform.WinPhone>
            </OnPlatform>
        </StackLayout.Padding>
        <Button x:Name="TestButton" VerticalOptions="StartAndExpand"
                        BackgroundColor="Navy"
                        TextColor="White"
                        HorizontalOptions="FillAndExpand" Text="Test" />
    </StackLayout>
</ContentPage>

因為是xaml 苦手

所以最後還是決定用最簡單的方式,寫在C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace DifferenceUI
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            Device.OnPlatform(iOS: () =>
             {
                 this.MainstackLayout.Padding = new Thickness(0, 20, 0, 0);

                 this.BackgroundColor = Color.Aqua;
             }, Android: () =>
             {
                 this.MainstackLayout.Padding = new Thickness(0, 0, 0, 0);
                 this.BackgroundColor = Color.Yellow;
             }, WinPhone: () =>
             {
                 this.MainstackLayout.Padding = new Thickness(0, 0, 0, 0);
                 this.BackgroundColor = Color.Yellow;
             },
            Default: null);






        }
    }
}