2012-08-22 45 views
0

加载设计(VS2010)WPF设计错误(System.NullReferenceException)装载MahApps.Metro.Controls

这些时,我得到一个未处理的异常是详细内容。

System.NullReferenceException 
Object reference not set to an instance of an object. 
at MahApps.Metro.Controls.TextboxHelper.TextBoxLoaded(Object sender, RoutedEventArgs e) 
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 e) 
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent) 
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root) 
at MS.Internal.LoadedOrUnloadedOperation.DoWork() 
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks() 
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) 
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) 
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 

我已经试过

--commenting的所有文本框

在我的视图模型构造

--commenting了所有的代码在主窗口构造--commenting出MainWindowVieWModel创作...

我很困惑..

代码...

public partial class MainWindow : MetroWindow 
{ 
    public MainWindowViewModel ViewModel { get; protected set; } 

    public MainWindow() 
    { 
     ViewModel = new MainWindowViewModel(new AppModel()); 
     InitializeComponent(); 
    } 


public class AppModel : ReactiveValidatedObject 
{ 
    public ReactiveCollection<SheetModel> CompletedSheets { get; protected set; } 

    public void RefreshData() 
    { 
     List<SheetModel> sheets = DataLayer.LoadData(); 

     CompletedSheets = new ReactiveCollection<SheetModel>(); 

     foreach (var sheetModel in sheets) 
     { 
      CompletedSheets.Add(sheetModel); 
     } 
    } 

    public AppModel() 
    { 
     CompletedSheets = new ReactiveCollection<SheetModel>(); 
    } 

构造主窗口视图模型是空的,所以不应该有任何问题出现。

MainWindowXaml ...

<Controls:MetroWindow 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
x:Class="TimeSheet.MainWindow" 
Title="MainWindow" 
x:Name="Window" 
Height="270" 
MinWidth="490" 
Width="600" 
xmlns:local="clr-namespace:TimeSheet" 
ShowTitleBar="False" WindowStartupLocation="CenterScreen" 
DataContext="{Binding ViewModel, ElementName=Window}"> 

<Controls:MetroWindow.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 

     <DataTemplate x:Key="CompletedBlockTile"> 
      <StackPanel Orientation="Vertical" Margin="10,0,0,0"> 
       <TextBlock Text="{Binding Date}" FontWeight="Bold" FontFamily="Segoe UI" /> 
      </StackPanel> 
     </DataTemplate> 

     <DataTemplate x:Key="TaskTemplate"> 
      <Grid HorizontalAlignment="Stretch"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition MinWidth="80px"/> 
        <ColumnDefinition Width="50px" /> 
        <ColumnDefinition Width="40px"/> 
        <ColumnDefinition Width="40px"/> 
       </Grid.ColumnDefinitions> 

       <TextBlock Grid.Column="0" Text="{Binding Description}" Height="25.3" Margin="0" HorizontalAlignment="Stretch"/> 
       <TextBlock Grid.Column="1" Text="{Binding Duration}" Margin="0,0,0,0" Height="25.3" Width="45.625"/> 
       <Button Grid.Column="2" Content="&gt;&gt;" HorizontalAlignment="Right" Width="38" Command="{Binding ReStartCommand}"/> 
       <Button Grid.Column="3" Content="STOP" VerticalAlignment="Top" Margin="0,0,0,0" Command="{Binding StopCommand}" HorizontalAlignment="Right" Width="35"/> 
      </Grid> 
     </DataTemplate> 

     <BitmapImage x:Key="SearchImage" UriSource="Images/blue-37.png" /> 
    </ResourceDictionary> 

</Controls:MetroWindow.Resources> 

<Controls:MetroWindow.WindowCommands> 
    <Controls:WindowCommands> 
     <Button x:Name="thisButton"> 
      <StackPanel Orientation="Horizontal"> 
       <StackPanel Orientation="Horizontal"> 

        <Label Margin="0,0,55,0" Content="09:00"/> 
        <Label Margin="0,0,5,0" Content="Friday 20th July 2012"/> 

        <TextBox Controls:TextboxHelper.Watermark="search" 
           Controls:TextboxHelper.ClearTextButton="True" 
           VerticalAlignment="Top" 
           HorizontalAlignment="Left" 
           Width="154.002" /> 

        <Button Style="{DynamicResource ChromelessButtonStyle}" Click="Button_Click"> 
         <Image Source="{StaticResource SearchImage}" Width="20"/> 
        </Button> 

       </StackPanel> 
      </StackPanel> 
     </Button> 
    </Controls:WindowCommands> 
</Controls:MetroWindow.WindowCommands> 

<DockPanel Margin="0,0,0,0" > 
    <DockPanel DockPanel.Dock="Left" Margin="0,0,0,0" Width="176.625" > 

     <ListBox Background="Transparent" 
       BorderBrush="Transparent" 
       Margin="0,30,0,0" 
       ItemsSource="{Binding CompletedSheets}" 
       ItemTemplate="{DynamicResource CompletedBlockTile}" 
       SelectedItem="{Binding SelectedTimeSheet}"/> 


    </DockPanel> 
    <DockPanel DockPanel.Dock="Right" > 
     <Grid Margin="0,30,0,0" VerticalAlignment="Top" DockPanel.Dock="Top"> 
      <Grid Margin="0,0,14,0.805"> 

       <local:TextBoxChangeExt 
        MinWidth="200" 
         Controls:TextboxHelper.Watermark="Task Name" 
         Controls:TextboxHelper.ClearTextButton="True" 
         d:LayoutOverrides="Height" 
         Margin="8,0,70,0" 
         Background="{x:Null}" 
        Text="{Binding NewTaskName}"> 

       </local:TextBoxChangeExt> 

       <Button HorizontalAlignment="Right" 
         Width="57" 
         d:LayoutOverrides="HorizontalAlignment, Height" 
         Content="Start" 
         Command="{Binding StartCommand}" IsEnabled="{Binding StartNewEnabled}"/> 

      </Grid> 
     </Grid> 
     <Grid DockPanel.Dock="Bottom"> 
      <ListBox Margin="8,8,5,5" 

        BorderBrush="{x:Null}" 
        ItemTemplate="{DynamicResource TaskTemplate}" 
        ItemsSource="{Binding SelectedTimeSheet.Tasks}" 
        HorizontalContentAlignment="Stretch" 
        /> 
     </Grid> 


    </DockPanel> 
</DockPanel> 

+0

您已经定义了一个控制'地方:在可视树TextBoxChangeExt' - 没有什么行为,它包含我不能帮你找出这个问题的根本原因的详细信息。 –

回答

0

我敢肯定这是一个错误。我遇到了同样的错误,并且在跟踪堆栈跟踪到ViewModel中的行时,我发现我正在适当地检查null。一切似乎都在运行时工作,但我认为这是在设计器中使用iCommand的impriment时的一个错误。

让我们知道,如果你发现你的原因是不同的。

我还使用VS 2010

+0

我最近没有玩过它..对你有任何喜悦吗? –

+0

我确实发现我的错误。在我的情况下,在设计时,我的CanExecute中有一个空对象,我试图访问该对象的属性。对我来说,一旦我明白了这一点,修复很明显,只需在检查我感兴趣的属性之前添加一个NullCheck就可以解决此问题(即MyObj!= null && MyObj.MyBoolProp)。 – William