Workflow 4.0 and System.Attribute - c#

workflows seem to be created straight from Xaml. How then is would it be possible to include a System.Attribute on my workflow classes?
The only ways i can think of are a bit crap:
Have a corresponding code file for each Activity.xaml:
[MyCustomAttribute("hello")]
public abstract class MyPointlessWorkflowBase : System.Activity
{
}
And then having my .xaml inherit from the base (i don't even know if this is possible)? But this sucks as i have to an extra class for each Workflow that requires the attribute.
is there anyway to code activities like they were normal classes before you slap the .xaml over it?

A XAML file generates a class with a partial keyword before it gets compiled so you can create a partial class with the same name and add the attribute there.
[MyCustomAttribute("hello")]
public partial class MyWorkflow : Activity
{
}
Alternatively you can add an attribute in XAML using the x:ClassAttributes element and add them that way.
<p:Activity x:Class="WorkflowConsoleApplication1.MyWorkflow"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:my="......"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:ClassAttributes>
<my:MyCustomAttribute>
<x:Arguments>
<s:String>hello</s:String>
</x:Arguments>
</my:MyCustomAttribute>
</x:ClassAttributes>
</p:Activity>

Related

Adding Generic Windows Form UserControl in design time [duplicate]

I would like to define the following control:
public partial class ObjectSelectorControl<T> : UserControl where T : class
The problem is that the designer can't resolve this. Is there a workaround to this issue?
This works
public class Control1<T> : UserControl { ... }
public class Control2 : Control1<double> { ... }
public class Control3 : Control2 { ... }
had read it here:
Generic User Controls?
Sounds much like what we do in our project.
There's a base class that is generic:
public partial class controlItemList<TBaseItem, TBaseItemCollection> : UserControl, IUIDispatcher
where TBaseItem : new()
where TBaseItemCollection : IItemCollection<TBaseItem>
Then for each use we define a non-generic version (which still couldn't be used by designer):
public class controlMessagesNonGenericParent : controlItemList<MailItem, MailItemCollection>
{
}
... and then we have derived controls that could be used in designer:
public partial class controlMessages : controlMessagesNonGenericParent
{
...
}
There are some restrictions on what your control can or cannot do in order to be able to use the designer. Fundamentally they all revolve around the designer being able to instantiate your class (must have a parameterless constructor, can't be abstract, etc.). Because the designer has no idea what type to pass as a generic argument (and I doubt this is even a consideration), your class can't be instantiated.
Your best hope would be to create your UserControl and change the constructor to protected (this, I believe, will work, since the designer uses reflection and ignores visibility, but I'm not 100% positive). You can then inherit from that UserControl and create your generic class and call the base (protected) constructor.
I don't know at which point (with which C#/.NET/VS verison update), but now it is possible to create generic control the same way you create any other generic class.
If you create your UserControl in VS the standard way (i.e. by adding it through GUI), you simply add <T> in both parts of class declaration ("base" class code and the designer managed file). Actually, that is what you have in your quoted code.
I don't believe this is possible, because the designer invokes an instance of your class. If you use generics, the designer doesn't know what type to pass into 'T'.
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=105876
The bug has been posted to microsoft's site and you can see that its marked as "Postponed" currently there is no solution !! .
Use composition instead of generics. Instead of using ObjectSelectorControl, give a generic member of another type (Selector<T> maybe) and act on that object instead of trying to force yourself to be generic.

Cannot find the type of subclass in DataType property

In my xaml file for window in wpf project am using binding to the type in such way
<TreeView>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type spec:SectionContainer.SectionNode}">
...
where SectionContainer.SectionNode is subclass of partial class SectionContainer and spec is namespace of current assembly, defined in Window tag as:
xmlns:spec="clr-namespace:Proj.Sections".
There is no error while defining spec, but using SectionNode type in DataType property generates:
Cannot find the type 'spec:SectionContainer.SectionNode'. Note that type names are case sensitive.
I understand what this error means, but there is no reason for it to appear there. May be problem in using of partial class or in something else? All classes are defined as public.
My classes:
namespace Proj.Sections
{
[Serializable]
public partial class SectionContainer : INotifyPropertyChanged
{
[Serializable]
public class SectionNode : SectionNode
{
}
}
}
Ahh... now that you have added your class definitions (please always show your relevant code when you ask your question), I think that I can see your problem. I had a similar situation where Serialization was the problem. For some unknown reason, it doesn't work well with the INotifyPropertyChanged interface.
To get around this problem, my WPF projects all have separate SerializableXXX classes for any objects that need serialization and the classes that are used in the UI and implement the INotifyPropertyChanged interface have no serialization. As a simple test, try removing your SerializableAttributes and see if it makes any difference.
Moving SectionNode subclass out of SectionContainer solves the problem. Before this I also tried to define DataType value in separate <HierarchicalDataTemplate.DataType/> tag, but it led to another error messages. So other ideas how to use subclasses and have no errors in the same time are welcomed.

How do I make these errors go away?

Can you tell me please what I need to change so that I won't get these errors anymore? I've duplicated an xaml+css class and that's why I get so many errors. I've attached an image to show you the errors. Someone recommended me to change something about these 2 things but I didn't quite understand, which is why I've decided to make a new post regarding this matter.
code c1.cs:
public partial class ImagesGrid : UserControl
code xaml c1.xaml:
x:Class="KinectInfoPortal.ImagesGrid"
EDIT1:
Code for C1.xaml.cs class:
public partial class ImagesGrid : UserControl
Code for ImageGrid.xaml.cs class:
public partial class ImagesGrid : UserControl
Code for C1.xaml:
x:Class="KinectInfoPortal.ImagesGrid"
Code for ImageGrid.xaml:
x:Class="Microsoft.Samples.Kinect.ControlsBasics.ImagesGrid"
Here is the mistake:
Code for C1.xaml.cs class:
public partial class ImagesGrid : UserControl
Code for ImageGrid.xaml.cs class:
public partial class ImagesGrid : UserControl
It should be:
Code for C1.xaml.cs class:
public partial class C1: UserControl
Code for ImageGrid.xaml.cs class:
public partial class ImagesGrid : UserControl
You renamed file to C1.xaml but your class name is same as it is for ImagesGird.xaml user control due to which it is saying that its already defined changed class name as well.
You have two class with same name.
1) in ImagesGrid.xaml user control
2) in C1.xaml user control
which is obviuosly wrong.
When you duplicate a xaml/cs pair, you need to take care to change the name of the class in both the XAML file and the codebehind, basically the two lines of code you listed. Make sure that in the original class and in the new class, BOTH are different.

C# / XAML: How to declare a textbox in XAML code that is used in a class

How do I declare a textbox in the XAML code that is used in a class? I copied all the source code from the MainWindow.xaml.cs to another class because the MainWindow is getting to big.
I hope you can help me. Thanks in advance.
The following causes the error:
TextChanged="textBox_1_Kurzbeschreibung_TextChanged"
EDIT: The following statement solved the problem.
~closed.
Simple make the class partial, and then use the same name in the two classes.
public partial class MyClass
{
// code....
}
The other class need to be equal:
public partial class MyClass
{
// code....
}
From MSDN:
"Partial type definitions allow for the definition of a class, struct, or interface to be split into multiple files
See more here: http://msdn.microsoft.com/en-us/library/wbx7zzdd.aspx

override a method using a partial class

I have a class formed by two partial classes.
One created by ORM code generation and one for extensions.
In this particular instance, I need to override one of the properties generated by the partial class because I need to do some validation on it first.
Is it possible to use my extension class to kind of override the property of the code generation partial class?
No, not possible. If you are the owner of the code-generation, you should put in hooks for handling that scenario. For example, sqlmetal.exe for LinqToSql produces partial classes wherein each property setter looks a bit like this:
if (this.myProperty != value)
{
this.OnMyPropertyChanging(value);
this.SendPropertyChanging();
this.myProperty = value;
this.SendPropertyChanged("MyProperty");
this.OnMyPropertyChanged();
}
Of course, the generator also creates those property-specific changing/change methods, but they declare those as partials:
partial void OnMyPropertyChanging(string newValue);
partial void OnMyPropertyChanged();
With this setup, it's obviously quite easy to tap into these events for the extension partial class.

Categories

Resources