So I realize this is an often-asked about error, and I've read many results from google, but I still can't figure out how it applies to my situation.
The setup: I create a window to present views. I create an instance of the first view and set the window's Content property to the view. Currently each of my views is a window itself. This is working perfectly on 7/10 computers. However, for some reason on a few computers (verified correct .NET version), switching to the second view (creating an instance of the second view and setting the window's Content property to the second view) throws a System.InvalidOperationException Specified element is already the logical child of another element. Disconnect it first.
I read that using ContentTemplate instead of Content would solve the problem, but that just produces a blank screen instead of the second view (I'm sure I'm doing that wrong).
I've also read that you should use a UserControl instead of a Window as the Window's Content property, so I'm trying that (I don't have a computer that's getting this error, so I have to wait to hear back about success). But it seems to me that that won't really change anything.
Any ideas why this is only happening on some computers, and any ideas on how to fix it?
Edit: Changing the views to UserControls did not solve the issue
Edit: I realized that I misspoke. I don't set the Content of the main window to a Window, I set it to the second Window's Content. So like:
MainWindow.Content = nextScreen.Content;
Thanks for the answers everyone. I discovered that changing my views to UserControls was the correct path. When I tried it the first time I made this mistake:
window.Content = userControl.Content;
instead of
window.Content = userControl;
which still worked on my machine, but continued to fail on the computers that were previously having problems.
I still don't understand why only a handful of computers were throwing an exception over this. Obviously there is some difference between them, though it wasn't anything obvious like Windows version or processor architecture.
Related
Application
It's a WPF application meant to send files between clients. I'm working on a usercontrol meant to show a list of sent files, their recipients, etc.
Setup
I'm trying to style the control, but I need sample data to populate it. Following these instructions, I create a SampleFilesSource.cs that extends DataSourceProvider. It generates an instance of the view's viewmodel populated with an ObservableCollection of random recent files.
Next, in the view, I set the design time DataContext with this:
<d:UserControl.DataContext>
<sample:SampleFilesSource/>
</d:UserControl.DataContext>
But nothing shows up... What happened?
The Data-Bound Exception
Well, let's see. The Data pane in Blend allows us to see what's in the design time DataContext that's available for binding. Let's take a peek.
Well that's interesting. There's an exception in my DataContext. The Data pane doesn't show you any of the values, just what fields can be bound to. So how do I get a peek at the exception's Message? Can I bind to it?
Binding To It
Unfortunately not... In fact the only binding I can get to output any text is "(Collection)", when I try {Binding Data.RecentFiles}. The fields are visible under the Data object, but nothing has any values.
But what if I set the run-time DataContext to the sample data generator? Just remove that little d: from the DataContext tag and debug... Still no luck. By taking a peek at the live visual tree I can see the DataContext is bound to an instance of the viewmodel. And interestingly enough, the only thing in the datacontext this time is an Error field.
There's no Data field at run-time and the Error field is an instance of Exception. I think it's null. The property explorer just has an empty box, with no options to view it. I think it's weird that no unhandled exceptions showed up in the debugging session. It got stuck in the DataContext somehow?
Debugging
So other than clean, rebuild, restart VS, restart the computer, and whatever else I had already tried, what else can I do to get a peek at that Message? Here's a handy little guide to attaching a debugger to Visual Studio's designer process. Pretty cool!
I can only learn a couple things from the debugger:
My viewmodel being generated by my DataSourceProvider is valid, looks good, no exceptions thrown.
My DataSourceProvider's Error field is null when it leaves my code.
So all I can say that the exception occurs after I've called DataSourceProvider.FinishQuery, and before the designer gets it's hands on the generated DataContext.
Conclusions
I'm stumped. Any ideas?
Update
I created a minimum example to share, and sure enough the exception does show up in the Data pane in expression blend. However, I can actually bind to the generated sample data in the example, so now I think the exception was a red herring and my problem is elsewhere. I'm not even sure if there is an exception, and the Data pane just shows what it does because there COULD be an exception there.
Here's the example if anyone else wants to peek.
Can you show some code?
You can look at the problem in a simple way, you can try to bind a single value. If successful, you can prove that your binding part is no problem. Then you can start with your data template.
I'm writing an Universal App in C# for Windows 10 using the SplitView with a frame for navigation.
I have a sidebar with a list that I load dynamically and, when I click on one of the items, I navigate to a page using the model from the menu to indicate which item I should load in the Frame.
I followed this sample: Windows-universal-samples/Samples/XamlNavigation/
The only difference is, instead of having multiple pages, I have only one page that is loaded every time I select a different item with its specific ViewModel. I use Autofac to load the ViewModels and MyFrame.Navigate(typeof(DetailsPage), idOfItem);to load the page.
The problem is, every time I navigate to the next page, it seems that the instance of the previous one is lost and when I navigate back, it loads a new instance. This kind of thing doesn't seem to happen when I'm navigating in the RootFrame and it didn't happen in Windows Phone 8.1.
I've been searching for a solution on the Web for hours, but I can't find anything relevant.
Does anyone know why this could be happening?
I hope I was clear enough with my question.
Thank you in advance for the answer.
The behavior you observe is correct and as expected. You have two options:
You can use Page.NavigationCacheMode, set it to Required. This is easy, but may consume a lot of memory.
Or you can save page state in OnNavigatedFrom and restore it in OnNavigatedTo. In fact, depending on your scenario, it may not even be necessary to save state in OnNavigatedFrom, assuming the state (your DataContext) can be constructed when returning to the page in the same way that you constructed it when first comming to the page.
for our WPF application I have designed a CustomControl to mimic some flyout-like behavior. The flyout basically just consists of a FrameworkElement which holds the content that is placed inside the flyout.
Controls that are placed in the FrameworkElement of the flyout are, of course, bound to other controls. However, I am confused as this results in several problems:
I can not use ElementName=sourceName to set the source of the binding. Instead I have to use Source={x:Reference sourceName} to denote the source.
The XAML editor of VisualStudio marks all lines where a binding of this type occurs and the ErrorList shows "Object reference not set to an instance of an object" for each occurrence of the binding (no exception is thrown within the designer view).
The project builds successfully and during runtime all bindings work flawlessly! Besides that, it does not matter if the flyout is implemented as a UserControl or a CustomControl.
If a control that is not placed in the flyout is bound to the same source I can use ElementName=sourceName without any problems and the correct values are displayed in the designer.
As proposed in some other replies concerning this error I tried to attach the VS instance to the debugger of a second instance of VS but no errors/exceptions are displayed there.
As already mentioned, everything works at runtime. However, as multiple developers are working on this project it is not acceptable to have any errors at all; warnings would be acceptable to a certain extend.
We know that using a binding proxy (like it is proposed in e.g. https://stackoverflow.com/a/22531679/119177) should/could solve the problem. However, because the current binding is actually working, we hope to solve the design-time problem some other way (maybe even with just suppressing the error message).
Thanks for your help!
I have a user control which is a labelled combo box, through debugging I noticed that each time I open the view which contains the user control there is one more get request to each of the models properties than the previous time!
I do re-instantiate the model each time the view is opened (for reasons that don't really need mentioning), but what appears to be happening is that the binding to any previous copies of the model are retained and the model seems to not be being disposed of correctly because it is still tied into the program through this binding! I appreciate that multiple bindings to the dependency property can be sustained, but how do I stop this happening?
Two considerations I have thought of:
1) The usercontrol is not being disposed of when the view is closed, if this is possible should I turn my attention towards ensuring it is being disposed of? Or is this overkill to the needs of WPF?
2) Instead of re-instantiating the model each time the view is opened should I just implement a copy function in the model base class which transfer all of the properties over opposed to actually creating another instance of the model?
What do you think?
Thanks
Finally fixed this problem, the issue was not limited to my user controls but in fact all controls (including the standard WPF ones) with bindings! When the window was being closed the bindings were not being terminated, this bug/issue/design (maybe it has a use - although I can't see what that would be) seems very strange to me. Initially I did think maybe it was the way I set the bindings up, however with one of the bindings to a normal text box I simply use Text="{Binding name}" - no other configuration defined which could be causing this issue!
Anyway, the solution I found was to add BindingOpertations.ClearAllBindings(this) to the windows closing event. Hope this helps if you are having this problem.
I am building a project with the WindowsFormsApplication libraries and everything was going well until I noticed that the class properties are not being set at runtime anymore. The first instance occurred when I set menustrip hotkeys: the set in the designer, the code for it is there, I made sure nothing else would be sneakily altering the value(s) at runtime by using methods instead of the 'property' format. Then, somehow I managed to fix this by calling a method created specifically for setting menustrip hotkeys right after the initialization method.
NOW, the docking properties will not set or, in other words: I set a control's dock style to dockstyle.fill and when I maximize the window, it's as if the dock style was never set, but the code that says-so, is still there! the dock-fill property was working successfully only a moment ago...
I've searched for problems similar to mine for a good day and only found one result, but the questioner wasn't provided good answers ~in my opinion. I don't have much time so any help would be much appreciated!
In the mean time, I'll port all the controls over to a new form. That seemed to do the trick for two other occurrences with past projects...
I have had bizarre results with Visual Studio sometimes losing it's mind. Exiting and restarting seems to fix it. What version are you using?