我如何禁用不相关的 xaml 警告

How I can disable not relevant xaml warning

在执行 XAML 代码后,我收到警告,提示某些文件丢失。

<Application.Resources>
    <!--Global View Model Locator-->
    <viewModel:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>

警告基于此代码:

if (!File.Exists(filename))
{
    throw new FileNotFoundException(string.Format("The specified file {0} is missing.", filename));
}

确实缺少此文件,但在 运行 时间它出现在正确的位置。 问题是我如何禁用此警告?以及为什么我首先在​​ xaml 中看到类似的内容?

谢谢。

这可能是因为您正在尝试 get/load/use 您的 ViewModelLocator 构造函数中的 "missing" 文件或在 XAML 中附加它时可以调用的任何其他方法.也许你需要检查异常中断......它应该在 c# 代码中中断而不是 xaml.

我想无论你在做什么都发生在你的 ViewModelLocator 的构造函数中,所以你应该将这段代码移到其他地方或者(因为你可能正在使用 MVVM Light)你可以检查你是否在设计模式,仅 return 来自构造函数或不进行文件检查(因为您可能需要为设计模式初始化其他内容)。

public class ViewModelLocator
{
    /// <summary>
    /// Initializes a new instance of the ViewModelLocator class.
    /// </summary>
    public ViewModelLocator()
    {
        if (ViewModelBase.IsInDesignModeStatic)
            return;
        ...
    }

    ...
}

回答你的问题,为什么首先会发生这种情况,因为代码运行在与你的构建文件夹不同的地方只是为了给你一个 "preview"。

设计时的功能集也非常有限,例如,据我所知,您不能在设计时执行的任何操作中使用线程,因此文件 IO 也可能不起作用,但即使它在哪里无论如何都找不到你的文件。