c# - 从 ScatterViewItem 中删除阴影不起作用

标签 c# .net wpf pixelsense

我总是使用此代码从 ScatterViewItem 中删除阴影:

 ScatterViewItem svi = new ScatterViewItem();
 svi.ApplyTemplate();
 svi.Background = new SolidColorBrush(Colors.Transparent);
 svi.ShowsActivationEffects = false;
 svi.BorderBrush = System.Windows.Media.Brushes.Transparent;
 Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome ssc;
 ssc = svi.Template.FindName("shadow", svi) as Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome;
 ssc.Visibility = Visibility.Hidden;

但最近这不再起作用了。我总是遇到这个异常:

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=Prototype_Concept_2
  StackTrace:
       at Prototype_Concept_2.PackageView.expand(Package pck) in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\PackageView.xaml.cs:line 45
       at Prototype_Concept_2.PackageView..ctor(SourceFile focus) in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\PackageView.xaml.cs:line 31
       at Prototype_Concept_2.DetailChooser.ShowPackage_PreviewContactDown(Object sender, ContactEventArgs e) in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\DetailChooser.xaml.cs:line 213
       at Microsoft.Surface.Presentation.ContactEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at Microsoft.Surface.Presentation.InputSurfaceProviderBase.DoProcessInput(Object obj)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Prototype_Concept_2.App.Main() in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

这意味着在行 ssc = svi.Template.FindName("shadow", svi) as Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome; 肯定有一个 NullReference,但我不知道为什么。

最佳答案

问题是 ApplyTemplate 返回 false,因此未应用模板,这意味着它为空。这解释了 NullReferenceException。您可以做的是附加一个事件处理程序,该事件处理程序订阅 ScatterViewItem 的 Loaded 事件,并在处理程序中执行相同的操作。在WPF中,当Loaded事件发生时,模板总是被应用

ScatterViewItem svi = new ScatterViewItem();
svi.Background = new SolidColorBrush(Colors.Transparent);
svi.ShowsActivationEffects = false;
svi.BorderBrush = System.Windows.Media.Brushes.Transparent;

RoutedEventHandler loadedEventHandler = null;
loadedEventHandler = new RoutedEventHandler(delegate
{
    svi.Loaded -= loadedEventHandler;
    Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome ssc;
    ssc = svi.Template.FindName("shadow", svi) as Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome;
    ssc.Visibility = Visibility.Hidden;
});
svi.Loaded += loadedEventHandler;

关于c# - 从 ScatterViewItem 中删除阴影不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4602013/

相关文章:

c# - 向对象数组添加元素

c# - 一次在十个图片框上显示多张图片

javascript - 使用 AES 在 Crypto.js 和 .NET 密码学中进行不同的加密

.net - 跑赢垃圾收集器?

c# - 以编程方式将文本粘贴到另一个应用程序的文本框中

wpf - 将 FrameworkElement 及其 DataContext 保存到图像文件没有成功

c# - 带 C# 和 WPF 的键盘 Hook ,用于最小化系统托盘应用程序

c# - 是否可以将lambda表达式存储在数组C#中

c# - 保存 Word 文档

c# - 将裸空文字与用户定义的运算符一起使用时奇怪的重载解决方案