iNKORE-NET / UI.WPF.Modern

Modern (Fluent 2) styles and controls for your WPF applications
https://docs.inkore.net/ui-wpf-modern
GNU Lesser General Public License v2.1
326 stars 29 forks source link

initialize library for case when App.xaml is absent #25

Closed ghost1372 closed 7 months ago

ghost1372 commented 7 months ago

There is some type of application which does not have any App.xaml. in this kind of app we can not use this lib because we can not load resources. we only have Window.xaml file and we should add resources in this file. i tried a lot of ways to doing this but all of them failed. there is a lib like this here https://github.com/lepoco/wpfui i could finaly use this lib for my porpuse like this:

Application.Current.Resources.MergedDictionaries.Add(new ThemesDictionary { Theme = ApplicationTheme.Dark });
Application.Current.Resources.MergedDictionaries.Add(new ControlsDictionary());

i tried something like this for your library and failed. there is some issues in mentioned repo which tell us this issue come from where Application.Current https://github.com/lepoco/wpfui/issues/837 https://github.com/lepoco/wpfui/issues/662 https://github.com/lepoco/wpfui/issues/668

NotYoojun commented 7 months ago

Okay i would investigate on this later. I think it's always better to have an Application instance in the app. If you need to custom the entry, you can always do something like this:

Program.cs

    public static class Program
    {
        public static App App_Main;
        public static AnnotateWindow? Window_Annotate;

        [STAThread]
        public static void Main(string[] args)
        {
            App_Main = new App();
            App_Main.Run();
        }

App.xaml

<Application x:Class="Inkways.Annotator.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Inkways.Annotator"
             Startup="Application_Startup" ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Inkways.Engine;component/Themes/AppResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //TODO: Do your own things here

            Program.Window_Annotate = new AnnotateWindow();
            this.MainWindow = Program.Window_Annotate;
            Program.Window_Annotate.Show();
        }
    }
}
ghost1372 commented 7 months ago

Okay i would investigate on this later. I think it's always better to have an Application instance in the app. If you need to custom the entry, you can always do something like this:

Program.cs

    public static class Program
    {
        public static App App_Main;
        public static AnnotateWindow? Window_Annotate;

        [STAThread]
        public static void Main(string[] args)
        {
            App_Main = new App();
            App_Main.Run();
        }

App.xaml

<Application x:Class="Inkways.Annotator.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Inkways.Annotator"
             Startup="Application_Startup" ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Inkways.Engine;component/Themes/AppResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //TODO: Do your own things here

            Program.Window_Annotate = new AnnotateWindow();
            this.MainWindow = Program.Window_Annotate;
            Program.Window_Annotate.Show();
        }
    }
}

tnx but in my case there is no Program.cs or App

NotYoojun commented 7 months ago

Hi, I have made a commit for UIApplication, can you help me test it out? Thanks

ghost1372 commented 7 months ago

Hi, I have made a commit for UIApplication, can you help me test it out? Thanks

Thank you, i will test it soon

ghost1372 commented 7 months ago

@NotYoojun Thank you issue now has been Fixed🙏👌 let me try more to see if there is other issues or not also i should mention that using following code is not enough and does not work, and we should add resources from code-behind

 <Window.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ui:ThemeResources />
             <ui:XamlControlsResources />
         </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Window.Resources>
public MainWindow()
{
    Application.Current.Resources.MergedDictionaries.Add(new ThemeResources());
    Application.Current.Resources.MergedDictionaries.Add(new XamlControlsResources());

    InitializeComponent();
}
NotYoojun commented 7 months ago

Can you send me your project? I'm available to test it now.

NotYoojun commented 7 months ago

I've made another commit 7f405d130202a2ca57419677963da8a41c2d9f9b, can you try that?

ghost1372 commented 7 months ago

Can you send me your project? I'm available to test it now.

WpfApp2.zip

ghost1372 commented 7 months ago

I've made another commit 7f405d1, can you try that?

yes, please wait

ghost1372 commented 7 months ago

I've made another commit 7f405d1, can you try that?

we should still using Code-Behind and Xaml both for adding resources image

NotYoojun commented 7 months ago

Damn it, no matter how i try, the App.xaml-less project just don't work on my computer anyway. I have no idea how to do this, sorry :(

ghost1372 commented 7 months ago

Damn it, no matter how i try, the App.xaml-less project just don't work on my computer anyway. I have no idea how to do this, sorry :(

using both code-behind and xaml is fine for me and is working, does this work for you? you should add code-behind before initialize... code. if this is working for you i think we can leave this here and we can close this issue.

NotYoojun commented 7 months ago

This doesn'tw work for me. But it's okay, I never code a program with App.xaml anyways. So i guess we can close this issue now.

ghost1372 commented 7 months ago

can you upload a nuget package?

NotYoojun commented 7 months ago

Sure but pls wait for me to finish dinner first.

ghost1372 commented 7 months ago

Sure, take your time. Enjoy your dinner. 😊