c# - WPF 如何使用 FactoryMethod 进行自定义控件
问题描述
.NET 4.7.2
我有一个包含自定义文本框的简单用户控件。我试图只在 Xaml 中定义接口,并通过指定 FactoryMethod 创建自定义 TextBox 的实例。
<UserControl x:Class="MyProject.TestView"
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"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<local:IInterfaceTextBox x:FactoryMethod="local:Factories.CreateInterfaceTextBox" x:Arguments="{x:Null}" Text="{Binding Text}" CustomText="{Binding CustomText}" />
</Grid>
工厂类:
namespace MyProject
{
public class Factories
{
public static IInterfaceTextBox CreateInterfaceTextBox() => new InterfaceTextBox();
}
}
第一个错误: “http://schemas.microsoft.com/winfx/2006/xaml”命名空间中不存在属性“FactoryMethod”。
当我设置 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 错误消息说在http://schemas.microsoft.com/winfx/2009/xaml中找不到 UserControl
有谁知道如何解决这个问题?
以防万一您想知道,为什么我首先要尝试这样做……我对新事物感到好奇:-)
解决方案
推荐阅读
- c - ZeroMQ Radio/Dish 不接收消息
- c++ - 进一步传递可变参数
- matlab - 有没有办法在 MATLAB 中输出 ode 求解器的中间计算?
- javascript - 如何将 Zip 文件从 s3 存储桶下载到 node.js lambda 函数中的本地目录
- jenkins - 同一个 repo 和同一个分支中的多个 Jenkins 文件用于不同的目的
- html - 从输入字段获取原始文本而不是日期对象
- ssis - 即使使用错误的 Kingswaysoft 连接参数,SSIS 部署的项目也能成功验证
- r - 根据 R 中的条件计算日期之间的平均差
- react-native-flatlist - React Native Draggable Flatlist 仅适用于 ScrollView 但错误?
- java - 如何在消费者 Java 8 中抛出异常