开发者

Converting WPF application to UserControl for use in WinForms application via ElementHost

I'm trying to convert this WPF application to WPF UserControl so I could use it in WinForms application via ElementHost. I'm new to WPF and have never even touched it prior to this attempt so I might be going about it completely the wrong way.

I got the UserControl project to compile, however, my StaticResources get underlined in VS with message "The resource 'x' could not be resolved". I've tried moving the xamls with the needed x:Key elements up one level (the same level as the UserControl Dijagram.xaml), but it still cannot resolve them.

In the WinForms app, when trying to add the UserControl through designer by selecting hosted content in ElementHost tasks I get the following error:

An error occured trying to create an object of type 'DijagramLC.Dijagram'. Make sure the type has a default constructor.

(even though default constructor exists).

However, if I add it in codebehind, like this:

wpfUserControl = new Dijagram();
elementHost1.Chil开发者_如何学Pythond = wpfUserControl;
Controls.Add(elementHost1);

the code compiles but throws this runtime error: "System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '20' and line position '4'. ---> System.Exception: Cannot find resource named 'MyToolbar'. Resource names are case sensitive."

I've tried with and without App.xaml and adding ResourceDictionary elements for problematic xamls, but basically, I have no idea what I'm doing :)

I've uploaded the code to my SkyDrive and would really appreciate it if someone could take a look and tell me what I'm doing wrong: https://skydrive.live.com/redir.aspx?cid=21be1f8e850e85cc&resid=21BE1F8E850E85CC!353

I hate jumping in blindly to new techonoly like this, but I have had no choice this time, and need to know if my requirement is even achievable this way.


You are probably getting the error because you use resources from a resource dictionary which is not loaded. Loading a resource dictionary in the hosting application will probably solve the issue:

// When hosting a WPF usercontrol inside an element host, 
// application resources are not loaded, so we need to load them manually.
var resources = new ResourceDictionary
{
    Source = new Uri("/UNIT4.MKB.GUI.XAML.Dashboard.Resources;component/resources.xaml", UriKind.Relative)
};


// Check for null reference
if (Application.Current != null)
{
    //Merge the loaded ResourceDictornairy with the dummy application Resources.
    Application.Current.Resources.MergedDictionaries.Add(resources);
}


The problem is, you need THE default constructor like this:

public CreatedPollsUC()
{
    InitializeComponent();
}

If you have any other code in the constructor, the error occures:

public CreatedPollsUC()
{
    InitializeComponent();
    // ... more code
}

So if you want to use further code in the constructor, you need to apply the control first to the element host. Then you can edit the constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜