[英]wpf custom control not recognized
This is my first foray into custom controls, and it's not going well. 这是我第一次尝试自定义控件,但进展并不顺利。 I have a custom graph control derived from Canvas.
我有一个从Canvas派生的自定义图形控件。
namespace Grapher2 {
public class SeriesManager : Canvas {
public SeriesManager() {
...
}
}
}
It's defined in the same project and namespace as my app. 它在与我的应用程序相同的项目和名称空间中定义。 I tried adding a reference to the control in XAML as follows:
我尝试在XAML中添加对控件的引用,如下所示:
<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">
<StackPanel Name="container" Width="700" Height="500">
<graph:SeriesManager Name="seriesManager" Width="700" Height="500" />
</StackPanel>
But when I try to reference the control name "seriesManager" in the code-behind for the Window, I get "The name 'seriesManager' does not exist in the current context." 但是,当我尝试在Window的代码中引用控件名称“ seriesManager”时,出现“名称'seriesManager'在当前上下文中不存在”。
Furthermore, the XAML editor will not render the Window, giving a huge stack trace with the error: "Type 'MS.Internal.Permissions.UserInitiatedNavigationPermission' in Assembly 'PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable." 此外,XAML编辑器将不会呈现窗口,从而产生巨大的堆栈跟踪,并显示以下错误:程序集'PresentationFramework中的“类型'MS.Internal.Permissions.UserInitiatedNavigationPermission',版本= 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'没有标记为可序列化。”
I imagine the solution is something stupidly simple for anyone who's done custom controls. 我认为对于完成自定义控件的任何人来说,解决方案都非常简单。 But I'm stumped.
但是我很沮丧。
did you try x:Name="seriesManager" in your xaml? 您是否在xaml中尝试了x:Name =“ seriesManager”?
Edit: This may not be the issue seeing how you said your xaml isn't rendering. 编辑:这可能不是您看到您的xaml无法呈现的问题。 I'm guessing once you get the xaml to render in the designer... the code behind will work better.
我猜想一旦让xaml在设计器中呈现...后面的代码将更好地工作。
Edit 2: Whenever I've had a problem with the designer rendering, it's because I'm doing something in the constructor of my custom control. 编辑2:每当我在设计器渲染方面遇到问题时,这是因为我在自定义控件的构造函数中进行了某些操作。 Check your SeriesManager to see if you are doing something in its constructor that is causing a problem.
检查您的SeriesManager,以查看您是否在其构造函数中执行了导致问题的操作。 Maybe you are referencing something that doesn't exist yet.
也许您引用的是尚不存在的东西。 If you do have extra code in your constructor, consider moving it to the UserControl_Loaded event.
如果构造函数中确实包含其他代码,请考虑将其移至UserControl_Loaded事件。
Backing up Scott's answer here, since he helped me solve it: 在这里备份Scott的答案,因为他帮助我解决了这个问题:
What I did wrong was trying to access the control BEFORE InitializeComponent(), but was confused by 2 other error messages somewhere else in the code. 我做错的是尝试在InitializeComponent()之前访问控件,但是在代码中的其他地方却被其他2条错误消息所混淆。 Just in case someone else has this error.
以防万一别人有这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.