Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF自定义控件不可见-找不到主题?_C#_Wpf - Fatal编程技术网

C# WPF自定义控件不可见-找不到主题?

C# WPF自定义控件不可见-找不到主题?,c#,wpf,C#,Wpf,我正在尝试构建一个WPF自定义控件库,以便从不同的WPF软件项目中使用。我正在使用VisualStudio2019。我使用WPF自定义控件库(.NET Framework)模板创建了一个新项目,其中定义了文本框的子类: CustomControl1.cs: using System.Windows; using System.Windows.Controls; namespace CCL1 { public class CustomControl1 : TextBox {

我正在尝试构建一个WPF自定义控件库,以便从不同的WPF软件项目中使用。我正在使用VisualStudio2019。我使用WPF自定义控件库(.NET Framework)模板创建了一个新项目,其中定义了文本框的子类:

CustomControl1.cs:

using System.Windows;
using System.Windows.Controls;

namespace CCL1
{
    public class CustomControl1 : TextBox
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }
}
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CCL1">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CCL1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CCL1")]
[assembly: AssemblyCopyright("Copyright ©  2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page,
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
)]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        xmlns:ccl="clr-namespace:CCL1;assembly=CCL1"
        mc:Ignorable="d"
        Title="MainWindow" Height="Auto" Width="Auto">
    <StackPanel>
        <TextBox Text="Hello"/>
        <ccl:CustomControl1 Height="20" Text="Hello"/>
    </StackPanel>
</Window>
(稍后添加功能),此项目中的generic.xaml如下所示:

Generic.xaml:

using System.Windows;
using System.Windows.Controls;

namespace CCL1
{
    public class CustomControl1 : TextBox
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }
}
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CCL1">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CCL1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CCL1")]
[assembly: AssemblyCopyright("Copyright ©  2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page,
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
)]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        xmlns:ccl="clr-namespace:CCL1;assembly=CCL1"
        mc:Ignorable="d"
        Title="MainWindow" Height="Auto" Width="Auto">
    <StackPanel>
        <TextBox Text="Hello"/>
        <ccl:CustomControl1 Height="20" Text="Hello"/>
    </StackPanel>
</Window>

这是装配信息:

AssemblyInfo.cs:

using System.Windows;
using System.Windows.Controls;

namespace CCL1
{
    public class CustomControl1 : TextBox
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }
}
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CCL1">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CCL1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CCL1")]
[assembly: AssemblyCopyright("Copyright ©  2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page,
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
)]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        xmlns:ccl="clr-namespace:CCL1;assembly=CCL1"
        mc:Ignorable="d"
        Title="MainWindow" Height="Auto" Width="Auto">
    <StackPanel>
        <TextBox Text="Hello"/>
        <ccl:CustomControl1 Height="20" Text="Hello"/>
    </StackPanel>
</Window>
使用系统反射;
使用System.Runtime.InteropServices;
使用System.Windows;
//有关部件的一般信息通过以下方式控制
//属性集。更改这些属性值以修改信息
//与程序集关联的。
[大会:大会名称(“CCL1”)]
[组装:组装说明(“”)]
[程序集:程序集配置(“”)]
[大会:大会公司(“)]
[组装:组装产品(“CCL1”)]
[大会:大会版权所有(“版权©2019”)]
[组装:组装商标(“”)]
[大会:大会文化(“”)
//将ComVisible设置为false会使此程序集中的类型不可见
//到COM组件。如果需要从访问此程序集中的类型
//COM,将该类型的ComVisible属性设置为true。
[大会:ComVisible(false)]
//要开始构建可本地化的应用程序,请设置
//您正在.csproj文件中编码的文化
//在一个房间里。例如,如果您使用的是美国英语
//在源文件中,将设置为en US。然后取消注释
//下面是NeutralResourceLanguage属性。更新中的“en-US”
//下面的行与项目文件中的UICulture设置相匹配。
//[汇编:NeutralResourcesLanguage(“en-US”,UltimateResourceCallBackLocation.Satellite)]
[大会:主题信息(
ResourceDictionaryLocation.None,//主题特定的资源字典所在的位置
//(在页面中找不到资源时使用,
//或应用程序资源字典)
ResourceDictionaryLocation.SourceAssembly//通用资源字典所在的位置
//(在页面中找不到资源时使用,
//应用程序或任何特定于主题的资源字典)
)]
//程序集的版本信息由以下四个值组成:
//
//主要版本
//次要版本
//建筑编号
//修改
//
//可以指定所有值,也可以默认生成号和修订号
//通过使用如下所示的“*”:
//[汇编:汇编版本(“1.0.*)]
[汇编:汇编版本(“1.0.0.0”)]
[程序集:AssemblyFileVersion(“1.0.0.0”)]
然后,我将此项目添加为对主项目的引用,并希望在主窗口中使用自定义控件。像这样:

MainWindow.xaml:

using System.Windows;
using System.Windows.Controls;

namespace CCL1
{
    public class CustomControl1 : TextBox
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }
}
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CCL1">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CCL1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CCL1")]
[assembly: AssemblyCopyright("Copyright ©  2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page,
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
)]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        xmlns:ccl="clr-namespace:CCL1;assembly=CCL1"
        mc:Ignorable="d"
        Title="MainWindow" Height="Auto" Width="Auto">
    <StackPanel>
        <TextBox Text="Hello"/>
        <ccl:CustomControl1 Height="20" Text="Hello"/>
    </StackPanel>
</Window>

普通文本框只是用来比较的。我可以看到我的CustomControl1类,它是设计模式下TextBox的一个子类,但当我构建并启动应用程序时,它并不在那里。我的搜索告诉我这是因为找不到自定义控件的主题,特别是以下问题: 告诉我,如果我将第二个答案中详细说明的行添加到AssemblyInfo中,那么它应该可以工作-但是这一行已经包含在AssemblyInfo中(从一开始就是这样,它是项目模板的一部分),并且没有任何区别。我可以通过注释自定义控件的静态构造函数来显示它,但这似乎是一个残酷的攻击,我不想被限制为控件的默认主题,我希望它能按预期工作

最奇怪的是,几个小时前这确实奏效,但后来突然变成了我刚才描述的行为。我不记得有什么特别的改变来实现这一点

很抱歉重新发布了一个以前被问到的问题,但我只是不知道我在做什么,与已经给出的答案不同


编辑:重新构建时,自定义控件现在在设计模式下甚至不可见…

使用您提供的示例代码,控件模板没有任何显示。您正在设置
Text=“Hello”
,但是您没有
ContentPresenter
,也没有绑定到
Text
属性


我建议提取文本框的控件模板,并将其用作自定义控件的起点。

尝试在
控件模板的
边框中添加一些内容。您当前的模板似乎为空。这有助于感谢您。我在这里也得到了一些很好的信息:以防有人还在寻找。