I have a class library with extension methods and I'm trying to write an extension method that works with a Window class, from a WPF app.
When I try writing the extension to accept a parameter of type Window, I get an error that this type can't be found;
I can't seem to find an assembly to import that will resolve this error, so I thought I'd post it here to see if I'm doing something wrong.
As an example, here's a simple extension I'd like to be able to use:
<Extension>
Public Sub helloworld(window As System.Windows.Window) 'Error- type system.windows.window is not defined
...
End Sub
But I'm getting the error that the type system.windows.window is not defined.
I've even tried adding a reference to this class library to the System.Windows assembly, and adding an imports at the top of the screen, but it doesn't help.
I ended up taking a look at the references used on a normal WPF app project, and realized that there's a reference to PresentationFramework.... which is verbiage similar to...Windows Presentation Framework (WPF) ;)
I added this reference to my class library, and suddenly everything just worked.
Related
I am getting this error in a WPF app that is built with F#, FsXaml, and Elmish.WPF.
System.Xaml.XamlObjectWriterException:
Cannot create unknown type '{http://schemas.componentone.com/winfx/2006/xaml}C1ProgressIndicator'.
at System.Xaml.XamlObjectWriter.WriteStartObject(XamlType xamlType)
at FsXaml.InjectXaml.from(String file, Boolean loadFromResource, Object root)
A Views project contains XAML and includes a reference to C1.WPF.4.dll.
The XAML declares an instance of a control from C1.WPF.4.dll.
However, there is no F# code that uses any types from C1.WPF.4.dll.
Consequently, Views.dll does not contain a reference to C1.WPF.4.dll, as confirmed by IL Spy.
Consequently, when I build App.exe, C1.WPF.4.dll is not included in the output folder.
Consequently, I get a run-time error.
While I could include a reference to C1.WPF.4.dll in my App project, I would rather not.
Is there a better way to do this?
To get this to work:
In your XAML file, replace the XAML namespace with a CLR namespace. For example, instead of xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" use something like xmlns:c1="clr-namespace:C1.WPF;assembly=C1.WPF.4".
This is required because when App.exe is running, it may not have loaded the controls library yet. It is in the controls library that an attribute creates the mapping from XAML namespace to CLR namespace. By explicitly stating the CLR namespace and assembly, the XamlReader can find the class to instantiate.
Create a dummy instance in the Views code. The dummy instance is an instance of something from the controls library. This creates the dependency in Views.dll on the controls library.
// force the compiler to add a reference to C1.WPF.4.dll
let private forceC1DllReference = C1.WPF.Anchor.TopLeft
type MainWindowView = XAML< "MainWindow.xaml" >
This second one feels like a hack. I wonder if there is a better way to say, "Please add a reference to this DLL even if I'm not using types from it."
I'm following a tutorial from here:
http://www.wpftutorial.net/LocalizeMarkupExtension.html
and everything works fine....Untill I devide my code in to different projects
So I have a class (TranslateExtension) that inherits from MarkupExtension. this allows me to add markup like this to WPF:
<TextBlock Text="{Mynamespace:Translate key1}" Margin="8" ></TextBlock>
and the textblock gets filled in with the necessary text
But when i want to put the front end in a different project I get the following exceptions:
The name "Translate" does not exist in the namespace
The type "Translate" was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
The tag "Translate" does not exist in XML namespace ....
I already have added a reference to the namespace on top of my XAML file and the namespace exists inside of the new project. I also have a reference from my Front end to my old project.
So everything works fine untill I extract the front end in to a different project....
IMPORTANT: If I take TranslateExtensions and Put it in the front end, with everything else in the old project, things magically work...
any ideas?
XAML namespaces starting with clr-namespace: are specific to an assembly. If you don't specify the assembly name, the current assembly is assumed.
The simplest option will be to use an assembly-qualified namespace:
xmlns:MyNamespace="clr-namespace:MyNamespace;assembly=MyAssembly"
For a more robust solution, you might want to look at the XmlnsDefinitionAttribute, which can be used to map multiple CLR namespaces across different assemblies to a single XML namespace URI.
I'm building a silverlight MVVM app (using the GalaSoft MVVM light template). The app has some UserControls. Now I have stumbeld across somthing which seems like a namespace issue. The namespaces are defined in this way:
MyApp.Controls
-> my user controls
MyApp.View
-> different pages of the app (which use the user controls)
MyApp
-> the namespace of the main page (root namespace)
This works ok as long as I don't give any of the UserControls an x:Name. As soon as an x:Name is defined, the build breaks with the following message:
error CS0426: The type name 'Controls' does not exist in the type 'MyApp.MyApp'
That is realy strange! I can resolve this issue by changing the namespace MyApp.Controls to MyAppControls or by manipulating the generated code, so that the direct reference MyApp.Controls.MyControl is replaced with using MyApp.Controls and then instanciate the control with MyControl (however this will be overwritten again, as soon as I switch to the desgin view).
Does anyone know the reason for this strange behaviour? I would have expected that this is a very common problem?
I think I found the reason: I also had a resource file with the name MyApp. This led to the generation of a class MyApp and therefore the compiler got confused between the class MyApp and the namespace MyApp. I have renamed the Resource file to something different and now the project compiles again.
I'm trying to use Windows From as ActiveX control inside SAP B1. Inside that control I host WPF control. It works great except when I try to cast activeX object onto my class so I can use all the methods on my class. But when I try to do that I get an exception. What is wrong?
I did registration and all that stuff. I can even display that control inside SAP with some handcrafted data (initialized in control constructor). Although I'm not sure if this is more general problem and not SAP-specific.
Welcome any suggestions and thanks in advance.
The message I got is:
Unable to cast COM object of type 'System.__ComObject' to class type 'CapacityCalendarControl.CapacityCalendar'
[Guid("9A2F1CCB-8DC0-4CE5-B3C7-403A1A8CDCE2")]
[ProgId("kamil.calendar.control")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public partial class CapacityCalendar : UserControl
{
//internals...
}
A System.__ComObject (a COM wrapper) cannot be cast to a .NET class. The simplest way for you is to use TLBImport (Type Library Import Tool), I think, if your activeX is equipped with a .TLB (external or embedded).
See here for more: Tool for creating .NET wrappers for a COM DLL?
I have a problem with creating the instance of a class in xaml file. I thought you can do it like this:
<src:MyDataClass x:Key="data"/>
in the resource part of the user control and then use it in the xaml file (for example bind to it).
But even tough the class I created is located in the same namespace it says that:
"The type was not found. Verfiy that all assemblies were built, etc".
How to get it right? is there another method?
Thanks for suggestions.
Well obviously you have to add the right namespace to your xaml. The namespace which contains the class you want instantiate. After reading this, you should be able to solve your problem.