gpt4 book ai didi

wpf - 如何将 UserControl 添加到 WPF 窗口上的面板

转载 作者:行者123 更新时间:2023-12-04 12:42:29 27 4
gpt4 key购买 nike

我想我在这里遗漏了一些应该很明显的东西,但是我在这个上画了一个空白。

我构建了一个非常原始的 UserControl,它只包含一个 TextBox用作日志窗口:

<UserControl x:Class="My.LoggerControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="LoggerView">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="LogWindow" AcceptsReturn="True"/>
</Grid>
</UserControl>

我不认为这是最好的方法,但对于原型(prototype)来说应该足够好。

代码隐藏同样简单:
public partial class LoggerControl : UserControl, ILogger
{
public LoggerControl()
{
InitializeComponent();
}

private LogLevel level = LogLevel.Warning;

#region ILogger

public LogLevel Level
{
get { return level; }
set { level = value; }
}

public void OnError(string s)
{
if (level >= LogLevel.Error)
LogWindow.AppendText("ERROR:::" + s + "\n");
}

// ...
#endregion
}

我想不通的是如何将此控件添加到我的 MainWindow.xaml .简化,假设我的窗口如下所示:
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My"
Title="Test" Height="350" Width="525">
<Grid>
<local:LoggerControl x:Name="LogView" />
</Grid>
</Window>

即使如此简单,Visual Studio 2010 中的设计器也无法加载主窗口。给出的错误是:

A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.



此错误消息在主要搜索引擎中只有一个不相关的命中(加上重复),所以我没有找到任何有用的帮助。微软自己的文档似乎暗示这应该可行。

知道如何解决这个问题吗?

最佳答案

<UserControl x:Class="My.LoggerControl"


xmlns:local="clr-namespace:My.LogTest"

看起来您可能在命名空间中犯了错误? LoggerControl 被列为命名空间 My,而您正在导入 My.LogTest 并将其分配给 xml-prefix 本地。将其更改为:
xmlns:local="clr-namespace:My"

我认为它应该工作。否则,修复 LoggerControl 声明。

关于wpf - 如何将 UserControl 添加到 WPF 窗口上的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11231649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com