1
Vote

ModernUI Started Externally (Possible bug?)

description

Hello,

I have been frustrated for hours, and cannot find a solution.

I am trying to start up a mui:ModernWindow Class defined in a WPF project, "from a Windows Form Project," by referencing the WPF project from the Windows Forms project.

The reason this being is because I have an ApplicationContext in the Windows Forms project that requires the default project to be Windows Forms, and not WPF.

I can start and run the WPF project successfully when it is set as the startup item. Everything works 100%.

However, whenever I try to reference to the WPF project from the Windows Forms project, by doing the following:
Dim x As New ModernUIExperiment.main
x.show()
Where the WPF mui:ModernWindow is the "main" class.

Doing this brings up the window, as expected. However, when clicking the link that shows the user control, an infinite line of "loading dots" appear.

The guide I have followed is below:
My first Modern UI app

Things I have tried to resolve the problem that did not work:
  • Set the Source Property for the link externally during runtime:
Dim x As New ModernUIExperiment.main
x.ContentSource = New Uri("/ModernUIExperiment;component/stuff.xaml", UriKind.Relative)
x.link.Source = New Uri("/ModernUIExperiment;component/stuff.xaml", UriKind.Relative)
x.Show()
  • Copy exactly as best as the IDE would let me, Reference-to-Reference of the WPF project to the Windows Forms Project
However, I did note that I was able to get this method to work, from a separate WPF project, to WPF project containing the ModernUI using the following:
Dim x As New ModernUIExperiment.main
x.ContentSource = New Uri("/ModernUIExperiment;component/stuff.xaml", UriKind.Relative)
x.link.Source = New Uri("/ModernUIExperiment;component/stuff.xaml", UriKind.Relative)
x.Show()
The same exact code, but it works, 100% as expected.

So I have come to the conclusion that it's something to do with the startup project being WPF created. In my opinion, this whole situation seems like a bug to me.

If there's is anything I am overseeing, or if you have any questions or comments, feel free to ask. Any help is greatly appreciated :).

P. S. I am using Visual Studio 2012 Express, with Visual Basic

EDIT:
I did notice however, if I tried to set the "x.ContentSource" from a Windows Forms project, I get the error: "Object reference not set to an instance of an object." in PresentationFramework.dll. This may be a clue to why this is happening.

Thanks,
  • Jake M.

comments

BlazingArdor wrote Aug 14, 2013 at 6:29 AM

After some more experimenting, I have come to the conclusion that the root cause occurs when setting the ModernWindow property "ContentSource", either through XAML or programatically. While it does technically get set, through some testing:

MsgBox(x.ContentSource.OriginalString)

x.Show()
It does not get a null reference exception in my project's executable (as it would if I did not use the New keyword, so it does get set). However, even after confirming it gets set, and "then" showing the window, the null reference exception still occurs in PresentationFramework.dll.

I can still set the link.Source property only, and everything except the "infinite loading dots" and ContentSource works fine (to my knowledge).

Again, this does not happen in WPF projects, but only Windows Forms projects. I feel like a reference is missing, but I even tried adding all possible references under the "Assemblies" section, and that didn't even work. I get no errors, no warnings, no messages, nothing.

MartinnitraM wrote Jan 23, 2015 at 2:28 PM

Hey Jake,

Did you ever get to the bottom of this? I've got the same issue, been pulling my hair out and googling like a mad thing to no avail.

Ta,

Martin

BlazingArdor wrote Jan 28, 2015 at 3:13 PM

Ah yes, I apologize for the late reply. I don't remember who told me, could have sworn it was on this forum. However, I converted it to full WPF after realizing all of the setbacks.

First of all, all WPF applications require Windows.Application to be set. Since Windows Forms projects do not do this automatically, you must do it yourself. The reason why we need to do this, is because ModernUI uses WPF resources, particuarlly the following:
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml"/>
</ResourceDictionary.MergedDictionaries>
So this is what we're going to do. Create a new .xaml file in your Forms Project of type ResourceDictionary (name does not matter; I used the name: Resources.xaml). It should look something like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    
</ResourceDictionary>
Next, add these line of codes in your Form Project's constructor:
'Declare a new instance of Windows.Application (this only gives your project a new instance of this; using it directly is not necessary)
Dim Application as New Windows.Application
Now, add these lines of code to whatever event is supposed to open a ModernUI window:
'Declare an instance of the ModernUI window from the WPF project
Dim muiWindow as New WPFProject.muiWindow

'Next, declare a resource dictionary, and set its source
Dim rd As New Windows.ResourceDictionary
rd.Source = New Uri("/FormsProject;component/Resources.xaml", UriKind.Relative)

'Finally, show the muiWindow
muiWindow.Show()
That's it! You should be set. If you have any further questions, let me know.

Best regards,
  • Jake M.
Sources:
Set up application resources from code
Managing Application Resources when WPF is Hosted

BlazingArdor wrote Jan 28, 2015 at 3:21 PM

Edit:

My apologies. The correct code is the following for the particular section:
'Declare a new instance of Windows.Application (this only gives your project a new instance of this; using it directly is not necessary)
Dim Application as New Windows.Application

'Next, declare a resource dictionary, and set its source
Dim rd As New Windows.ResourceDictionary
rd.Source = New Uri("/FormsProject;component/Resources.xaml", UriKind.Relative)

'Finally, add the resource to your application:
Windows.Application.Current.Resources.MergedDictionaries.Add(rd)
Only declaring and showing the ModernUI window is necessary in the event that creates it. The ResourceDictionary section should be added into your constructor as well.

BlazingArdor wrote Jan 28, 2015 at 3:25 PM

Edit:

I have found the original discussion here if you'd like to know more:

ModernUI Started Externally (Possible bug?)