提督業プラグインを作ってみよう(1)とりあえずウインドウを出そう

1.プロジェクトを作ろう

メニューから「新規作成」「プロジェクト」を選んで、「WPFユーザーコントロールライブラリ」を選びます。プロジェクト名は適当なものを入力しておいてください。

f:id:reniris:20180905192254p:plain

2.余計なファイルを消してプロジェクト構造を整理しよう

作成したプロジェクトから

f:id:reniris:20180905193235p:plain

UserControl1.xamlを削除して「Models」「Views」「ViewModels」フォルダを追加します。

f:id:reniris:20180905205757p:plain

3.Nugetから必要なライブラリをインストールしよう

KanColleViewer.Composition(必須) KanColleViewer.PluginAnalyzer(ほぼ必須) KanColleWrapper(艦これの情報を取るなら必要) LivetCask(この連載では使います) をNugetからインストールしましょう

4.プラグイン本体を実装しよう

プロジェクト直下にクラスファイルをひとつ、Viewsフォルダにユーザーコントロールひとつと、ウインドウひとつ、ViewModelsフォルダにクラスひとつを追加します。

メインの情報表示ユーザーコントロール(まだ空)

<UserControl x:Class="プロジェクト名.Views.Information"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:プロジェクト名.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
            
    </Grid>
</UserControl>

メインのウインドウ(Informationが入っているだけ)

<Window x:Class="プロジェクト名.Views.InformationWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:プロジェクト名.Views"
        mc:Ignorable="d"
        Title="InformationWindow" Height="450" Width="800">
    <local:Information/>
</Window>

メインのViewModel(まだ空)

class InformationWindowViewModel : Livet.ViewModel
{
    public InformationWindowViewModel()
    {

    }
}

プラグイン本体

class Plugin : IPlugin, IDisposable
{
    private Views.InformationWindow info;  //メインのView
    private ViewModels.InformationViewModel infovm;    //メインのViewModel

    public Plugin()
    {
    }

    //プラグインが読み込まれたときに呼ばれる
    public void Initialize()
    {
        infovm = new ViewModels.InformationViewModel();
        info = new Views.InformationWindow
        {
            DataContext = infovm
        };
        info.Show();
    }

    public void Dispose()
    {
        //ここに終了時の処理を書く
    }
}

ソースはブログに乗せるために余計なものを省いています。 これらのソースをいい感じに修正してビルドしたdllをKanColleViewerのPluginsフォルダにコピーすると、空っぽのウインドウが出るようになります。

参考サイト

KanColleViewer プラグインの作り方とか – CAT EARS