I've hardly tried to use Cognex In-Sight SDK together with WPF app. In the SDK there are some examples but unfortunately only for WinForms. I was wondering that I can make a Host for WinForm control fron Cognex DLL, but with no succes. I've added WinFormsIntegration and also System.Windows.Forms.dll. I added a namespace in XAML:
xmlns:cognex="clr-namespace:Cognex.InSight.Controls.Display;assembly=Cognex.InSight.Controls.Display"
and as well tried to use:
<WindowsFormsHost HorizontalAlignment="Left" Height="244" Margin="314,73,0,0" VerticalAlignment="Top" Width="275">
<cognex:CvsInSightDisplay Name="Display" />
</WindowsFormsHost>
but it throws: The name does not exists in the namespace. Strange, because when typing the exactly the same contol name is suggested.
Is there any other option to get Cognex controls in WPF? OpenAI suggested the file named Cognex.InSight.Controls.Wpf.dll but I can't find it.
Regards. Piotr.
Related
I have a working application that has been written in C#, and I now want to extend that application to allow the user to switch between viewing the application, and viewing a built in web browser (inside the same application window).
I also have a separate working web browser, that has also been written in C#.
I have just added the functionality to the original application to include 'tabbed' displays, where the original application will be displayed on the first tab, and a built in web browser on the second tab.
The 'tabbed' displays for the application have been created using XAML markup in Visual Studio. I now want to add an instance of the Web browser that has also been written in C# to the second tab that I have created in the XAML markup.
It would be something like:
<TabControl>
<TabItem Header="Browser">
<StackPanel>
<!-- Call/ instantiate the browser here -->
</StackPanel>
</TabItem>
</TabControl>
But I have no idea how I call/ create an instance of the browser from within the XAML markup...
The browser has been created using C#:
namespace Agent
{
public partial class Browser : Form
{
public Browser()
{
...
}
}
}
Can anyone explain to me how a create an instance of Browser inside the ` of the XAML markup?
Edit
Ok, so I have edited my XAML markup as recommended in the answer that's been suggested- I now have:
<Window ...
xmlns:Agent="clr-namespace:Agent"
...>
<Grid>
...
<TabControl>
<TabItem Header="R">
<StackPanel>
...
</StackPanel>
</TabItem>
<TabItem Header="Browser">
<Agent:Browser x:Name="Browser" />
</TabItem>
</TabControl>
</Grid>
</Window>
I have also updated my Browser.cs class, so that it is now extending UserControl, rather than Form:
public partial class Browser : UserControl{
However, I am getting a compile error on the line:
<Agent:Browser x:Name="Browser" />
which says:
The name "Browser" does not exist in the namespace "clr-namespace:Agent".
But clearly Browser does exist in Agent, as shown by the code I've included here... In fact, when typing the line <Agent:Browser x:Name="Browser />, when I typed the :, Browser was one of the options that came up in the autocomplete menu...
What am I doing wrong here? Why doesn't the compiler think that Browser exists inside Agent?
The key to instantiating any object in XAML is to make sure the namespace is declared. You can provide any XML prefix and assign it to your CLR namespace (ref) and it will act like a using statement. For example:
<TabControl xmlns:agent="clr-namespace:Agent">
<TabItem Header="Browser">
<StackPanel>
<agent:Browser/>
</StackPanel>
</TabItem>
</TabControl>
NOTE: your object has to extend UIElement (or one of its children) for it to work in a XAML tree. If your control is a WinForms control you either need to find the equivalent XAML control or wrap it in a WindowsFormsHost (ref).
WPF vs. WinForms
The purpose of this section is to help recognize which platform code is by namespace, as well as some of the trade-offs. I've used both and can say from experience that they each have good points and... not so good points.
WinForms classes live in the System.Windows.Forms namespace, and are available by referencing the System.Windows.Forms.dll assembly.
WPF classes live in the System.Windows and System.Windows.Controls namespaces, and are available by referencing a set of DLLs
WinForms rendering is immediate. That means you are working against bitmaps and you are responsible for clearing and redrawing stuff yourself (usually you can just call Invalidate()). If you do heavy image bit manipulation, WinForms is easier to work with.
WPF rendering is declarative. That means more work is offloaded to your GPU and you just tell it how to draw stuff. You can also use GPU render shaders for special effects. WPF has a nicer look out of the box, but it has a reputation for making easy things difficult but impossible things possible.
WinForms is easier to learn, but has a dated look out of the box.
WPF is built around data binding, enabling the UI to update in response to property values automatically. It's also able to be completely restyled, although that is quite an undertaking.
If you are just getting started, I'd go ahead and bite the bullet to start the heavier learning curve for WPF. It will provide a basic understanding that transfers to other platforms like Windows Store apps, etc.
Firstly you need to place that tag inside of your UserControl opening tag like so:
<UserControl x:Class="View.testControl"
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:Agent="clr-namespace:Agent">
Then you can use it like this:
<TabControl>
<TabItem Header="R">
<StackPanel>
...
</StackPanel>
</TabItem>
<TabItem Header="Browser">
<Agent:Browser x:Name="Browser" />
</TabItem>
</TabControl>
EDIT
From what you told me in the comments you will need to create a Custom Control in your WPF project. To do that you need to:
Right Click your Project;
Select Add New Item;
From Installed (which is in the left column) select WPF;
From the list in the middle column select Custom Control;
Now you can create that control in your XAML with xmlns attribute.
Here is a great example from msdn on how to create custom controls
I've been developing a UWP project in my spare time to get a hang of UWP, MVVM and Prism. The project was originally really classic, with no use of MVVM and Prism, and I've been working to get those 2 into the project. I've been relying on https://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx, https://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx and https://msdn.microsoft.com/en-us/library/ff921098(v=pandp.40).aspx to work my way through it.
Some background: I originally had a direct function call from my Mainpage.xaml to the MainPage.xaml.cs codebehind, but during the conversion to MVVM and a separate usercontrol, I removed that function call so I could later using Command Binding. After I removed that, I got an error that was somewhere in GameRouletteView.g.i.cs that was a remnant of this removed function call, where the g.i.cs file assumed it was still bound. I rebuilt my project and those g.i.cs files apparently got removed.
I added the following lines to my Usercontrol View so my ViewModel gets added:
xmlns:gameRoulette="using:GameRoulette.DesignViewModels"
xmlns:prism="using:Prism.Windows.Mvvm"
d:DataContext="{d:DesignInstance gameRoulette:GameRouletteDesignViewModel, IsDesignTimeCreatable=True}"
prism:ViewModelLocator.AutoWireViewModel="True"
Full Code:
<UserControl
x:Class="GameRoulette.Views.GameRouletteView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GameRoulette.Views"
xmlns:gameRoulette="using:GameRoulette.DesignViewModels"
xmlns:prism="using:Prism.Windows.Mvvm"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
d:DataContext="{d:DesignInstance gameRoulette:GameRouletteDesignViewModel, IsDesignTimeCreatable=True}"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid Background="White">
<Button x:Name="btnSelectGames" Content="Click here to select your games"
HorizontalAlignment="Left" Margin="110,50,0,0"
VerticalAlignment="Top" Height="40" Width="240"
Click="{Binding SelectCommand}"/>
<Button x:Name="btnChooseGame" Content=""
HorizontalAlignment="Left" Margin="110,150,0,0"
VerticalAlignment="Top" Width="240" Height="40"
Click="{Binding ChooseCommand}" IsEnabled="True"/>
<ProgressRing HorizontalAlignment="Left" Margin="200,100,0,0"
VerticalAlignment="Top" RenderTransformOrigin="1.05,1.983"
Height="45" Width="45" IsActive="True" Visibility="{Binding }"/>
<Image x:Name="imgFileIcon" HorizontalAlignment="Left"
Height="64" Margin="110,224,0,0"
VerticalAlignment="Top" Width="64" />
<TextBlock x:Name="lblFileName" HorizontalAlignment="Left"
Margin="179,224,0,0" TextWrapping="Wrap"
Text="" VerticalAlignment="Top" Width="171" Height="64"/>
</Grid>
</UserControl>
It gave the following error:
The name "GameRouletteDesignViewModel" does not exist in the namespace "using:GameRoulette.DesignViewModels".
I rebuilt the project, and then it gave the following error for each of my 3 .xaml files: GameRouletteView, App.xaml andMainPage.xaml:
'GameRouletteView' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'GameRouletteView' could be found (are you missing a using directive or an assembly reference?)
Also, when first opening the project, I get the Intellisense errors:
[Failure] Could not find file 'C:\Users\username\Source\Repos\GameRoulette\GameRoulette\GameRoulette\obj\ARM\Debug\MainPage.g.i.cs'.
[Failure] Could not find file 'C:\Users\username\Source\Repos\GameRoulette\GameRoulette\GameRoulette\obj\ARM\Debug\Views\GameRouletteView.g.i.cs'.
[Failure] Could not find file 'C:\Users\username\Source\Repos\GameRoulette\GameRoulette\GameRoulette\obj\ARM\Debug\App.g.i.cs'.
Things I've ruled out:
My namespaces are correct;
I've tried https://stackoverflow.com/a/27260580/1770430, didn't work;
i've deleted the bin, obj folders and the .suo file, didn't fix it;
I've closed and reopened the solution, did not fix it.
Repair Visual Studio through the add/repair/remove programs window, no result.
I've googled this error, but I can't really find anything that I haven't tried yet.
I've also noticed that my NuGet Packages have gone missing and that my Package Manager Console does not recognize NuGet anymore. I also get this error:
Microsoft.NETCore.Portable.Compatibility 1.0.0 provides a compile-time reference assembly for mscorlib on UAP,Version=v10.0, but there is no run-time assembly compatible with win10.
I got a feeling that all these issues are related, but I can't figure out what's wrong with it. As mentioned above, Google doesn't really provide much assistance, and what it does provide doesn't work.
I'm using Visual Studio 2015 Community Edition with Update 1. The project can be found at https://github.com/nzall/GameRoulette.
I've struggled with this issue for a couple of days, and, at least in my case, I'm pretty sure that it depended on some subtle bug that I introduced inadvertently in my xaml code. Or, better, not exactly a bug, but something that's not supported by the version of xaml for the platform we are targeting. In that case the xaml parser fails, and the g.i.cs files (which contain the part of the pages and app classes generated by the parser, including the InitializeComponent method) are not generated as expected.
Why do I believe that?
At first, I thought it was a problem with Visual Studio, so I cleared component cache, then deleted completely the folder C:\Users{myname}\AppData\Local\Microsoft\VisualStudio\14.0, then I disinstalled and then reinstalled VS from scratch. I still had the error.
When I tried to build the solution, in the error windows I Only saw a generic 'Object reference not set to an instance of an object', but if I examined build output window, I could see that the problem was in effect related to the xaml parser (precisely: Xaml Internal Error WMC9999).
There's not so much on the internet about this issue, a part for a few help request by people facing the same situation, but the only two pages I managed to found that do offer a solution both linked this problem to the use of (different) unsupported functionality in xaml (code that is syntactically correct but unsupported by the target platform): see here and here.
I finally managed to get rid of the errors simply by undoing the last changes with my source control repository. Unfortunately, I had made a lot of change in code, so I can't say which was the exact line of code that caused the problem in my case. But the takeaway of this story is that every evidence points to the fact that it was something related to my xaml code. So, even if, of course, one can't exclude that sometimes there could be a major, system-wide issue with the xaml parser, I would advise other people facing this problem first to quickly undo the last changes and see if the problem disappears.
I'm trying to get a ScrollView to logical scroll instead of physically. After reading up online on how to do this, most sources say to set the CanContentScroll property to False. However, when attempting to do this, it seems that ScrollViewer doesn't have this property.
Here is my XAML code:
<ScrollViewer x:Name="TestScroll" CanContentScroll="True" HorizontalScrollBarVisibility="Disabled" HorizontalScrollMode="Disabled" Margin="66,215,1020,10" Grid.Row="1">
<StackPanel x:Name="TestPanel" Orientation="Vertical">
</StackPanel>
</ScrollViewer>
And the error(s) thrown:
Error 1 The member "CanContentScroll" is not recognized or is not accessible.
Error 2 The property 'CanContentScroll' was not found in type 'ScrollViewer'
I am developing for Windows 8.1, creating a universal app. I feel like I'm missing something like a reference or something incredibly simple because everywhere else that I have looked, it just works.
Any help would be appreciated.
Figured it out, using the VerticalSnapPointsType="Mandatory" property for ScrollViewer instead.
I'm trying to use the ListPicker controller with ListPickerMode="Full", to get the fullscreen pick window. However it just generate an error when i try
"A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll
Additional information: Set property Microsoft.Phone.Controls.ListPicker.ListPickerMode threw an exception. [Line: 49 Position: 57]"
Here's my code:
<toolkit:ListPicker x:Name="OutputSelector" ListPickerMode="Full"
Margin="0,542,6,0" Header="Output Type" Width="450" VerticalAlignment="Top" />
I populate my ListPicker in C# using a list to set as ItemSource if that is any help.
Another thing is that when i try to write "ListPickerMode" in xml it doest give it as an option, but when i have written the whole thing it suggest "Full" "expanded" and "Normal".
If i add 5 items to the ListPicker it automatically uses FullMode, and i have tried changing ItemCountThreshold="0" but that just generate more errors.
I'm using Windowns Phone 7.1 OS 2011 aug release.
It's probably just me that is stupid, first day with Windows Phone programing :)
UPDATE!
Well it looks like ItemCountThreshold & ListPickerMode was remove for 7.1 or something, atleast in XAML part, not the C# part, where they are read only.
Solution for my problem!
<toolkit:ListPicker x:Name="OutputSelector" ExpansionMode="FullScreenOnly"
Margin="0,542,6,0" Header="Output Type" Width="450" VerticalAlignment="Top" />
The ExpansionMode will make the Listpicker appear in fullscreen or expanded.
As stated in the issue tracker of the silverlight toolkit [1], ItemCountThreshold should not be set (and cannot be set using simple xaml).
However, there are two workarounds for this issue. If you you don't mind to use codebehind, set the property via SetValue:
//e.g., in the constructor, just after InitializeComponent();
ListPicker.SetValue(Microsoft.Phone.Controls.ListPicker.ItemCountThresholdProperty, 0);
To set the value in xaml, you can use a binding:
<toolkit:ListPicker ItemCountThreshold="{Binding Hugo,FallbackValue=0}">(...)
In this example, I use a bogus binding expression and set the value using FallbackValue. Of course, an actual working binding should work as well. The xaml approach was only tested on the WP8 SDK, however it should work on 7.1 as well.
Edit: Just found out that the xaml approach breaks the designer.
[1] http://silverlight.codeplex.com/workitem/9742
The solution proposed by the author (moving here for better visibility):
Well it looks like ItemCountThreshold & ListPickerMode was remove for
7.1 or something, atleast in XAML part, not the C# part, where they are read only.
Solution for my problem!
<toolkit:ListPicker x:Name="OutputSelector" ExpansionMode="FullScreenOnly"
Margin="0,542,6,0" Header="Output Type" Width="450" VerticalAlignment="Top" />
The ExpansionMode will make the Listpicker appear in fullscreen or
expanded.
I'm new to WPF programming and decided to give it a shot by trying out some ribbon control libraries.
The library that looks best for now is the Microsoft RibbonControlsLibrary. You can get it on the ribbon licensing page.
So far I've started a new project, added the control to the windows, but them I'm stuck: This is the code so far:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="808" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:my1="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" WindowStyle="SingleBorderWindow">
<Grid>
<my1:Ribbon HorizontalAlignment="Left" Name="ribbon1" VerticalAlignment="Top" Height="165" Width="786" ShowQuickAccessToolbarOnTop="False" WindowIconVisibility="Visible" DataContext="{Binding}" Margin="0,-20,0,0">
<my1:Ribbon.ApplicationMenu>
<my1:RibbonApplicationMenu Visibility="Hidden" IsEnabled="True" />
</my1:Ribbon.ApplicationMenu>
<my1:RibbonTab Label="Tab1" Name="rtab1" >
</my1:RibbonTab>
<my1:RibbonTab Label="tab2" Name="rtab2"/>
</my1:Ribbon>
</Grid>
</Window>
Questions:
1) Where can I find samples for this ribbon control? I've tried googling, but came up with nothing useful.
2) How to add items to specific ribbon tabs? I'm lost in all these properties in the property grid. So far I havent found a designer for that purpose.
3) How can I switch the designer to show me what icons/button/... I placed on TabPage2?
(FYI: The fluent ribbon library does not seem to work for me, because I can't get rid of the ApplicationMenu.)
Found a great sample/tutorial:
http://windowsclient.net/downloads/folders/hands-on-labs/entry76491.aspx
The sample provides a manual with explanations and some test projects with step by step instructions to implement the ribbon control.
Though I'm totally new to WPF, I managed to extract necessary classes from the sample to provide a ribon based menu in my program.