【问题标题】:How to use App.Xaml's ResourseDictionaries with own entry point如何将 App.Xaml ResourceDictionary 与自己的入口点一起使用
【发布时间】:2011-03-01 04:15:23
【问题描述】:

我为单实例应用程序创建了一些逻辑,我必须为应用程序使用我自己的入口点(不是 App.xaml)。我在 App.xaml 中有一些现在不起作用的样式。在我的情况下,如何将 App.xaml 中的 ResourceDictionaries 用于整个项目?

我的应用程序启动类管理

 public class SingleInstanceManager : WindowsFormsApplicationBase
{
    App app;

    public SingleInstanceManager()
    {
        this.IsSingleInstance = true;
    }

    protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
    {
        try
        {
            // First time app is launched
            app = new App();
            App.Current.Properties["rcID"] = e.CommandLine;
            //IntroLibrary.OpenDocumentFromNotify();
            app.Run();
            return false;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
    }

    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
    {
        // Subsequent launches
        base.OnStartupNextInstance(eventArgs);
        Intro win = (Intro)app.MainWindow;
        if (eventArgs != null)
        {
            App.Current.Properties["rcID"] = eventArgs.CommandLine[0];
        }
        IntroLibrary.OpenDocumentFromNotify();
        app.Activate();
    }
}

还有我自己的入口点:

    public class EntryPoint
{
    [STAThread]
    public static void Main(string[] args)
    {
        SingleInstanceManager manager = new SingleInstanceManager();
        manager.Run(args);
    }
}

后面还有我的 App.Xaml 代码:

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

        // Create and show the application's main window
        Intro window = new Intro();
        window.Show();
    }

    public void Activate()
    {
        // Reactivate application's main window
        this.MainWindow.Activate();
    }
}

而且我的 App.xaml 有一些代码描述 ResourceDictionaries 不起作用。为什么?

【问题讨论】:

    标签: c# wpf app.xaml


    【解决方案1】:

    我找到了解决这个问题的方法。我创建了新的资源字典并将我所有的其他资源粘贴到这个新字典中:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="themes/ShinyBlue.xaml" />
        <ResourceDictionary Source="themes/Anim.xaml" />
        <ResourceDictionary Source="themes/Generic.xaml" />
        <ResourceDictionary Source="themes/CustomStyles.xaml" />
        <ResourceDictionary Source="themes/Resource.xaml" />
        <ResourceDictionary Source="themes/CustomWindowChrome.xaml" />
     </ResourceDictionary.MergedDictionaries>
    

    然后只是编辑了一点我的 App.cs

       public partial class App : Application
    {
    
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);
            ResourceDictionary rd = new ResourceDictionary() { Source = new Uri("CommonStyle.xaml",UriKind.RelativeOrAbsolute) };
            this.Resources = rd;
            // Create and show the application's main window
            Intro window = new Intro();
            window.Show();
        }
    
        public void Activate()
        {
            // Reactivate application's main window
            this.MainWindow.Activate();
        }
    }
    

    我希望这个解决方案可以帮助某人)))

    【讨论】:

    • 如果想让IDE在编辑器中使用此处定义的样式,可以在App.xaml中添加对主资源字典的引用。但是,应用程序仍将使用代码隐藏进行分配
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多