如何创建带有非字符串参数的 MarkupExtension?
我正在 WPF 应用程序中开发自定义标记扩展。 我见过的每个文档示例都使用 XAML 中的字符串参数来构造新对象。 是否可以使用非字符串参数?
换句话说,我怎样才能做这样的事情呢?
[MarkupExtensionReturnType(typeof(Uri))]
public class RefPackUriExtension : MarkupExtension
{
object _assembly = null;
public RefPackUriExtension() { }
public RefPackUriExtension(object assembly)
{
this._assembly = assembly;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
//return an object by using the "_assembly" member somehow
}
}
I am working on a Custom MarkupExtension within a WPF application. Every documented example I have seen uses string parameters from XAML to construct the new object. Is it possible to use a non-string parameter?
In other words, how can I do something like this?
[MarkupExtensionReturnType(typeof(Uri))]
public class RefPackUriExtension : MarkupExtension
{
object _assembly = null;
public RefPackUriExtension() { }
public RefPackUriExtension(object assembly)
{
this._assembly = assembly;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
//return an object by using the "_assembly" member somehow
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MarkupExtension
的任何参数都遵循与 CLR 对象属性相同的解析行为。 您可以使用TypeConverter
允许用户提供转换为目标类型的string
,也可以使用另一个MarkupExtension
。作为前者的示例,请参阅
ColorConverter
类。 作为后者的示例,请参阅RelativeSource
类(在Binding
MarkupExtension
中使用)。Any parameters to your
MarkupExtension
are subject to the same parsing behavior as properties on CLR objects. You can use aTypeConverter
to allow the user to provide astring
that is converted to the target type, or you can use anotherMarkupExtension
.As an example of the former, see the
ColorConverter
class. As an example of the latter, see theRelativeSource
class (which is used within theBinding
MarkupExtension
).