2012-08-22 62 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