Unable to cast object of type 'Microsoft.Expre

2019-08-14 09:57发布

I have created a Style for Window in WPF

<Style x:Key="JIMSWindow" TargetType="Window">
    <Setter Property="ResizeMode" Value="NoResize"/>
    <Setter Property="ShowInTaskbar" Value="False"/>
    <Setter Property="SizeToContent" Value="WidthAndHeight"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="WindowStyle" Value="None"/>
    <Setter Property="BorderThickness" Value="0"/>        
    <Setter Property="AllowsTransparency" Value="True"/>                
    <Setter Property="loc:CloseWindow.EscapeClosesWindow" Value="True"></Setter>
</Style>

and the EscapeCloseWindow class is this.

public static class CloseWindow 
    {
        public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
           "EscapeClosesWindow",
           typeof(bool),
           typeof(CloseWindow ),
           new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));

        public static bool GetEscapeClosesWindow(DependencyObject d)
        {
            return (bool)d.GetValue(EscapeClosesWindowProperty);
        }    
        public static void SetEscapeClosesWindow(DependencyObject d, bool value)
        {
            d.SetValue(EscapeClosesWindowProperty, value);
        }    
        private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Window target = (Window)d;
            if (target != null)
            {
                target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
            }
        }    
        private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Window target = (Window)sender;  
            // If this is the escape key, close the window
            if (e.Key == Key.Escape)
                target.Close();
        }
    }

Now when ever i open my View in WPF MVVM i am getting an exception like

Unable to cast object of type 'Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance' to type 'System.Windows.Window'.
   at JIMS.Commands.CloseWindow.OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in C:\Documents and Settings\323582\Desktop\JIMS\JIMS.Commands\CloseWindow.cs:line 84
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.StyleHelper.ApplyStyleOrTemplateValue(FrameworkObject fo, DependencyProperty dp)
   at System.Windows.StyleHelper.InvalidateContainerDependents(DependencyObject container, FrugalStructList`1& exclusionContainerDependents, FrugalStructList`1& oldContainerDependents, FrugalStructList`1& newContainerDependents)
   at System.Windows.StyleHelper.DoStyleInvalidations(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle)
   at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
   at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Microsoft.Expression.Platform.WPF.WpfDependencyPropertyImplementation.SetValue(Object target, Object valueToSet)
   at Microsoft.Expression.DesignModel.Metadata.DependencyPropertyReferenceStep.SetValue(Object target, Object valueToSet)
   at Microsoft.Expression.DesignModel.Core.InstanceBuilderOperations.SetValue(Object target, IProperty propertyKey, Object value)
   at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification)
   at Microsoft.Expression.DesignModel.InstanceBuilders.DependencyObjectInstanceBuilderBase`1.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification)
   at Microsoft.Expression.Platform.WPF.InstanceBuilders.FrameworkElementInstanceBuilder.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification)
   at Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstanceBuilder.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification)
   at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode)

Please can any one help me to resolve this issue.

标签: c# wpf mvvm window
2条回答
混吃等死
2楼-- · 2019-08-14 10:24

you can try to change

 Window target = (Window)d;

to

Window target = d as Window

in my machine ,it can go well.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-14 10:25

You can add a reference to Radical Framework and change the definition of the Dependecy Property:

        public static readonly DependencyProperty ShowIconProperty = DependencyProperty.RegisterAttached(
                            "ShowIcon",
                            typeof(bool),
                            typeof(WindowsManager),
                            new FrameworkPropertyMetadata(true, new PropertyChangedCallback((d, e) =>
                                {
                                    if (!DesignTimeHelper.GetIsInDesignMode())
                                        RemoveIcon((Window)d);
                                }
                                )));
查看更多
登录 后发表回答