C# Program can't find XAML page in DLL

Will Pittenger 306 Reputation points
2024-07-31T16:13:28.4066667+00:00

I have a program with a window declared as StartWnd (in the main assembly) derived from a class, WzdBase in a DLL which in turn is derived from System.Windows.Window. StartWnd doesn't provide its own XAML page. Rather that's in the DLL and provided by WzdBase as WzdBase.xaml. However, I'm getting a runtime exception that it can't find the page in the DLL. The code in question was all generated by the XAML compiler.

Does this happen much? What can I do about it?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,768 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,902 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
808 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,583 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 1,765 Reputation points Microsoft Vendor
    2024-08-05T08:04:17.36+00:00

    Hi,@Will Pittenger.

    Reason: You could enter InitializeComponent by pressing Ctrl+left key to view the following code

    
    System.Uri resourceLocater = new System.Uri("/t;component/base.xaml", System.UriKind.Relative);
    
    

    That is, the program will use the Uri to find the xaml file it should be associated with. If it cannot find the file, it will report the error you see.(The file does not appear to be editable manually).

     

    Solution

    Way 1(Recommend):

    Create a class without xaml to define the window.For example, the following code is just a simple class.

    
        public class newBase:Window
    
        {
    
            public newBase()
    
            {
    
                StackPanel stackPanel = new StackPanel();
    
                stackPanel.Background = Brushes.DarkGray;
    
                Button button =  new Button();
    
                button.Content = "HAHA";
    
                button.Width = 200;
    
                button.Height = 50;
    
                button.HorizontalAlignment = HorizontalAlignment.Center;
    
                stackPanel.Children.Add(button);
    
                this.AddChild(stackPanel);
    
            }
    
        }
    
    

    Then, reference it in the t project and derive it to inherit newBase.(At this time, derive does not need to call InitializeComponent() in the constructor;)

     

    Way 2:

    Copy base.xaml (delete base.xaml.cs) to the same folder as derived.cs

     

    Adjust the code

    
    class derived : g._base
    
    {
    
        public derived()
    
        {
    
            InitializeComponent();
    
        }
    
    }
    
    

     

    In base.xaml (project t) you could continue to define your page


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.