LoginSignup
13
14

More than 5 years have passed since last update.

Xamarin.Forms(Prism.Forms)でApp.xamlを作成して共通のStyleを定義する

Last updated at Posted at 2016-05-26

Xamarin.Formsのプロジェクトテンプレートでプロジェクトを作成した場合、AppクラスのApp.cs(プロジェクト名.cs)は作成されますがApp.xamlは作成されません。共通のスタイルなどのリソースをXamlで定義したい場合は改めて作成する必要があります。ここではXamarinStudioで作成する方法を紹介します。
prism.formsを使用している場合の方法も後半に書いておきます。

追記
Xamarin Studio 6.0以降はソリューション作成の時に
Use XAML for user interface files
にチェックを入れるとApp.Xamlを作成してくれるようになりました。

手順

PCLプロジェクト右クリック→追加→新しいファイル→Forms→Forms ContentPage Xaml
でファイルを追加。「App」などAppクラスに使いたい名前を付ける。

App.xaml.csを以下のように書き換える。
元々のApp.cs相当に既にコードを書いている場合はそれも適宜移植する。

App.xaml.cs
using Xamarin.Forms;

namespace Sample
{
    public partial class App : Application
    {
        public App ()
        {
            InitializeComponent ();
            MainPage = new Sample.MainPage();
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }
    }
}

App.xamlを以下のように変更

App.xaml
<?xml version="1.0" encoding="UTF-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Sample.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Label"></Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

元々のAppクラスのファイルはとりあえずコメントアウトしておいて動作確認とれたら削除する。
ここで定義したStyleはアプリケーション全体に適用される。

prism.formsの場合の手順

通常と手順は同じですがApplicationクラスじゃなくてPrismApplicationクラスを継承するコードを使用します。

App.xaml.cs

    public partial class App : PrismApplication
    {

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }


        protected override async void OnInitialized()
        {

            InitializeComponent ();
            await this.NavigationService.NavigateAsync("MainPage");
        }

        protected override void RegisterTypes()
        {
            this.Container.RegisterTypeForNavigation<MainPage> ();
        }
    }
App.xaml
<?xml version="1.0" encoding="UTF-8"?>
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
             x:Class="Sample.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Label">
                <Setter Property="TextColor" Value="Red" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</prism:PrismApplication>

独自クラスを継承した場合のルート要素の書き方が分からず少しはまりました。

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14