0

On following XAML code I get warning that some file is missing.

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

Thanks.

2 Answers 2

2

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

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

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.

0

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. Maybe you need to check the exception breaking... It should break in c# code not in xaml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.