2012-04-18 17 views
1

私はWPF入力するタイプ「Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance」のオブジェクトをキャストできません「System.Windows.Window」

<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> 

EscapeCloseWindowでウィンドウのスタイルを作成しましたクラスはこれです。私はWPF MVVMで私Viewを開くかつてとき

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(); 
     } 
    } 

は、今私は、いずれかが、私はこの問題を解決するのに役立ちますしてください

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) 

のような例外を取得しています。あなたは私のマシンで

Window target = d as Window 

Window target = (Window)d; 

を変更しようとすることができます

+1

送信者がウィンドウの場合だけをチェックする方法は? – Nikolay

+0

これがウィンドウインスタンスかどうかを確認することは、ソリューションの半分です。このコードはデザイナーによって実行され、 'd'変数は' Microsoft.Expression.WpfPlatform.InstanceBuilders.WindowInstance'のインスタンスになるため、実行時にキャストは必然的にヌル値になるため、チェックする必要がありますキャストがnullを返すかどうかそれがヌルの場合は、ロジックを実行したくないビジュアルスタジオデザイナーで実行されているので、ロジックをスキップすることができます。 – Triynko

+0

私はここにこの質問へのリンクを追加する価値があると思う - http://stackoverflow.com/questions/419596/how-does-the-wpf-button-iscancel-property-work – sarh

答えて

2

、それがうまく行くことができます。

+1

あなたは実際に参照を追加する必要がありますMicrosoft.Expression.WpfPlatform.dllを開き、変数 'd'がMicrosoft.Expression.WpfPlatform.InstanceBuilders.WindowInstanceまたはSystem.Windows.Windowのインスタンスであるかどうかをテストします。これは、コードがデザイナーが実行するか、通常通り実行します。実際には、ロジックが実際にデザイナーで何かをすることは望ましくないので、おそらくは "as Window"経由でキャストし、nullの場合はそれを無視するのが最も良いでしょう。 – Triynko

0

あなたはRadical Frameworkへの参照を追加し、Dependecyプロパティの定義を変更することができます。

 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); 
           } 
           ))); 
関連する問題