1

加载设计器时出现未处理的异常(VS2010)

这些是细节。

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)

我努力了

--注释掉所有文本框

--注释掉我的视图模型构造函数中的所有代码

-- 在 MainWindow 构造函数中注释掉 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>();
    }

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

主窗口Xaml...

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

4

1 回答 1

1

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

如果您发现自己的原因有所不同,请告诉我们。

我也在使用 VS 2010。

于 2012-09-26T17:36:21.987 回答