Ali Yousefi
Ali Yousefi

Reputation: 2755

WPF - using main method and load resources in runtime xaml viewer not work?

I'm using Single Application class in my project, i want to create custom Main Method in my application,but i have a problem: I cannot view *.xaml files in my project (page and user controls in visual studio) i get this warning not style and templates working (because my resource file is loading runtime) : warning in all xaml files: The resource "name" could not be resolved.

my custom app.cs file Code:

public partial class App : Application, ISingleInstanceApp
{
    private const string Unique = "Agrin Download Manager";

    [STAThread]
    public static void Main()
    {
        if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
        {
            var application = new App();
            application.InitializeComponent();
            application.Run();

            // Allow single instance code to perform cleanup operations
            SingleInstance<App>.Cleanup();
        }
    }

    private bool _contentLoaded;

    /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public void InitializeComponent()
    {
        if (_contentLoaded)
        {
            return;
        }
        _contentLoaded = true;

        this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
        Assembly a = Assembly.GetExecutingAssembly();
        using (Stream stream = a.GetManifestResourceStream("Agrin.Windows.UI.AppResources.xaml"))
        {
            XmlReader XmlRead = XmlReader.Create(stream);
            Application.Current.Resources =
                (ResourceDictionary)XamlReader.Load(XmlRead);
            XmlRead.Close();
        }
    }
}

and my AppResources.xaml (load runtime from InitializeComponent method):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Resources/Themes/ControlStyles.xaml"/>
    <ResourceDictionary Source="Resources/Themes/VectorStyles.xaml"/>
    <ResourceDictionary Source="Resources/Themes/Brushes.xaml"/>
    <ResourceDictionary Source="Resources/Themes/LanguageAndFonts.xaml"/>
    <ResourceDictionary Source="Resources/Themes/ColorsResource.xaml"/>
    <ResourceDictionary Source="Resources/Themes/WpfDataGridTheme.xaml"/>
    <!--<ResourceDictionary Source="Theme/PathToBitmapResources.xaml"/>-->
</ResourceDictionary.MergedDictionaries>

please help me to use xaml viewer in visual studio, that AppResources.xaml build action is "Embedded Resource" and load runtime .

Upvotes: 1

Views: 756

Answers (1)

Ali Yousefi
Ali Yousefi

Reputation: 2755

OK, I changed my project AppResources.xaml file to this and works:

<Application 
x:Class="Agrin.Windows.UI.App2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Converters.xaml"/>
            <ResourceDictionary Source="Resources/Themes/ControlStyles.xaml"/>
            <ResourceDictionary Source="Resources/Themes/VectorStyles.xaml"/>
            <ResourceDictionary Source="Resources/Themes/Brushes.xaml"/>
            <ResourceDictionary Source="Resources/Themes/LanguageAndFonts.xaml"/>
            <ResourceDictionary Source="Resources/Themes/ColorsResource.xaml"/>
            <ResourceDictionary Source="Resources/Themes/WpfDataGridTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Upvotes: 0

Related Questions