简体   繁体   English

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

[英]How I can disable not relevant xaml warning

On following XAML code I get warning that some file is missing. 在遵循XAML代码时,我警告某些文件丢失。

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

The warning comes based on this code: 该警告基于以下代码:

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

Indeed this file is missing but in run time it appears in the right location. 确实缺少此文件,但在运行时它将显示在正确的位置。 The question is how i disable this warning ? 问题是我如何禁用此警告? and why I see something like this in xaml in a first place ? 为什么我首先在​​xaml中看到这样的东西?

Thanks. 谢谢。

I guess whatever you are doing happens in the Constructor of your ViewModelLocator , so you should either move this code somewhere else or (since you are probably using MVVM Light) you can just check if you are in design mode and just return from the constructor or don't do the file check (because maybe you need to initialize other things for your design mode). 我想您在做什么都发生在ViewModelLocator的Constructor中,因此您应该将此代码移到其他位置,或者(因为您可能正在使用MVVM Light),您可以仅检查自己是否处于设计模式下并从该构造方法中返回即可。不要进行文件检查(因为可能需要为设计模式初始化其他内容)。

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

    ...
}

To answer your question why this happens in the first place, because the code runs at a different place than your build folder just to give you a "preview". 首先回答为什么会发生这种情况,因为代码在与build文件夹不同的位置运行,只是为了给您一个“预览”。

Design time also has a very limited feature set, for example to my knowledge you can't use threading in anything that gets executed at design time, so maybe file IO also is not working, but even if it where it wouldn't be able to find your file anyways. 设计时也具有非常有限的功能集,例如,据我所知,您不能在设计时执行的任何事情中使用线程,因此也许文件IO也无法正常工作,但是即使在无法执行的情况下仍然可以找到您的文件。

That is probably because you are trying to get/load/use that "missing" file in your ViewModelLocator contructor or any other method that could be called while attaching it in XAML. 那可能是因为您试图获取/加载/使用ViewModelLocator构造器中的“丢失”文件或在XAML中附加该文件时可能调用的任何其他方法。 Maybe you need to check the exception breaking... It should break in c# code not in xaml. 也许您需要检查异常中断...它应该在c#代码而不是xaml中中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM