I have a WPF application and am suddenly getting the error
Value cannot be null. Parameter name: hostNameOrAddress
With this error the program will still run. When I remove the datacontext from the window this error goes away. In this window my datacontext is my MainViewModel. I tried removing the MainViewModel from the assembly and adding it back in thinking that might solve the problem but to no avail. Does anyone have any thoughts on why this could be happening. Note: I am using MahApps.Metro.
<controls:MetroWindow x:Class="DatalogConversion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DatalogConversion"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:helper="clr-namespace:DatalogConversion.Helper"
Height="auto"
Width="325"
SizeToContent="Height">
<controls:MetroWindow.DataContext>
<local:MainViewModel/>
</controls:MetroWindow.DataContext>
I was using an attached property to call a method in my view model when my window was loaded
helper:MvvmBehaviors.LoadedMethodName="OnWindowLoaded"
I didn't realize this at the time but the problem was I was calling a service from the method and it was breaking the designer. To fix the problem I inserted a guard clause in the OnWindowLoaded method to fix the problem.
if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject())) return;
Related
So I have a dll from another project which contains many useful classes and controls for me (lets call it foo.dll). I'm making an WPF app. I need to use some of them in my app. I created my usercontrol for windows forms and referenced UserControlForMe from foo.dll. It's shown, all good. Now I want to insert my usercontrol into a wpf form. It looks like this:
<UserControl x:Class="FlatRectangular_Profile.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:uc="clr-namespace:FlatRectangular_Profile.UC"
Height="2093" Width="717">
<Grid Name="grid">
<WindowsFormsHost>
<uc:WindowsFormsProfManual ></uc:WindowsFormsProfManual>
</WindowsFormsHost>
</Grid>
</UserControl>
But here I get an error "cant load type UserControlForMe from foo.dll". No info on that error. Again, UserControlForMe loads in WindowsFormsProfManual. All these is going on in one class library. I referenced everything that foo.dll needed.
No idea how what to do next. I also tried to load it in code in usercontrol.loaded event, but it fails too, and shows stacktrace which leads to the constructor of the UserControlForMe.
I guess you'll have to add the assembly to your namespace import to point your application in the right direction:
xmlns:uc="clr-namespace:FlatRectangular_Profile.UC;Assembly=MyDLL"
I found a workaround since I cant get why it is not working. If I load a UserControlForMe from foo.dll directly to the windowsformhost, it works. But if there is a "buffer" dll, it works in this dll, but doesnt open in futher window. Also I add a UserControlForMe programmatically to a windowsformhost.
My application runs and works absolutely fine. Now I want to use the VS2012 designer to design my MainWindow which is derived type of the HandledWindow from a different library.
Here is my XAML code for MainWindow:
<UI:HandledWindow x:Class="Diamond.Executor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UI="clr-namespace:Diamond.Core.UI;assembly=Diamond.Core"
Title="MainWindow"
Height="250"
Width="500"
Style="{DynamicResource WindowStandard}"
WindowStartupLocation="CenterScreen"
Loaded="Initialise">
Here is where base type HandledWindow:
namespace Diamond.Core.UI
{
public class HandledWindow : Window
{
What is wrong here? When I run my application anything works, the bindings, The style and it's template.
But suddenly the designer says "'UI' doesn't map to a namespace". I just don't get it. It stops my development really without a reason. Perhaps a VS2012 bug, But I want to insure I am doing everything right and if there is a way to pass that bug and continue using the designer?
I've discovered what just causes the issue, But I don't know why, Because by syntax it's okay and logically it works. But XAML's desginer fails to work properly with such data binding expression:
FontSize="{Binding Path=(UI:HandledWindow.FontSizeTitle), Mode=OneWay}"
(It's part of a style inside the UI namespace that XAML declined in a whole because of this expression)
Then I've changed it to this expression:
FontSize="{Binding FontSizeTitle, RelativeSource={RelativeSource TemplatedParent}}"
It does exactly the same, But now the XAML designer works again and stops ignoring the whole namespace UI because of a single data binding expression.
Anyway, The issue is finally resolved.
I have a user control. I had this situations again some times but could always fix it by using the "New() contructor". But I still wonder what I am doing wrong because the load event has to be fired if control was loaded!
Here is some code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:OUTPUT___VideoContent"
Title="OUTPUT - VideoContent" Height="350" Width="525" Icon="/OUTPUT%20-%20VideoContent;component/Images/VideoContent.png">
<Grid x:Name="LayoutRoot">
<Grid x:Name="VideoGrid">
<my:ucVideoPresenter x:Name="VideoPresenter1"/>
<TextBlock x:Name="txtInfo" Visibility="Collapsed" />
</Grid>
</Grid>
</Window>
and in the usercontrol, the load event is declared on WPF or codebehing without any success!
This is because an exception is being thrown in the 'Loaded' eventhandler. The exception may be occurring as a result of a mixed mode assembly or some other exception that is "user handled", and the WPF framework is catching it (unknown to the debugger). This causes the debugger not to break when a breakpoint is set within the Loaded method.
To make sure you can see exactly what error is occurring:
In VS2010 go to Debug | Exceptions.
Tick "Thrown" exception radio boxes for the exceptions that may be applicable in your case.
Re-run the app and VS2010 should break on the exception that is being thrown in the event handler.
Debug according to a now known exception.
Does your UserControl constructor still make a call InitializeComponent(), without this, it will not build up its visuals and the Loaded event may not fire.
I'm having a problem accessing a Panel control defined on the XAML of a page, the XAML is defined this way:
<UserControl
x:Class="PhoneBook.SilverlightMainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d" Width="400" Height="300" d:DesignWidth="993" d:DesignHeight="887">
<Grid x:Name="LayoutRoot" />
</UserControl>
The class is defined like this:
public partial class SilverlightMainPage : UserControl
{
public SilverlightMainPage()
{
InitializeComponent();
}
}
And I'm trying to instantiate it this way:
var silverlightMainPage = new PhoneBook.SilverlightMainPage();
SomeMethod((silverlightMainPage.LayoutRoot);
What I find strange is that when I put the dot after the object instance, it actually list LayoutRoot as one of the members, but when I try to compile the application it says that there's no member with that name.
Any ideas of what can be hapenning?
Thanks
EDIT: I also tried creating a property on the SilverlightMainPage class that returned the LayoutRoot element, but it also says that the class doesn't contain a definition for Layout root.
Is there any chance that you're trying to access SilverlightMainPage.LayoutRoot from a different assembly? In the MainPage.g.i.cs file, LayoutRoot (and all other controls defined in XAML) are marked "internal", i.e.:
internal System.Windows.Controls.Grid LayoutRoot;
You might want to try creating a public rather than an internal property that does a FindName("LayoutRoot") and returns the appropriate control.
Actually I found the problem.
I was generating the project automatically with a tool built by someone else in the company.
I did some extra tests and added a new UserControl to the project and tried to access the LayoutRoot from a property in the code behind, and it worked.
Then copied the exact same code to the file with the problem (just changing the class name) and it didn't compile.
Then I checked the project file, and found a section like this:
<ItemGroup>
<Compile Include="SilverlightMainPage.xaml.cs">
<DependentUpon>SilverlightMainPage.xaml</DependentUpon>
</Compile>
</ItemGroup>
Which for some reason was causing the compilation to fail.
I removed that section and now everything works fine.
Thanks for your answers though.
This is my first foray into custom controls, and it's not going well. I have a custom graph control derived from Canvas.
namespace Grapher2 {
public class SeriesManager : Canvas {
public SeriesManager() {
...
}
}
}
It's defined in the same project and namespace as my app. I tried adding a reference to the control in XAML as follows:
<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">
<StackPanel Name="container" Width="700" Height="500">
<graph:SeriesManager Name="seriesManager" Width="700" Height="500" />
</StackPanel>
But when I try to reference the control name "seriesManager" in the code-behind for the Window, I get "The name 'seriesManager' does not exist in the current context."
Furthermore, the XAML editor will not render the Window, giving a huge stack trace with the error: "Type 'MS.Internal.Permissions.UserInitiatedNavigationPermission' in Assembly 'PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."
I imagine the solution is something stupidly simple for anyone who's done custom controls. But I'm stumped.
did you try x:Name="seriesManager" in your xaml?
Edit: This may not be the issue seeing how you said your xaml isn't rendering. I'm guessing once you get the xaml to render in the designer... the code behind will work better.
Edit 2: Whenever I've had a problem with the designer rendering, it's because I'm doing something in the constructor of my custom control. Check your SeriesManager to see if you are doing something in its constructor that is causing a problem. Maybe you are referencing something that doesn't exist yet. If you do have extra code in your constructor, consider moving it to the UserControl_Loaded event.
Backing up Scott's answer here, since he helped me solve it:
What I did wrong was trying to access the control BEFORE InitializeComponent(), but was confused by 2 other error messages somewhere else in the code.
Just in case someone else has this error.