无法找到 ViewModel 的视图,即使 SelectedAssembly 被覆盖
Cannot find view for ViewModel, even though SelectedAssembly is overridden
出于某种原因,我收到 Cannot find view for CaliburnPractice.ViewModel
错误。为此,我查看了此处的一些答案,但其中 none 对我有用。我已将视图和视图模型放入单独的命名文件夹中,并且已经覆盖了 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()
};
}
}
}
因为它正在寻找视图,您的视图模型名为 ViewModel...您列出的视图是 PracticeView...
ShellView -> ShellViewModel
MainView -> MainViewModel 等....
因此 PracticeView -> PracticeViewModel 将得到解析....
SelectedAssembly用于不同DLLviews/viewmodels的情况
除非您没有提供我们可能需要知道的任何信息...
出于某种原因,我收到 Cannot find view for CaliburnPractice.ViewModel
错误。为此,我查看了此处的一些答案,但其中 none 对我有用。我已将视图和视图模型放入单独的命名文件夹中,并且已经覆盖了 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()
};
}
}
}
因为它正在寻找视图,您的视图模型名为 ViewModel...您列出的视图是 PracticeView...
ShellView -> ShellViewModel
MainView -> MainViewModel 等....
因此 PracticeView -> PracticeViewModel 将得到解析....
SelectedAssembly用于不同DLLviews/viewmodels的情况
除非您没有提供我们可能需要知道的任何信息...