FindResource在两个相同的代码路径之一上抛出异常

时间:2014-01-24 15:16:54

标签: c# wpf xaml

这是一个聊天应用程序。

我在Gender Enum上使用ValueConverer,它将查看用户的设置,以确定为给定的性别显示哪个资源。目前,该应用程序中有3个图标包,用户可以随时选择,并显示新图标中的任何新消息。这在IconChoice设置中定义,该设置是一个整数。

然而,我对女性资源有疑问,我无法弄清楚问题是什么。该图标将正好装入包3,但是当它被打包1时它会抛出ResourceReferenceKeyNotFoundException。在包之间来回切换没有区别,它将保持显示包3正常并继续投掷第1包的例外。

转换器:

public class GenderToGeometryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (Settings.Default.IconChoice)
        {
            case 0:
            default:
                return GetGenderDataPackOne((Gender)value);
            case 1:
                return GetGenderDataPackTwo((Gender)value);
            case 2:
                return GetGenderDataPackThree((Gender)value);
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }

    private Geometry GetGenderDataPackOne(Gender gender)
    {
        switch (gender)
        {
            case Gender.Male:
                return Application.Current.FindResource("Male") as Geometry;
            case Gender.Female:
                return Application.Current.FindResource("Female") as Geometry;
            case Gender.None: default:
                return Application.Current.FindResource("None") as Geometry;
        }
    }

    private Geometry GetGenderDataPackTwo(Gender gender)
    {
        switch (gender)
        {
            case Gender.Male:
                return Application.Current.FindResource("Male_Square") as Geometry;
            case Gender.Female:
                return Application.Current.FindResource("Female_Square") as Geometry;
            case Gender.None: default:
                return Application.Current.FindResource("None_Square") as Geometry;
        }
    }

    private Geometry GetGenderDataPackThree(Gender gender)
    {
        switch (gender)
        {
            case Gender.Male:
                return Application.Current.FindResource("Male") as Geometry;
            case Gender.Female:
                return Application.Current.FindResource("Female") as Geometry;
            case Gender.None: default:
                return Application.Current.FindResource("None") as Geometry;
        }
    }
}

XAML:

<Path Margin="2 0" Grid.Column="1"
 Fill="{Binding Converter={StaticResource GFC}}"
 Data="{Binding User.Gender, Converter={StaticResource GtG}}"
 Stroke="{Binding User.Gender, Converter={StaticResource GtGS}}"
 VerticalAlignment="Center" HorizontalAlignment="Center" />

资源本身:

<GeometryGroup x:Key="None">
    <EllipseGeometry RadiusX="8" RadiusY="8" Center="10,10" />
    <RectangleGeometry RadiusX="5" RadiusY="5" Rect="0,0,0,22">
        <RectangleGeometry.Transform>
            <TransformGroup>
                <RotateTransform Angle="45" CenterX="10" CenterY="10" />
                <TranslateTransform X="7.5" Y="6" />
            </TransformGroup>
        </RectangleGeometry.Transform>
    </RectangleGeometry>
    <GeometryGroup.Transform>
        <TransformGroup>
            <ScaleTransform ScaleX=".4" ScaleY=".4" />
        </TransformGroup>
    </GeometryGroup.Transform>
</GeometryGroup>
<GeometryGroup x:Key="Female">
    <EllipseGeometry RadiusX="8" RadiusY="8" Center="10,10" />
    <RectangleGeometry RadiusX="2" RadiusY="10" Rect="10,19,0,12" />
    <RectangleGeometry RadiusX="2" RadiusY="10" Rect="5,26,10,0" />
    <GeometryGroup.Transform>
        <TransformGroup>
            <ScaleTransform ScaleX=".4" ScaleY=".4" />
        </TransformGroup>
    </GeometryGroup.Transform>
</GeometryGroup>
<GeometryGroup x:Key="Male">
    <EllipseGeometry RadiusX="8" RadiusY="8" Center="10,10" />
    <GeometryGroup>
        <RectangleGeometry RadiusX="2" RadiusY="10" Rect="10,19,0,12" />
        <RectangleGeometry RadiusX="2" RadiusY="10" Rect="10,30,0,10">
            <RectangleGeometry.Transform>
                <RotateTransform CenterX="10.2" CenterY="30.5" Angle="-145" />
            </RectangleGeometry.Transform>
        </RectangleGeometry>
        <RectangleGeometry RadiusX="2" RadiusY="10" Rect="10,30,0,10">
            <RectangleGeometry.Transform>
                <RotateTransform CenterX="9.8" CenterY="30.5" Angle="145" />
            </RectangleGeometry.Transform>
        </RectangleGeometry>
        <GeometryGroup.Transform>
            <RotateTransform Angle="-155" CenterX="10" CenterY="10" />
        </GeometryGroup.Transform>
    </GeometryGroup>
    <GeometryGroup.Transform>
        <TransformGroup>
            <TranslateTransform Y="15" />
            <ScaleTransform ScaleX=".4" ScaleY=".4" />
        </TransformGroup>
    </GeometryGroup.Transform>
</GeometryGroup>

0 个答案:

没有答案