我在我的 WPF 应用程序中使用自定义控件。此自定义控件包括逻辑和资源字典。在我的应用程序中,我有登录窗口和主窗口。应用程序首先加载登录窗口
<Application x:Class="Dest.App"
....
StartupUri="Login.xaml">
</Application>
我在主窗口中使用的自定义控件
<time:Calendar CloseAppointment="OnDeleteAppointment" AddAppointment="OnAddAppointment" Appointments="{Binding Path=Model}" />
同样在 MainWindow 我从程序集中加载资源字典
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfCalendar;component/Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Visual Studio 加载自定义控件没有任何异常。但是当我加载应用程序并成功登录时,登录视图的视图模型会调用主窗口
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
主窗口的构造函数
public MainWindow()
{
InitializeComponent();
m_MainViewModel = new MainViewModel();
this.DataContext = m_MainViewModel;
}
我收到一个例外
异常详情
在 D:\development\Dental Soft\LotusScheduler\WpfScheduler\WpfCalendar\Logic\Calendar.cs: WpfCalendar.Logic.Calendar.OnAppointmentsChanged(DependencyPropertyChangedEventArgs e) 中的 D:\development 中的 WpfCalendar.Logic.Calendar.FilterAppointments() 中的第 122 行\Dental Soft\LotusScheduler\WpfScheduler\WpfCalendar\Logic\Calendar.cs:D:\development\Dental Soft\LotusScheduler\WpfScheduler\WpfCalendar\Logic\ 中 WpfCalendar.Logic.Calendar.OnAppointmentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 的第 109 行Calendar.cs:System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 处 System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 处 System.Windows.DependencyObject 处的第 100 行。NotifyPropertyChange(DependencyPropertyChangedEventArgs args) 在 System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata 元数据, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) 在 System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue) at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange) 在 System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) 在 System.Windows.Data.BindingExpression.Activate(Object item) 在 System System.Windows.Data 处的 .Windows.Data.BindingExpression.AttachToContext(AttachAttempt 尝试)。BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Run(Object arg) 在 MS.Internal。 Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e) 在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() 在 System.Windows.ContextLayoutManager.UpdateLayout() 在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) 在 System.Windows.Media .MediaContext.InvokeOnRenderCallback.DoWork() 在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) 在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) 在 System .Windows.媒体。MediaContext.Resize(ICompositionTarget resizedCompositionTarget) 在 System.Windows.Interop.HwndTarget.OnResize() 在 System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Interop.HwndSource.HwndTargetFilterMessage( IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&handled) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&handled) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o ) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 在系统。 Windows.Threading.Dispatcher。MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 , IntPtr wParam, IntPtr lParam) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, Int32 x, Int32 y, Int32 cx,Int32 cy,Int32 标志)在 System.Windows.Window.SetupInitialState(双请求顶部,双请求左,双请求宽度,双请求高度)在 System.Windows.Window.CreateSourceWindow(布尔 duringShow)在 System.Windows.Window.CreateSourceWindowDuringShow() 在 System.Windows.Window.SafeCreateWindowDuringShow() 在 System.Windows.Window.ShowHelper( Object booleanBox) at System.Windows.Window.Show() at ViewModel.LoginingViewModel.Login(Object parameter) in D:\development\Dental Soft\Lotus\AMS\Dentist\ViewModel\LoginingViewModel.cs:ViewModel.LoginingViewModel 的第 162 行D:\development\Dental Soft\Lotus\AMS\Dentist\ViewModel\LoginingViewModel.cs 中的 .b__0(对象参数):D:\development\Dental Soft\Lotus\ 中 ViewModel.CommonCommand.Execute(对象参数)的第 115 行AMS\Dentist\Commands\CommonCommand.cs:MS.Internal.Commands.CommandHelpers 的第 59 行。CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)在 System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 在 System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 在 System. Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 在 System.Windows.UIElement。ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) 在 System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 在 System.Windows.RoutedEventArgs。 InvokeHandler(委托处理程序,对象目标)在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl( System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trust) 在 System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) 在 System.Windows.Input 的 DependencyObject 发送方,RoutedEventArgs args)。InputManager.ProcessStagingArea() 在 System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入) 在 System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) 在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode 模式, Int32 时间戳,RawMouseActions 操作,Int32 x,Int32 y,Int32 轮)在 System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd,WindowMessage msg,IntPtr wParam,IntPtr lParam,Boolean& 处理)在 System.Windows.Interop.HwndSource。 InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理)在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理)在 MS.Win32.HwndSubclass.DispatcherCallbackOperation( System.Windows.Threading 中的对象 o)。ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl( DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在系统.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(对象忽略)在 System.Windows.Application。RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at Dentist.App.Main() in D:\development\Dental Soft\Lotus\AMS\Dentist\ obj\x86\Debug\App.g.cs:Microsoft 的 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args) 处的第 0 行 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)。 VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run (ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx)在 System.System.Threading.ThreadHelper.ThreadStart() 处的 Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
带有问题控件的资源字典的一部分
<Border BorderBrush="#A5BFE1" BorderThickness="0,1,1,1" Background="White" Grid.Column="1" Grid.Row="0" >
<TextBlock x:Name="DayHeader" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock FontWeight="Bold" Text="{x:Static Properties:Resources.CalendarSelectedDayTitle}" />
<TextBlock Text="{x:Static Properties:Resources.CalendarSelectedDayDescription}" />
</StackPanel>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</Border>
<time:Scroll x:Name="scrollViewer" CustomVerticalOffset="640.0" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<time:TimeRibbon Grid.Column="0"></time:TimeRibbon>
<time:SchedulerDay Grid.Column="1" x:Name="Day"></time:SchedulerDay>
</Grid>
</time:Scroll>
Calendar 控件的构造函数(抛出异常)
public Calendar()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Calendar), new FrameworkPropertyMetadata(typeof(Calendar)));
CommandManager.RegisterClassCommandBinding(typeof(Calendar),
new CommandBinding(NextDay, new ExecutedRoutedEventHandler(OnExecutedNextDay),
new CanExecuteRoutedEventHandler(OnCanExecuteNextDay)));
CommandManager.RegisterClassCommandBinding(typeof(Calendar),
new CommandBinding(PreviousDay, new ExecutedRoutedEventHandler(OnExecutedPreviousDay),
new CanExecuteRoutedEventHandler(OnCanExecutePreviousDay)));
}
但是,当在 app.xaml 中设置第一个窗口 MainWindow,而不是 LoginWindow 时,一切正常,没有任何异常!
请告知问题的原因可能是什么?
谢谢