[Grundlagen] Application.xaml während Runtime verändern

  • WPF

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Artentus.

    [Grundlagen] Application.xaml während Runtime verändern

    Hallo Com,

    ich wollte fragen ob man den Code aus der Application während der Runtime durch zum Beispiel einen Button-Klick verändern kann.
    Mein Code:

    XML-Quellcode

    1. <Application x:Class="Application"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. StartupUri="MainWindow.xaml">
    5. <Application.Resources>
    6. <ResourceDictionary>
    7. <ResourceDictionary.MergedDictionaries>
    8. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
    9. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    10. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    11. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Green.xaml" />
    12. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    13. </ResourceDictionary.MergedDictionaries>
    14. </ResourceDictionary>
    15. </Application.Resources>
    16. </Application>


    Das

    XML-Quellcode

    1. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Green.xaml" />
    soll durch einen Button Klick in

    XML-Quellcode

    1. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml" />
    verändert werden.Geht das wenn ja wie ?

    Gruß iEi
    @iEi
    Wie ich sehe verwendest du MahAppsMetro ;)
    Um das Farbtheme zu verändern, musst du deinem Button folgenden Code hinzufügen:

    VB.NET-Quellcode

    1. Dim dict As New ResourceDictionary
    2. dict.Source = New Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml")
    3. Application.Current.Resources.MergedDictionaries.Add(dict)

    Statt Red kannst du auch noch Blue, Green, Orange oder Purple verwenden. Diese Farben kannst du mit BaseLight oder BaseDark kombinieren. Während der Laufzeit veränderst du das so:

    VB.NET-Quellcode

    1. Dim dict As New ResourceDictionary
    2. dict.Source = New Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml")
    3. Application.Current.Resources.MergedDictionaries.Add(dict)

    BaseDark für das dunkle und BaseLight für das helle Theme (versteht sich glaube ich von selbst ;) )

    Gruß
    Switcherlapp97
    RubiksCubeSolver


    Jetzt im Showroom
    Dein Grundansatz ist schon falsch. Die XAML-Dateien werden nicht etwa als Textressource in der Anwendung gespeichert, sie sind genauso Codedateien, wie auch die VB-Dateien, sie werden also kompiliert. Und im Quelltext kannst du nichts während der Ausführung verändern, oder manipulierst du deinen VB-Code auch immer?