wpf 找不到名为“ViewModelLocator”的资源异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19011463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:43:47  来源:igfitidea点击:

Cannot find resource named 'ViewModelLocator' exception

wpfmvvmmvvm-lightviewmodellocator

提问by user2788792

I am trying to use ViewModelLocator by declaring it as a resource in App.xaml. Its a very simple class as follows:

我试图通过在 App.xaml 中将其声明为资源来使用 ViewModelLocator。它是一个非常简单的类,如下所示:

public class ViewModelLocator
    {
        public ShellViewModel ShellPage
        {
            get
            {
                return new ShellViewModel();
            }
        }
}

App.xaml file is as below:

App.xaml 文件如下:

<Application x:Class="SomeNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SomeNamespace.ViewModels">
    <Application.Resources>
         <vm:ViewModelLocator x:Key="ViewModelLocator" />
    </Application.Resources>
</Application>

App.xaml.cs is as below:

App.xaml.cs 如下:

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var view = new ShellView();
            Current.MainWindow = view;
            Current.MainWindow.Show();            
        }
    }

ShellView.xaml is a below:

ShellView.xaml 如下:

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="SomeNamespace.ShellView"
        Title="MainWindow" 
        Height="350" 
        Width="525" 
        ResizeMode="NoResize" 
        MinWidth="700" 
        MinHeight="700"
        DataContext="{Binding ShellPage, Source={StaticResource ViewModelLocator}}"
        >
    <Grid>
        <TextBlock Text="{Binding Title}"></TextBlock>
    </Grid>
</Window>

I can see the correct title in Visual Studio designer but when i run the app, get XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '11' and line position '9'.

我可以在 Visual Studio 设计器中看到正确的标题,但是当我运行应用程序时,得到 XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' 抛出异常。行号“11”和行位置“9”。

The innerexception has {"Cannot find resource named 'ViewModelLocator'. Resource names are case sensitive."}

内部异常具有{“找不到名为 'ViewModelLocator' 的资源。资源名称区分大小写。”}

Am i missing something ?

我错过了什么吗?

回答by mdm20

Try putting it inside a ResourceDictionary

尝试将其放入 ResourceDictionary

<Application.Resources>
    <ResourceDictionary>
        <vm:ViewModelLocator x:Key="ViewModelLocator" />
    </ResourceDictionary>
</Application.Resources>

Edit:

编辑:

I solved the problem by using the Startup event in the App, instead of overriding OnStartup.

我通过在应用程序中使用 Startup 事件而不是覆盖 OnStartup 解决了这个问题。

<Application x:Class="TestWPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:TestWPF.ViewModels"
             Startup="App_Startup">
    <Application.Resources>
          <vm:ViewModelLocator x:Key="ViewModelLocator" />
    </Application.Resources>
</Application>

Code

代码

public partial class App : Application
{
    void App_Startup(object sender, StartupEventArgs e)
    {
        var view = new ShellView();
        Current.MainWindow = view;
        Current.MainWindow.Show();
    }
}

回答by HichemSeeSharp

At startup time you're loading the window, this is not a good practice in my opinion because the resources are not yet entirely resolved in both App in witch your Window depends. Even if you're seeing properties loading at design time this is not always a good argument, though design is following another logic to execute the code.
You have to let App and Windows load and then fill the context property in the Window's grid

在启动时您正在加载窗口,在我看来这不是一个好的做法,因为在您的窗口所依赖的两个应用程序中,资源尚未完全解决。即使您在设计时看到属性加载,这并不总是一个很好的论据,尽管设计遵循另一种逻辑来执行代码。
您必须让 App 和 Windows 加载,然后在 Window 的网格中填充上下文属性


Remove the resource XAML from App.xaml (it looks really out of the scope there) and do this instead :


从 App.xaml 中删除资源 XAML(它看起来真的超出了那里的范围)并改为执行以下操作:

 <Window.Resources>
        <wpfApplication1:ViewModelLocator x:Key="ViewModelLocator" />
    </Window.Resources>

    <Grid   DataContext="{Binding ShellPage, Source={StaticResource ViewModelLocator}}">
        <TextBlock Text="{Binding Title}"></TextBlock>
    </Grid>

回答by startewho

It seems i found a solution.The reason maybe the StartUrl in the App.xaml.

看来我找到了解决方案。原因可能是App.xaml.

1.Remove the default StartUrl in the App.xaml.Such like this:

1.去掉默认的StartUrl中的App.xaml.像这样:

        <Application x:Class="BidingAccessories.App" 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" d1p1:Ignorable="d"
                     xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
            <Application.Resources>
                <ResourceDictionary>
                    <vm:ViewModelLocator x:Key="Locator" xmlns:vm="clr-namespace:BidingAccessories.ViewModel" />
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/BidingCommon;component/CommonStyleDictionary.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </Application.Resources>
        </Application>
        </Application.Resources>
        </Application>

2. override the OnstartUp event in App.xaml.cs

2. 覆盖 OnstartUp 事件 App.xaml.cs

    protected override void OnStartup(StartupEventArgs e)
        {
            StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
    }

回答by sumanth JK

Remove this part of code and run again. I am not sure about what's happening internally but that is what throwing the exception.

删除这部分代码并再次运行。我不确定内部发生了什么,但这就是抛出异常的原因。

enter image description here

在此处输入图片说明