简体   繁体   English

您如何声明自己的Main()并导入ResourceDirectory

[英]How do you declare your own Main() and import ResourceDirectory

I need to customise the Main() method in my WPF application, so I have turned off the auto-generation of the main, by changing the Build Action of my App.xaml to 'Page' (instead of 'ApplicationDefinition'). 我需要在WPF应用程序中自定义Main()方法,因此通过将App.xaml的“生成操作”更改为“页面”(而不是“ ApplicationDefinition”)来关闭了自动生成主文件的App.xaml However, I also need to use a ResourceDictionary , and if the App.xaml isn't marked as 'ApplicationDefinition', none of the resources get imported (I tried this, see here ). 但是,我还需要使用ResourceDictionary ,并且如果未将App.xaml标记为'ApplicationDefinition',则不会导入任何资源(我尝试过此操作,请参见此处 )。

I need a custom Main() method because the application is required to be a singleton, to the main simply reads: 我需要一个自定义Main()方法,因为应用程序必须是单例,主要是这样:

[STAThread]
public static void Main()
{
    MainApplication app = MainApplication.Instance;
    app.Run();
}

No how do I automatically import all resources and define my own main at the same time? 否,如何自动导入所有资源并同时定义自己的主要资源?

One solution is to import them programatically, eg like this: 一种解决方案是以编程方式导入它们,例如:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("/MyProject;component/MyResourceDictionary.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(dict);

This solution is not acceptable, though, as I want to be able to give my application to a designer who uses Blend and may know nothing about the background of the programme. 但是,此解决方案是不可接受的,因为我希望能够将我的应用程序提供给使用Blend的设计人员,并且可能对该程序的背景一无所知 So he would not be permitted to create new resource files, which is not ideal. 因此,不允许他创建新的资源文件,这是不理想的。 Any better suggestions? 还有更好的建议吗?

I strongly recommend that you leave the Main() method to the default. 强烈建议您将Main()方法保留为默认值。

what you're trying to achieve here requires no custom code, and even if it did, the Main() method is NOT the right place to put that custom code. 您在此处想要实现的目标不需要自定义代码,即使这样做, Main()方法也不是放置自定义代码的正确位置。

You don't need to create a "singleton" property for the Application object in WPF, because WPF already does that. 您无需在WPF中为Application对象创建“单一”属性,因为WPF已经做到了。

You can access the "singleton" instance of the Application object in WPF via the System.Windows.Application.Current property: 您可以通过System.Windows.Application.Current属性访问WPF中Application对象的“单例”实例:

So that if you put custom properties in the App class, like this: 因此,如果您将自定义属性放在App类中,如下所示:

public partial class App : Application
{
    public string MyString { get; set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MyString = "I'm a string in the App class";
    }
}

Then you can access that in XAML: 然后,您可以在XAML中访问它:

<TextBox Text="{Binding MyString, Source={x:Static Application.Current}}"/>

Or, if you need to access the instance of the MainWindow , then you can do: 或者,如果您需要访问MainWindow的实例,则可以执行以下操作:

<TextBox Text="{Binding Title, 
                        Source={x:Static Application.Current.MainWindow}}"/>

or 要么

<TextBox Text="{Binding DataContext.MyString, 
                        Source={x:Static Application.Current.MainWindow}}"/>

for properties that are in the MainWindow's ViewModel. 用于MainWindow的ViewModel中的属性。

If you had titled your question accurately, something like How to create a singleton WPF application? 如果您已正确地为问题加上标题,则类似如何创建单例WPF应用程序? , then I'm sure that you would have received several replies by now. ,那么我确定您现在会收到几封答复。 However, luckily I read the comments and saw your remark stating that requirement. 但是,幸运的是,我阅读了评论并看到您的评论指出了这一要求。

There are many tutorials online showing how to achieve your actual requirement. 在线上有许多教程显示如何实现您的实际需求。 For example, you could take a look at the C# .NET Single Instance Application page on the Sanity Free Coding website which uses the Mutex class. 例如,您可以看一下Sanity Free Coding网站上使用Mutex类的C#.NET单一实例应用程序页面。 If that one doesn't suit you, just search for others until you find one that does. 如果那个人不适合您,只需搜索其他人,直到找到一个适合您的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM