0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UserControlで部分テンプレートを作る

Posted at

UserControlで部分テンプレートを作る

生のC#で行う方法とprismライブラリを使用した場合で比較しました。

基本の方法

  • 以下のディレクトリ構成にてMainWindow.xamlにRingo.xamlを表示する
Solution'Item'
  Item(project)
    View
      MainWindow.xaml
      Fruit
        Ringo.xaml
MainWindow.xaml
<Window
    x:Class="Fruit.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Fruit">
    <StackPanel>
	//「local」は表示したいxaml(xaml.cs)のnamespace
        <local:Ringo/>
	<TextBox Text="ここはMainWindow">
    </StackPanel>
</Window>
Ringo.xaml
<UserControl
    x:Class="Fruit.SimpleUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Orientation="Horizontal">
	<TextBox Text="ここはりんご">
    </StackPanel>
</UserControl>

PrismのRegionManagerを使った場合

  • ディレクトリを変更
    Solution'Item'
    Item(project)
    View
    Fruit
    Ringo.xaml
    MainWindow.xaml
    MainWindow.xaml.cs //New!
    RegionNames.cs //New!
RegionNames.cs
//文字列を変数として扱うクラス
//なくてもできるけどintelisenceが働くからあった方が良い
namespace Item
{
    public static class RegionName
    {
        public static readonly string Ringo = "Ringo";
    }
}
MainWindow.xaml.cs
//コードビハインドでRegionManagerを登録
namespace Item.View
{
    public partial class MainWindow : Window
    {
        public MainWindow(IRegionManager regionManager)
        {
            InitializeComponent();
            regionManager.RegisterViewWithRegion(RegionName.Ringo, typeof(Ringo));
        }
    }
}
MainWindow.xaml
<Window x:Class="Item.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/" //New!
        prism:ViewModelLocator.AutoWireViewModel="True" //New!
        xmlns:Item="clr-namespace:Item" //New!
        Title="Item">
    <Grid>
	//prismのRegionNameに文字列を登録している
	//文字列はx:static
	//xmlns:ItemというnamespaceのRegionNameクラスにあるRingoプロパティ
        <ContentControl prism:RegionManager.RegionName="{x:Static Item:RegionName.Ringo}">
	</ContentControl>
    </Grid>
</Window>

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?