c# - 无法找到 ViewModel 的 View ,即使 SelectedAssembly 被覆盖

标签 c# wpf caliburn.micro

出于某种原因,我收到了 Cannot find view for CaliburnPractice.ViewModel 错误。我已经查看了这里的一些答案,但没有一个对我有用。我已将 View 和 View 模型放入单独的命名文件夹中,并且已经覆盖了 SelectedAssembly 方法。到底是怎么回事?请参阅下面的代码。

ViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;

namespace CaliburnPractice
{
    public class ViewModel : PropertyChangedBase
   {

   }
}

App.xaml

<Application x:Class="CaliburnPractice.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:CaliburnPractice"
         >
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <local:Bootstrapper x:Key="bootstrapper" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>

PracticeView.xaml

<UserControl x:Class="CaliburnPractice.PracticeView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Width="300" Height="300" Background="LightBlue">

</Grid>

Bootstrapper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;
using System.Reflection;

namespace CaliburnPractice
{
    public class Bootstrapper : BootstrapperBase
    {
        public Bootstrapper()
        {
            Initialize();
        }
        protected override void OnStartup(object sender,         System.Windows.StartupEventArgs e)
        {
            base.OnStartup(sender, e);
            DisplayRootViewFor<ViewModel>();
        }
        protected override IEnumerable<System.Reflection.Assembly>     SelectAssemblies()
        {
            return new[]
            {
                Assembly.GetExecutingAssembly()
            };
        }
    }
}

最佳答案

因为它正在寻找 View ,您的 View 模型名为 ViewModel...您列出的 View 是 PracticeView...

ShellView -> ShellViewModel

MainView -> MainViewModel 等......

因此 PracticeView -> PracticeViewModel 将得到解析....

SelectedAssembly 用于不同 DLL 中的单独 View / View 模型的情况

除非您没有提供我们可能需要知道的任何信息...

关于c# - 无法找到 ViewModel 的 View ,即使 SelectedAssembly 被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187768/

相关文章:

c# - selenium webdriver 单击 javascript 链接

wpf - 仅在 WPF MVVM 中的两个 View 之间进行通信

c# - 调用方法的结果到主线程

c# - 如何防止 PropertyChanged 在控件初始化时触发

c# - WPF 简单表格布局

c# - 更改语音识别引擎库的语言

c# - 从字符串中检索类型

c# - 处理 --> Arduino --> Unity 通过端口通信

c# - 如何检测代码中的 ViewModel 绑定(bind)错误?

c# - WinRT 应用程序中的 DRY