C# Program can't find XAML page in DLL

Will Pittenger 311 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?

Developer technologies | Windows Presentation Foundation
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | XAML
Developer technologies | C#
{count} vote

1 answer

Sort by: Most helpful
  1. Anonymous
    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.