Java 出身の私は、GUI コンポーネントを作成する際の一般的な方法に慣れています。通常、GUI コンポーネントのすべての共通オブジェクトを含むある種の基本クラスを作成し、それを拡張します。
基本的に、これは C# と XAML で実現したいことです。
質問を明確にするために、私がやっていることの例を次に示します (これは機能していません!)。
独自の XAML を持つ基本クラスがあります。
<UserControl x:Class="BaseClass"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Border BorderBrush="Aqua" BorderThickness="10" CornerRadius="10" x:Name="Border" HorizontalAlignment="Left" Height="480" VerticalAlignment="Top" Width="480"/>
</Grid>
</UserControl>
そして、最初のクラスを拡張するクラスがあります
<base:BaseClass x:Class="DerivedClass"
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:base="clr-namespace:BaseClass"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="60" d:DesignWidth="200">
<Grid x:Name="LayoutRoot" Margin="0" Width="200" Height="60" MaxWidth="200" MaxHeight="60" Background="{StaticResource PhoneAccentBrush}">
<TextBlock x:Name="dummyText" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Dummy Plugin" VerticalAlignment="Top" Height="40" Width="180" Foreground="White" TextAlignment="Center"/>
</Grid>
</base:BaseClass>
DerivedClass
2 つの XAML コードから始めて、BaseClass
コンテナーに を入れたいと思います。これにより、必要なたびにコードを記述することなく、さまざまな派生クラス間でコンポーネントを共有できます。
たとえば、すべてのコンポーネントに丸みを帯びた境界線を持たせたい場合は、それをベース クラスに配置し、それを書き換えずにすべての派生クラスに配置したいと考えています。
もちろん、各 C# クラスには独自のInitializeComponent()
メソッドがあり、これはおそらく、派生コンポーネントが基本クラスのコンテンツを削除して独自のコンテンツを構築することを意味します。
コンストラクターからメソッドを削除するDerivedClass
と、派生クラスでも基本コンテンツが得られますが、DerivedClass
.
から基本コンストラクターを呼び出してもDerivedClass
、派生した前に呼び出されるため、効果はありませんInitializeComponent()
。
問題は、派生クラスの XAML 設計を壊すことなく、基本クラスから派生クラスに XAML 設計を使用するにはどうすればよいかということです。デザイナー自体で作業しながら、コンテンツを基本クラスに単純に追加する方法はありますか?
(派生クラスの XAML を削除して、コードでやりたいことを実行できることはわかっていますが、デザイナーだけでこれを実行できるかどうかを知りたいのです。デザイナーが利用可能)
編集:
HighCore の返信に続いて、Windows Phone で動作することを行いましたが、正しいことを行っているかどうかはわかりません (はい、動作しますが、単に間違っている可能性があります!)。
これが私がしたことです:
BaseControl.xaml
<UserControl x:Class="TestInheritance.BaseControl"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<TextBlock HorizontalAlignment="Center">BASE</TextBlock>
<ContentPresenter Name="Presenter" Content="{Binding PresenterContent}"/>
</Grid>
</UserControl>
BaseControl.xaml.cs
namespace TestInheritance
{
public partial class BaseControl : UserControl
{
public Grid PresenterContent { get; set; }
public BaseControl()
{
DataContext = this;
InitializeComponent();
}
}
}
DerivedControl.xaml
<local:BaseControl x:Class="TestInheritance.DerivedControl"
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:TestInheritance"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<local:BaseControl.PresenterContent>
<Grid>
<TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Center">DERIVED</TextBlock>
</Grid>
</local:BaseControl.PresenterContent>
</local:BaseControl>
他の理由でいくつかの共通のプロパティ/メソッドを持つ必要があるため、DerivedClass
は のインスタンスであることに注意してください。BaseClass
私の解決策についてどう思いますか? それは理にかなっていますか?