一切正常,直到我添加资源字典。然后看起来像:
App.xaml:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:local="clr-namespace:MyApp.MyApp.Common">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<local:MainMenuButtonVisibilityConverter x:Key="MainMenuButtonVisibilityConverter" /> <!-- withoot this line is fine-->
</ResourceDictionary>
</Application.Resources>
App.g.cs:
namespace MyApp {
...
public static void Main() {
MyApp.App app = new MyApp.App();
app.InitializeComponent(); // <------ here is the problem
app.Run();
}
}
MainMenuButtonVisibilityConverter:
namespace MyApp.MyApp.Common
{
public class MainMenuButtonVisibilityConverter : IValueConverter
{
...
}}
我的错误:
Error 2 'MyApp.MyApp.App' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'MyApp.MyApp.App' could be found (are you missing a using directive or an assembly reference?)
我的项目名称:MyApp
包含ViewModels和Common的文件夹也经过校准:MyApp
添加资源字典时我做错了什么?
App.xaml.cs
namespace MyApp.MyApp
{
public partial class App : Application
{
private Shell shell;
private MyAppMain myAppMain;
public App()
{
}
private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "", MessageBoxButton.OK, MessageBoxImage.Hand);
e.Handled = true;
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
base.Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(this.Dispatcher_UnhandledException);
myAppMain = new MyAppMain("");
}
}}
最佳答案
您是否尝试过更改声明的顺序?首先应在ResourceDictionary中声明转换器。
<ResourceDictionary>
<local:MainMenuButtonVisibilityConverter x:Key="MainMenuButtonVisibilityConverter" /> <!-- withoot this line is fine-->
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
关于c# - MyApp.App不包含添加ResourceDictionary之后的初始化定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31072179/