WPF-XAML文件显示“值不能为空”错误,但仍可以运行项目

我是WPF的新手,我的MainWindow.xaml文件中有一个错误,似乎并不妨碍构建或运行对我来说很奇怪的项目。

在我的MainWindow.xaml中,我有:

<Window x:Class="MyP"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyProject"
        xmlns:controls="clr-namespace:MyProject.View"
        mc:Ignorable="d"
        Title="MyProject" Height="800" Width="1200">

    <Grid>
        <controls:LoginView></controls:LoginView>
        <controls:Screen1></controls:Screen1>
    </Grid>
</Window>

<controls:LoginView></controls:LoginView>行显示为绿色的波浪线,将鼠标悬停在该行上时显示:“值不能为空。参数名称:uriString”

我的LoginView.xaml

<UserControl x:Class="MyProject.View.LoginView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyProject.View"
             mc:Ignorable="d" 
             d:DesignHeight="800" d:DesignWidth="1200">

    <Grid>...</Grid>
</UserControl

LoginView.xaml.cs

public partial class LoginView : UserControl
{
    public LoginView()
    {
        InitializeComponent();
        DataContext = new LoginViewModel();
    }
}

有人知道此错误指的是什么以及如何解决该错误吗?

namoral 回答:WPF-XAML文件显示“值不能为空”错误,但仍可以运行项目

当您在用户控件的构造函数中放置一些代码,并且如果您在设计时通过DI注入参数或进行远程调用等时,会引发异常,则会发生此问题。

if(DesignerProperties.GetIsInDesignMode(this))
    return;
// put the rest of your code here
本文链接:https://www.f2er.com/3106992.html

大家都在问