在 XAML ResourceDictionary 中,如何创建对嵌套类 IValueConverter 的引用?

发布于 2025-01-19 00:23:51 字数 2565 浏览 1 评论 0 原文

给定一个实现 ivalueconverter 的类:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ...;
    }

    // Never called.
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ...;
    }
}

创建一个不是嵌套类的键入的keyed resourcectionary条目,如果是

<ContentView
    ...
    xmlns:converters="clr-namespace:MyNamespace.Converters" 
    x:Class "MyNamespace.MyView">
    
    <ContentView.Resources>
        <ResourceDictionary>
            <converters:MyConverter x:Key="myConverter" />
            ...

nested class /em>:

public class OuterClass
{
    public class MyConverter : IValueConverter
    ...

嵌套类不能称为元素名称(无效xml也不是XAML):

// Won't compile.
<ContentView.Resources>
    <ResourceDictionary>
        <converters:OuterClass.MyConverter x:Key="myConverter" />
        ...

或者

        <converters:OuterClass+MyConverter x:Key="myConverter" />
        ...

,如果有将类名称移至属性值的等效XAML,则可以是完成,如我的答案在这里

<??? SomeProperty="converters:OuterClass+MyConverter" x:Key="myConverter" />

或者也许其中一个,per x:键入标记扩展

<x:Type TypeName="converters:OuterClass+MyConverter" x:Key="myConverter" />
<x:Type TypeName="converters:OuterClass.MyConverter" x:Key="myConverter" />
<??? SomeProperty="{x:Type converters:OuterClass+MyConverter}" x:Key="myConverter" />

我已经尝试过所有这些,加上其他所有这些变体(例如替换 ??? ivalueconverter object )。
到目前为止,我总是会遇到构建时间问题:

Type '...' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.

或者在打开视图时运行时:

System.RuntimeType doesn't implement interface Xamarin.Forms.IValueConverter
... SIGABRT

q:有没有办法指定 ivalueconverter 资源,当类型是嵌套类别时?

注意:

  • 我已经验证了该类是不是嵌套的。 [那是我现在正在使用的解决方案。但是我希望将类嵌套在类背后的代码中,因为这是此视图上使用的 。]

Given a class that implements IValueConverter:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ...;
    }

    // Never called.
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ...;
    }
}

It is straightforward to create a keyed ResourceDictionary entry, if that is not a nested class:

<ContentView
    ...
    xmlns:converters="clr-namespace:MyNamespace.Converters" 
    x:Class "MyNamespace.MyView">
    
    <ContentView.Resources>
        <ResourceDictionary>
            <converters:MyConverter x:Key="myConverter" />
            ...

But suppose it is a nested class:

public class OuterClass
{
    public class MyConverter : IValueConverter
    ...

A nested class can't be referred to as an element name (would not be valid XML nor XAML):

// Won't compile.
<ContentView.Resources>
    <ResourceDictionary>
        <converters:OuterClass.MyConverter x:Key="myConverter" />
        ...

OR

        <converters:OuterClass+MyConverter x:Key="myConverter" />
        ...

HOWEVER, if there is an equivalent XAML that moves the class name to a property value, then it can be done, as described in my answer here:

<??? SomeProperty="converters:OuterClass+MyConverter" x:Key="myConverter" />

OR MAYBE ONE OF THESE, per x:Type Markup Extension:

<x:Type TypeName="converters:OuterClass+MyConverter" x:Key="myConverter" />
<x:Type TypeName="converters:OuterClass.MyConverter" x:Key="myConverter" />
<??? SomeProperty="{x:Type converters:OuterClass+MyConverter}" x:Key="myConverter" />

I've tried all of these, plus other variations (e.g. replace ??? with IValueConverter, or object).
So far I've always gotten either build-time problem:

Type '...' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.

OR at runtime when open the view:

System.RuntimeType doesn't implement interface Xamarin.Forms.IValueConverter
... SIGABRT

Q: Is there a way to specify an IValueConverter resource, when the type is a nested class?

NOTE:

  • I've verified that the class works if it is not nested. [That is the solution I am using now. But I would prefer to nest the classes within the code behind class, because this is only used on this view.]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文