0

I have been working on creating a control resource library for a reusable dialog message box and have run into some issues. Below I have just the bar bones code behind and ViewModel

namespace MyLibrary
{
    public partial class WindowsMessage : UserControl
    {
        public WindowsMessage(Window parentWindow)
        {
            InitializeComponent();
            // Code behind stuff
        }
    }

    public class WindowsMessageViewModel : DialogBaseWindowViewModel
    {
        // ViewModel stuff
    }
}

I also have a ResourceDictionary that must be referenced by the using application that has the following:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MyLibrary">

    <!-- Sets Datatype for control used as content in DialogBaseWindow. -->
    <DataTemplate DataType="{x:Type local:WindowsMessageViewModel}">
        <local:WindowsMessage/>
    </DataTemplate>

</ResourceDictionary>

This all works when the code is part of an application, but I get an error message that states:

"The type "WindowsMessage" does not include any accessible constructors."

Is there a way to get this working for Control Libraries?

David Bentley
  • 824
  • 1
  • 8
  • 27
  • This doesn't have anything to do with libraries. Anything you create in XAML as an XML element has to have a parameterless constructor. Add a parameterless constructor. – 15ee8f99-57ff-4f92-890c-b56153 Jun 28 '19 at 19:09
  • Ahhhh...I did add a parameter when I converted it to the library. It was something needed to work in the way I wanted it to work. Guess I need to figure that out now. Is there a way to do this? – David Bentley Jun 28 '19 at 19:21
  • How were you planning to set the parent window for the instance created in the DataTemplate? Personally I'd write a method using `VisualTreeHelper.GetParent()` to walk the parent chain until you hit a `Window` (or relativesource ancestortype=window, derp), and call that in the constructor to have the control find its own parent window automatically. – 15ee8f99-57ff-4f92-890c-b56153 Jun 28 '19 at 19:24
  • Create a bindable DependencyProperty for the window parameter (or let some code-behind in your UserControl figure out its parent window whenever an instance of your UserControl is being added/removed from the logical tree, assuming there is a related method or event allowing you to do that)... –  Jun 28 '19 at 19:25
  • Well, I wanted to be able to plug in a window parameter so when using this I can choose the window a specific dialog window pops up on. I wanted to choose the parent and not have one auto chosen for me. – David Bentley Jun 28 '19 at 19:28
  • 1
    Well, ignoring your `WindowsMessage` code and addressing your use case: How would you even specify the desired window there in the DataTemplate? Say like `` –  Jun 28 '19 at 19:31
  • @DavidBentley Ah, I see. Can you go into more detail about who's responsible for showing these dialogs, and how that works? – 15ee8f99-57ff-4f92-890c-b56153 Jun 28 '19 at 19:31
  • I have a window with this: ``. I then assign the data context for that window and plug in the user control. The user control constructor is not even really used. – David Bentley Jun 28 '19 at 19:50
  • Yes, I made an edit on my answer as it does contain an `InitializeComponent();` call. – David Bentley Jun 28 '19 at 19:56
  • Well, if you don't want to explain, you don't. Good luck. – 15ee8f99-57ff-4f92-890c-b56153 Jun 28 '19 at 19:56
  • https://github.com/pvpxan/MVVMTemplate is where you can find an older version of my code. Quite a bit is being converted to a library. My specific dialog code uses DialogBaseWindow, WindowsMessage, and DialogService. Probably faster to look that over. – David Bentley Jun 28 '19 at 20:04
  • @EdPlunkett There is quite a bit to explain it might be easier to just look at my code. – David Bentley Jun 28 '19 at 20:11
  • Yeah, so I found a more elegant way of handling a Window parameter. Part of the dialog generation uses a helper class and I added another field to that for Window. Should have done it that way from the beginning. – David Bentley Jun 28 '19 at 20:37

0 Answers0