Ошибка:XDG0003 - Ссылка на объект не указывает на экземпляр объекта

Рейтинг: 0Ответов: 1Опубликовано: 18.07.2023

При написании кода на - XAML, появляется ошибка, следующего вида:

Код - XDG0003. Ошибка: Ссылка на объект не указывает на экземпляр объекта. Строка - 19.

При запуске программы в редакторе VisualStudio 2022, ошибка исчезает (сборка завершена успешна), ничего не появляется, завершает программа и снова показывает ошибку.

Программа в создана в проекте - Приложение WPF (NET.Framework 4.8)

введите сюда описание изображения

Код - MainWindow.xaml:

<Window
x:Class="Debris_Cleaner.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Debris_Cleaner"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vml="clr-namespace:Debris_Cleaner.MVVM.ViewModel"
Title="MainWindow"
Width="800"
Height="450"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="CanResize"
WindowStyle="None"
mc:Ignorable="d">

<Window.DataContext>
    <vml:MainViewModel />
</Window.DataContext>

<DockPanel Margin="7" Background="#1E1E1E">
    <Border
        Height="28"
        Background="#252525"
        DockPanel.Dock="Top">

        <Border.InputBindings>
            <MouseBinding Command="{Binding MoveWindowCommand}" MouseAction="LeftClick" />
        </Border.InputBindings>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="100" />
            </Grid.ColumnDefinitions>

            <TextBlock
                Margin="100,0,0,0"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontFamily="Consolas"
                Foreground="LightGray"
                Text="Derbis Cleaner 1.0" />
            <StackPanel
                Grid.Column="1"
                Margin="0,0,4,0"
                HorizontalAlignment="Right"
                Orientation="Horizontal">
                <Button Command="{Binding MinimizewWindowCommand}" Content="▂" />
                <Button Command="{Binding MaximizeWindowCommand}" Content="🔲" />
                <Button Command="{Binding ShutdownWindowCommand}" Content="✖️" />
            </StackPanel>
        </Grid>
    </Border>

    <Border
        Width="48"
        HorizontalAlignment="Left"
        Background="#252525">
        <Border.Style>
            <Style>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Border.MouseEnter">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetProperty="(Border.Width)"
                                        To="120"
                                        Duration="0:0:.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>

                    <EventTrigger RoutedEvent="Border.MouseLeave">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetProperty="(Border.Width)"
                                        To="48"
                                        Duration="0:0:.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>

                </Style.Triggers>
            </Style>
        </Border.Style>

        <StackPanel>
            <RadioButton
                Content="Очистка"
                FontSize="16"
                FontWeight="Normal"
                Foreground="LightGray" />

            <RadioButton
                Content="Очистка"
                FontSize="16"
                FontWeight="Normal"
                Foreground="LightGray" />

            <RadioButton
                Content="Очистка"
                FontSize="16"
                FontWeight="Normal"
                Foreground="LightGray" />
        </StackPanel>

    </Border>

    <ContentPresenter />

</DockPanel>

Код - MainViewModel.cs:

using Debris_Cleaner.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Debris_Cleaner.MVVM.ViewModel
{
    internal class MainViewModel : ObservalObject
    {

    /* Команды */

    public RelayCommand MoveWindowCommand { get; set; }
    public RelayCommand ShutdownWindowCommand { get; set; }
    public RelayCommand MaximizeWindowCommand { get; set; }
    public RelayCommand MinimizewWindowCommand { get; set; }

    private object _currentView;

    public object CurrentView
    {
        get { return _currentView; }
        set
        {
            _currentView = value;
            OnPropertyChanged();
        }
    }

    public MainViewModel()
    {
        Application.Current.MainWindow.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;

        MoveWindowCommand = new RelayCommand(o => { Application.Current.MainWindow.DragMove(); });
        ShutdownWindowCommand = new RelayCommand(o => { Application.Current.Shutdown(); });
        MaximizeWindowCommand = new RelayCommand(o =>
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
                Application.Current.MainWindow.WindowState = WindowState.Normal;
            else
                Application.Current.MainWindow.WindowState = WindowState.Maximized;
        });
        MinimizewWindowCommand = new RelayCommand(o => { Application.Current.MainWindow.WindowState = WindowState.Minimized; });
        }
    }
}

Ответы

▲ 0

У меня возникла такая же ошибка. Методом проб и комментариев, выяснил, что использование Application в конструкторе вызывает эту ошибку.