작동하는 DataTypeWholeNumber 라는 다음 WPF UserControl 이 있습니다.
이제 DataTypeDateTime 및 DataTypeEmail 등 의 UserControl을 만들고 싶습니다 .
많은 종속성 속성이 이러한 모든 컨트롤에서 공유되므로 공통 메서드를 BaseDataType에 넣고 이러한 각 UserControls가이 기본 형식에서 상속 되도록하려고합니다 .
그러나 그렇게 할 때 오류가 발생합니다. 부분 선언에는 다른 기본 클래스가 없을 수 있습니다 .
그렇다면 공유 기능이 모두 기본 클래스에 있도록 UserControls로 상속을 구현하려면 어떻게해야합니까?
using System.Windows;
using System.Windows.Controls;
namespace TestDependencyProperty827.DataTypes
{
public partial class DataTypeWholeNumber : BaseDataType
{
public DataTypeWholeNumber()
{
InitializeComponent();
DataContext = this;
//defaults
TheWidth = 200;
}
public string TheLabel
{
get
{
return (string)GetValue(TheLabelProperty);
}
set
{
SetValue(TheLabelProperty, value);
}
}
public static readonly DependencyProperty TheLabelProperty =
DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
new FrameworkPropertyMetadata());
public string TheContent
{
get
{
return (string)GetValue(TheContentProperty);
}
set
{
SetValue(TheContentProperty, value);
}
}
public static readonly DependencyProperty TheContentProperty =
DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType),
new FrameworkPropertyMetadata());
public int TheWidth
{
get
{
return (int)GetValue(TheWidthProperty);
}
set
{
SetValue(TheWidthProperty, value);
}
}
public static readonly DependencyProperty TheWidthProperty =
DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber),
new FrameworkPropertyMetadata());
}
}
답변
xaml의 첫 번째 태그를 변경하여 새 기본 유형에서도 상속했는지 확인하십시오.
그래서
<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
>
된다
<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes"
>
따라서 아래 의견의 추가 세부 정보를 포함하여 전체 답변을 요약하십시오.
- 기본 클래스는 xaml 파일을 포함하지 않아야합니다. 단일 (부분이 아닌) cs 파일에 정의하고 Usercontrol에서 직접 상속하도록 정의합니다.
- cs 코드 숨김 파일과 xaml의 첫 번째 태그 (위에 표시된대로) 모두에서 하위 클래스가 기본 클래스에서 상속되는지 확인합니다.
답변
public partial class MooringConfigurator : MooringLineConfigurator
{
public MooringConfigurator()
{
InitializeComponent();
}
}
<dst:MooringLineConfigurator x:Class="Wave.Dashboards.Instruments.ConfiguratorViews.DST.MooringConfigurator"
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:dst="clr-namespace:Wave.Dashboards.Instruments.ConfiguratorViews.DST"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</dst:MooringLineConfigurator>
답변
이 기사에서 답을 찾았습니다. http://www.paulstovell.com/xmlnsdefinition
기본적으로 XAML에서 사용할 수있는 AssemlyInfo.cs 파일에 XML 네임 스페이스를 정의해야합니다. 그것은 나를 위해 일했지만 기본 사용자 컨트롤 클래스를 별도의 DLL에 배치했습니다 …
답변
동일한 문제가 발생했지만 디자이너가 지원하지 않는 추상 클래스에서 컨트롤을 상속 받아야했습니다. 내 문제를 해결 한 것은 usercontrol을 표준 클래스 (UserControl 상속)와 인터페이스 모두에서 상속하도록 만드는 것입니다. 이런 식으로 디자이너가 일하고 있습니다.
//the xaml
<local:EcranFiche x:Class="VLEva.SIFEval.Ecrans.UC_BatimentAgricole"
xmlns:local="clr-namespace:VLEva.SIFEval.Ecrans"
...>
...
</local:EcranFiche>
// the usercontrol code behind
public partial class UC_BatimentAgricole : EcranFiche, IEcranFiche
{
...
}
// the interface
public interface IEcranFiche
{
...
}
// base class containing common implemented methods
public class EcranFiche : UserControl
{
... (ex: common interface implementation)
}
답변
디자이너가 만든 부분적인 클래스 정의가 있으며 InitializeComponent () 메서드 정의를 통해 쉽게 열 수 있습니다. 그런 다음 부분 클래스 상속을 UserControl에서 BaseDataType (또는 클래스 정의에서 지정한 임의)으로 변경하면됩니다.
그 후에 InitializeComponent () 메서드가 자식 클래스에 숨겨져 있다는 경고가 표시됩니다.
따라서 UserControl 대신 CustomControl을 기본 클래스로 만들어 기본 클래스에서 부분적인 정의를 피할 수 있습니다 (하나의 주석에 설명 됨).
답변
