为什么在运行XAML程序时出现“找不到名为'GUIDConverter'的资源”错误?

4zcjmb1e  于 2023-05-27  发布在  其他
关注(0)|答案(1)|浏览(148)

我有一个Xaml页面,它使用绑定转换将GUID转换为字符串,但是当运行程序时,我得到以下错误:

Exception: Cannot find resource named 'GUIDConverter'. Resource names are case sensitive.

我有IValueConverter接口作为

public class GUIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string? strGuid;
            try
            {
                strGuid = value.ToString();
            }
            catch (Exception)
            {
                return string.Empty;
                throw;
            }
            
            return strGuid;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Guid? OldGuid;
            try
            {
                OldGuid = Guid.Parse(value.ToString());
            }
            catch (Exception)
            {
                return Guid.Empty;
            }
            return OldGuid;
        }
    }

并在App.Xaml中将其引用为:

<Application ...
             xmlns:super="clr-namespace:DatabaseTest">
    <Application.Resources>
        <super:GUIDConverter x:Key="GUIDConverter"/>
    </Application.Resources>
</Application>

最后,我添加了转换器,如下所示:

<ListView.View>
                            <GridView>
                                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElementName=customerID, Converter={StaticResource GUIDConverter}, ConverterParameter=Normal}"/>
                                <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding firstName}"/>
                            </GridView>
                        </ListView.View>

我真的不知道为什么这是不工作,因为它似乎是写正确的方式,我会感激任何帮助
(Also,我正在使用Mah:Metro)

qlzsbp2j

qlzsbp2j1#

如果你想让它在StaticStaticResources中可访问,你必须把引用放在ResourceDictionary中,如下所示:

<Application ...
    xmlns:super="clr-namespace:DatabaseTest">
    <Application.Resources>
        <ResourceDictionary>
            <super:GUIDConverter x:Key="GUIDConverter" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

或者,如果您只在使用ListView的地方需要它,您可以将引用插入到Window.Resources中。

相关问题