Problem Inserting Report Viewer into WPF Application using WindowsformsHost - wpf

I am trying to create a popup from my WPF application which shows a report viewer hosted in a WindowsFormsHost
however I am having problems with the following Xaml
<Page x:Class="FIS3.ReportViewer.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="MapViewer">
<Grid>
<my:WindowsFormsHost>
</my:WindowsFormsHost>
<Label Name="exampleText" Content="this is the Report Viewer ..." />
</Grid>
I am getting a build error notifying me that
"The type 'my:WindowsFormsHost' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built"
have I made an error in my XAML
I have added WindowsFormsIntegration as a reference to my project.
Thanks for your help
Col

There is a second solution to this that people seem to have overlooked. I scratched my head on this one for a good 20 minutes. No solution, including this one, would work. Turns out the solution for me was to add:
WindowsFormsIntegration
to my references
after I realized that based on:
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx
that this uses the WindowsFormsIntegration.dll. It seemed to have been missing from my references.
Hope this helps!

According to the MSDN documentation, WindowsFormsHost is included in the default Xaml namespace ("http://schemas.microsoft.com/winfx/2006/xaml/presentation") - though you do need to reference WindowsFormsIntegration, as you have done. Have you tried referencing WindowsFormsHost with no namespace prefix?
<Page x:Class="FIS3.ReportViewer.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MapViewer">
<Grid>
<WindowsFormsHost>
<WindowsFormsHost>
<Label Name="exampleText" Content="this is the Report Viewer ..." />
</Grid>

Related

MahApps.Metro cannot load file or assembly in Visual Studio extension

I am using MahApps.Metro controls in my XAML code for the toolbox in Visual Studio Extension. I installed the package via NuGet, then I tried to add a control into my XAML markup. Below is the code snippet.
<UserControl
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"
xmlns:custom="http://metro.mahapps.com/winfx/xaml/controls"
x:Class="AutoDebug.MyControl"
Background="{DynamicResource VsBrush.Window}"
Foreground="{DynamicResource VsBrush.WindowText}"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="400"
DataContext="{Binding UserControlModel}"
x:Name="AutoDebugWindow">
<Grid Margin="15">
<custom:Tile Content="Tile" HorizontalAlignment="Left" Margin="75,150,0,0" VerticalAlignment="Top" Background="#FF8B00BF"/>
</Grid>
</UserControl>
But I receive the following error no matter what.
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Could not load file or assembly 'MahApps.Metro, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I have already tried installing/uninstalling, deleting/adding references but nothing has worked so far.
This is caused by the fact that MahApps.Metro is not included as a reference when the Visual Studio extension is compiled.
I'm not entirely sure why, but if you only use MahApps in XAML, then no reference is included in the compiled assembly. You can check this by unpackaging the extension (it's just a zip file), and opening the assembly in ILSpy. Under the references, MahApps will not be listed.
A workaround for this is to use MahApps somewhere in code. The simplest way to do this is to name the MahApps control that you are using. This generates a field for the control, and that seems to be enough to cause a reference to be included in the assembly.
<Grid Margin="15">
<custom:Tile x:Name="MyTile" />
</Grid>
You can also use an object from the MahApps assembly anywhere else in code (for example, you could create a new object in the constructor of the Package), but giving one of the controls a name is probably the simplest way.

windowsformhost cant load a usercontrol from another dll

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.

IntelliSense for Data Binding not working - followup

Having got IntelliSense for Data Binding working in a simple test application, thanks to the answer to my previously raised question, I'm now trying to apply what I've learnt to the actual application I'm working on. Again I'm encountering problems that I don't understand. A snippet of my code is below - I've had to change names to protect propriety information:
<Page x:Class="MyProject.Views.Pages.MyPage"
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" d:DesignHeight="300" d:DesignWidth="450"
xmlns:Converters="clr-namespace:MyProject.Converters"
xmlns:ViewModels="clr-namespace:MyProject.ViewModels"
Title="My View"
SnapsToDevicePixels="True" KeepAlive="True" TextOptions.TextFormattingMode="Display">
<Page.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
</Page.Resources>
<StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">
<!-- ... -->
</StackPanel>
</Page>
I'm getting an error message on the line <StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">:
The name "MyViewModel" does not exist in the namespace "clr-namespace:MyProject.ViewModels".
The error doesn't make sense MyViewModel does exist within the MyProject.ViewModels namspace.
Any suggestions? I've tried a clean rebuild.
The MyProject.ViewModels namepsace is within a different assembly to the MyProject.Views.Pages and it appears to necessary to add ;assembly=MyProject.ViewModels to the xmlns:ViewModels="clr-namespace:MyProject.ViewModels delcaration:
xmlns:ViewModels="clr-namespace:MyProject.ViewModels;assembly=MyProject.ViewModels"
I assumed that because the assembly is referenced by the project, I wouldn't need to specify an assembly, just as I don't have to specify an assembly when using a namespace within a C# code file.
I just got the same error and I got it many times before. Each time I have this error, it come from the fact that I have other errors and my code won't compile for other reasons. As soon as I fixed all other errors, my code compile and VisualStudio find all 'd:' missing references... if they really exists.
Also, as an alternative, if you instanciate your ViewModel with its default constructor, I suggest to use something like (without using 'd:'):
...
</Page>
<Page.DataContext>
<ViewModels:MyViewModel/>
</Page.DataContext>
It will solve your problem forever.
I haven't tried it yet, but perhaps that with Roslyn (the new VS2015 compiler) this problem will go away. I hope :-)

View in a module not displaying image but it will display text

I am using MEF, Prism and WPF. I have a simple module (for now, I'll do more with it when I get past this roadblock) that displays a single image sourced as static resource in that module. Here is the xaml for the view:
<UserControl x:Class="SplashScreenModule.SplashView"
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"
Height="960" Width="1180">
<Grid>
<Label>
<Image Source="Images/My Image.png"/>
</Label>
</Grid>
The image shows up in the designer but when I run the code the window is blank. If I replace the image with simple text the text is display as expected.
<Label>
This shows up in the window.
</Label>
I've spent the last 3 hours trying to figure this out and am stumped. I would appreciate any help.
Thanks,
Bill
You have to know that there is a difference on how ressources are managed at DesignTime and at RunTime. At DesignTime there is no compilation required, thus it is able to display a ressource even if it is not include in the project.
At runtime this is normally possible, but it is better to include the ressources in your project (ie create a folder Images in your solution, and add the "My Image.png" in this folder), to be sure of the location of your ressource compared to your exe.
If you do it and the image still doesn't show up, try to remove the label and execute again.
By doing this it works fine for me. As soon as the image show up, you can add back the label.
Hope it helps.
The following code is working for me:
<Window x:Class="LabelImage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label>
<Image Source="Images/visual studio.png" />
</Label>
</Grid>
</Window>
I structured my solution like you did: I have the picture inside a folder named Images. The MainWindow is located directly under the project node (it's not in any subdirectory).
Make sure that the Build Action for your image is set to resource, otherwise it won't work with the WPF Resource Finding System (which internally uses PACK-Uris: http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx)
I use .NET 4.5 with VS 2013. If you have any further questions, please feel free to ask.
Update after Bill's comment:
That's a whole different thing when your resource resides within another assembly. The syntax for referencing the image should be like this:
<Image Source="/ClassLibraryForResources;component/Images/visual studio.png" />
where you do the following steps:
Begin with a slash "/"
Specify the name of the assembly that holds your resource (this can also be a strong assembly name)
Continue with ";component/"
End with the path of the image (or other resource)
(Please note that you have to omit the quotation marks).
The solution that I used is structured like this:
This should resemble what you have described. Again: it's all about the PACK URIs that WPF uses (see link above).

What to do when WPF Window doesn't recognize user control defined in the same project?

I have a window "Operation" in XAML that uses a user control "Status" defined in the same project. When I build the solution, it returns ok, but when I open the design view for the Window, visual studio says "Could not create an instance of type 'Status'. The XAML for the Window "Operation" is below:
<Window x:Class="TCI.Indexer.UI.Operacao"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles"
Title=" "
MinHeight="550"
MinWidth="675"
Loaded="Load"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
WindowState="Maximized"
Focusable="True">
<Canvas Name="canv">
<tci:Status x:Name="ucStatus"/>
<Grid Canvas.Top="0" Canvas.Left="0">
<StackPanel Orientation="Horizontal">
<!-- Indices -->
<Label Width="200"/>
</StackPanel>
</Grid>
</Canvas>
</Window>
The xmlns:tci is the namespace where the Status Usercontrol is. And tci:Status becomes underlined in blue when this error happens. How to use a UserControl like this?
I was consistently having this problem with one control in my project and wound up resolving it by changing all of my images to DynamicResource rather than StaticResource.
The really strange thing is that the designer worked just fine for the control itself and showed the images as expected. It was only when I used the control inside my main window that the designer gave me the "Could not create instance of " message.
I don't know exactly whats the solution, but that happens to me too from time to time. I end up deleting declaration of the namespace, rebuilding and trying again. :\
Try cleaning your entire project, then do a full rebuild. The WPF designer in VS is poorly done, in my opinion, and there are weird issues like this all over the place.
I recommend not relying on Design View for anything, at this point - it's just too unstable. Try Expression Blend, it's a little bit better with things like this. If you don't want to go that route, you're probably better off building and running your app :-(
If you're running VS 2008, do you have SP1 installed?
I typically see this when I haven't built the control. Make sure you build the control and see if you still see the problem. Occasionally, VS just gets confused and you need to close and open the control you are having a problem with. In this case, that would be your window.
Similar to what Jon Norton said, I also found this link (-link removed, see below-) that implicates resources. I had the situation described in the link, and Jon's fix sorted it.
EDIT
Sorry, the link now requires a login and the page doesn't exist anymore. I couldn't find what it was supposed to be after three years.

Resources