WPF root element is not valid for navigation - wpf

I am converting a WPF XBAP app to a WPF desktop app. I have it running on the desktop, but am now trying to change the Page references to Window references.
'MyApp.StartForm' root element is not valid for navigation.
I have tried creating a simple version of this app and converting that, and this works fine, so there must be something within the XAML that is causing this when using Window tags. My question relates to how I can investigate this. Currently, all I get is this error, accompanied by a "No Source Available" screen; no stack locations are shown and "Show Disassembly" doesn't work. Other than systematically commenting out individual chunks of XAML until it works, is there a way to work out what this issue is?

Navigation in a WPF application can only be done between pages. The error shows up because you're trying to "navigate" to what is now a Window, and this isn't possible.
Instead of converting your Page into a Window, create a new Window with a Frame control in it. A Frame can be used to host your existing pages - which should stay exactly as they are, and not be changed into Windows.

Its not entirely accurate about it not being possible to host a window in a frame, the following code will do it for you
public void HostWindowInFrame(Frame fraContainer, Window win) {
object tmp = win.Content;
win.Content = null;
fraContainer.Content = new ContentControl() { Content = tmp };
}

please check StartForm having the page as root element or some other else. I am having the same. I have check the page which has to be navigated. I declare that page as window.

Related

Use a control in MainWindow from a nested page WPF VB.NET

I have a program in visual basic .net and WPF that uses a series of pages displayed in a Frame control on my main Window, pages are navigated from controls in the main Window outside of the frame and I have all that working ok.
I also have a MediaElement control on that main Window and I need to be able to change the source property of this control from the user clicking on elements in the pages. Every time I have tried to do this so far i have run into an error.
Right now I have a Public Shared function within main Window that is called from the control in the page, the code on the control passes on the URL to be loaded into the source property to the function, then the function is supposed to pass the url into the source property and tell the MediaElement to play.
The error I am getting when trying to achieve this is:
Cannot refer to an instance member of a class from within a shared
method or shared member initializer without an explicit instance of
the class.
Please help, how could I achive this?
Are you getting an instance of the main window or the media element inside of your shared function? If not, this is you problem.
You might stick the media element into a shared variable when the main window is loaded or you can access your main window using the rootvisual of your application.current.app.rootvisual.
If your issue is something else, please post the code in your shared function that causes the error.
Thank you very much Steve for your answer, however I found how to do what I need on another post after idly trying some different search terms today.
Someone known as Jean on another post: https://stackoverflow.com/a/16781963/2668533 suggested something that worked for me, by using application.current.windows:
Dim parent As MainWindow = Application.Current.Windows(0)
parent.player.Source = New Uri(url)
Apologies for seeming such a novice but WPF is completely new to me and I have never encounterd a need for anything similer using WindowsForms.

Out-of-Browser Silverlight and many "pages"

I'm building a Silverlight out-of-browser app that will eventually run on a Windows 7 touchscreen tablet, independent of any browser - it will run just like any other app.
My code, at the moment, is all within one XAML and corresponding .cs file but this is messy and I'd like to split it out and call each page as and when required i.e. Main.xaml, AboutUs.xaml, Contact.xaml etc.
Is this possible in an OOB app? I tried to use the frame and pages controls, but when I set the source to one of my new XAMLs via a button click i.e. "/AboutUs.xaml", it tells me that it's an invalid URI.
Thanks,
Greg.
Try and create a root canvas (e.g: myCanvas) in your MainPage.xaml to act as a container which displays all your pages.
On navigation clicks, write this.
myCanvas.Children.Clear();
myCanvas.Children.Add(new myPage());
A good practice is to set a public property on every page
public MainPage parentPage;
in this case, to which you can assign the parent page that hold that root canvas (myCanvas in case). On further pages, you just navigate using
parentPage.myCanvas.Clear();
anotherPage tempPage = new anotherPage();
tempPage.parentPage = parentPage;
parentPage.myCanvas.Add(tempPage);

How to make WPF page navigation code look better?

I want to make a program by WPF which includes several pages.
I have a Window and several Pages now.
In order to navigate, I use
this.Content = new Page1();
in the main window (from Window to Page), and
((Window)this.Parent).Content = new Page1();
between pages (from Page to Page), because Page can only be sub-element of Window or Frame.
However, the second line of code above looks quite ugly.
Is there a better way to achieve the same goal?
I have coded from several Windows Phone applications before, and I think it might be better to navigate between Pages rather than hide/show elements (such as Grids).
If your navigation code is on your Page class, either :
move it to the window class
create an event in the Page class, and react to it on the Window class.
Is there any reason why you can't just put a Frame in your Window ?
You could use NavigationService.
http://msdn.microsoft.com/en-us/library/ms750478.aspx
http://www.paulstovell.com/wpf-navigation
Maybe this could help:
http://azerdark.wordpress.com/2010/04/23/multi-page-application-in-wpf/

Weird scrollbar UI in hosted WPF composite control

My windows forms application hosts AvalonEdit (the composite WPF control in question) in one of its forms to cater to its text editing requirements. Here's the code I use:
WPFHost = gcnew ElementHost();
TextField = gcnew AvalonEdit::TextEditor();
WPFHost->Dock = DockStyle::Fill;
WPFHost->Child = TextField;
TextField->Options->AllowScrollBelowDocument = false;
TextField->Options->EnableEmailHyperlinks = false;
TextField->Options->EnableHyperlinks = true;
TextField->Options->RequireControlModifierForHyperlinkClick = true;
TextField->ShowLineNumbers = true;
ContainerControl->Controls->Add(WPFHost); // the container is a panel
The code compiles and executes fine, except for the scrollbars - http://dl.dropbox.com/u/2584752/avalonEditBug.png . Right clicking on what's left of the bar raises an ArgumentOutOfRange exception.
Strangely, I wasn't able to reproduce the issue when I tried hosting the control in a newly created sample project. 'mI using the latest build of the text editor and have all the requisite assemblies installed.
EDIT: Wrapping the editor in a usercontrol doesn't help either.
You say that the control works fine in a new/blank project but fails in the one you need makes me wonder about conflicts more than anything. In the project you're really wanting compared to the project it worked in what are the differences? .NET version? Referencing an assembly from a directory in one but out of the GAC in another?
It's hard to say that the control is messing up for you when you've got it working elsewhere, so the only thing I can suggest is just dive deep into the differences of the two projects.
Good luck.
This looks like a layout error to me. Maybe WPFHost measures the TextField unexpectedly.
I can suggest setting specific Width and Height on the TextField itself. If this fixes the problem you can adjust those as the size of the WPFHost control changes or try setting the MaxHeight/Width, sometimes they help and save some code for Width/Height updates.
try to create a WPF grid as a child of ElementHost, and place the editor inside that grid. On Other way, is to create an UserControl have the editor in that control and use the control inside your Winform app. Such approach helped me a couple of times.
I've implemented a workaround for the issue as mentioned in this thread [ Synchronizing a WPF ScrollViewer with a WinForms ScrollBar ].

Silverlight : Issue with Childwindow and navigation framework

could someone help me with childwindow and Navigation?
I created a childwindow from code and set its content as
ChildWindow CW = new ChildWindow();
CW.Content = new MainPageMenu(param1);
Here "MainPageMenu" itself is a Silverlight Page with a "navigation:Frame"
In the constructor, i navigate to various pages according to "param1" .
As i navigate to the desired page , the ie address bar shows the updated uri.
Should it show that? as the page is navigated in a childwindow......?
This works fine for first time. Once i close the childwindow and reopen with diff "param1" to navigate to other page
it never gets navigated..!!! it shows the same page as navigated to for the first call...
there is no error at line -
this.ContentFrame.Navigate(u);
in the constructor.
Is this a bug in navigation framework or i am supposed to something else to achieve this ?
Please help.
I see you already figured out that JournalOwnership is th key here. But just for a bit more background so this is less magic and more algorithm, here's how the Navigation Framework figures out whether to integrate with the browser journal, based on what Frame.JournalOwnership is set to.
JournalOwnership.Automatic (the default) - if the Frame is a "top level" Frame, it will navigate with the browser journal. If it's not, it won't. "Top level" means that if it walks up the visual tree it does not find any other Frame along the way.
JournalOwnership.OwnsJournal - the Frame will always own its own journal and not attempt to interact with the browser's journal.
JournalOwnership.UsesParentJournal - the Frame will always integrate with the browser. If it is not a top-level Frame this causes an Exception to be thrown.
So what's interesting about ChildWindow is that when Frame walks up the visual tree to see if it's a "top-level" Frame, it won't find one (because Popup is not rooted in the visual tree in the same way, and ChildWindow uses a Popup). Hence, it will think it's top-level (which may not really be correct in this case, but what else could it do?) so it will integrate with the browser journal.

Resources