今回はXamarin.Formsで使えるNugetパッケージを作成して、実際にプロジェクトに取り込み使ってみます。
1.VisualStudioを起動し新規を選択
![](https://xamaui.net/wp-content/uploads/2020/11/20200904125933-1024x608.png)
2.マルチプラットフォーム>ライブラリ>Xamarin.Forms クラスライブラリを選択
![](https://xamaui.net/wp-content/uploads/2020/11/20200904130057-1024x731.png)
3.プロジェクト名、ソリューション名、プロジェクトファイルの保存先を入力し、作成
![](https://xamaui.net/wp-content/uploads/2020/11/20200904130239-1024x697.png)
4.プロジェクトフォルダを選択しダブルクリック
![](https://xamaui.net/wp-content/uploads/2020/11/20200904130429-1024x609.png)
5.Nugetパッケージ > メタデータ
id,バージョン,作成者,説明を入力しOKを押す
![](https://xamaui.net/wp-content/uploads/2020/11/20200904130614-1024x744.png)
6.プロジェクトファイル内にパッケージ化したい処理を書く
今回は例として、タップしたら色の変わるBoxViewを作成
![](https://xamaui.net/wp-content/uploads/2020/11/20200904131157-1024x514.png)
using System;
using Xamarin.Forms;
namespace NugetTest {
public class MyBoxView : BoxView {
public static readonly BindableProperty TapCommandProperty =
BindableProperty.Create(
nameof(TapCommand),
typeof(Command),
typeof(MyBoxView),
null,
propertyChanged: (bindable, oldValue, newValue) => ((MyBoxView)bindable).TapCommand = (Command)newValue);
public Command TapCommand {
get { return (Command)GetValue(TapCommandProperty); }
set { SetValue(TapCommandProperty, value); }
}
public MyBoxView() : base() {
var tgr = new TapGestureRecognizer();
tgr.Tapped += (sender, e) => OnClicked(sender, e);
this.GestureRecognizers.Add(tgr);
}
private void OnClicked(object sender, EventArgs e) {
var myBoxView = sender as MyBoxView;
myBoxView.BackgroundColor = Color.Red;
if (TapCommand != null && TapCommand.CanExecute(""))
TapCommand.Execute("");
}
}
}
7.ビルド構成をReleaseに変更
![](https://xamaui.net/wp-content/uploads/2020/11/20200904131325-1024x490.png)
8.プロジェクトフォルダを右クリックし、Nugetパッケージの作成を押す
![](https://xamaui.net/wp-content/uploads/2020/11/20200904131435.png)
9.Finderで作成したプロジェクトのフォルダを開き プロジェクトフォルダ>bin>Releaseの中に「.nupkg」の拡張子のファイルができているので、デスクトップにコピーしておきます。
ここからは実際にNugetパッケージを使う工程です。
10.作成したnugetパッケージを使いたいプロジェクトファイルをVisualStudioで開く
11.プロジェクトのNugetパッケージの管理を開く
![](https://xamaui.net/wp-content/uploads/2020/11/20200904132147-1024x520.png)
12.Nugetパッケージの管理のページ上部のタブから「ソースの構成」を選択
![](https://xamaui.net/wp-content/uploads/2020/11/20200904132329.png)
13.Nuget>ソースを選択後、追加を押す
![](https://xamaui.net/wp-content/uploads/2020/11/20200904132422-1024x747.png)
14.名前にDesktopと入力、場所にデスクトップのパスを入れ、ソースを追加を押す
(ユーザー名とパスワードは未入力でOK)
![](https://xamaui.net/wp-content/uploads/2020/11/20200904132656.png)
15.再度Nugetパッケージの管理のページを表示すると上部のタブから「Desktop」が選択できるようになるので選択
16.先ほど作成し、デスクトップにコピーしたNugetパッケージが選択できるようになっているので選択し、パッケージを追加する
17.追加したパッケージが使えるようになるので先ほど作った「MyBoxView」を使ってみる
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="test1.MainPage"
xmlns:nugetTest="clr-namespace:NugetTest;assembly=NugetTest">
<StackLayout
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<nugetTest:MyBoxView
BackgroundColor="Aqua"
WidthRequest="100"
HeightRequest="100"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"/>
</StackLayout>
</ContentPage>
xmlns:nugetTest=”clr-namespace:NugetTest;assembly=NugetTest”
↑ここのnamespaceとassemblyには自分がNugetパッケージを作る際に設定したプロジェクト名(namespace)を記述してください。
このようにして自分で作ったコントロール等をnuget化し使うことができます。
実際に動かしてみると
↓ページ表示
![](https://xamaui.net/wp-content/uploads/2020/11/20200904133524-596x1024.png)
↓Boxをタップ
![](https://xamaui.net/wp-content/uploads/2020/11/20200904133601-596x1024.png)
ちゃんと動きました。