How to register a DependencyProperty that shall work with a value type - winrt-xaml

In a C++/CX class I want to create a DependencyProperty that gets or sets a normal C++ float value type but I just cannot figure out how I need to specify the type that is expected by the DependencyProperty::Register.
Let me make an example. If I were to use a property that works with a reference type I'd put the following into the header file of my C++/CX class:
public:
static property Windows::UI::Xaml::DependencyProperty^ BackgroundColorProperty
{
Windows::UI::Xaml::DependencyProperty^ get();
};
property Windows::UI::Xaml::Media::Brush^ BackgroundColor
{
Windows::UI::Xaml::Media::Brush^ get();
void set(Windows::UI::Xaml::Media::Brush^ value);
};
private:
static Windows::UI::Xaml::DependencyProperty^ _backgroundColorProperty;
static void OnBackgroundColorChanged(Windows::UI::Xaml::DependencyObject^ d, Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ e);
The implementation would look like this:
DependencyProperty^ MyCustomClass::BackgroundColorProperty::get()
{
return _backgroundColorProperty;
}
Windows::UI::Xaml::Media::Brush^ MyCustomClass::BackgroundColor::get()
{
return static_cast<Windows::UI::Xaml::Media::Brush^>(GetValue(BackgroundColorProperty));
}
void MyCustomClass::BackgroundColor::set(Windows::UI::Xaml::Media::Brush^ value)
{
SetValue(BackgroundColorProperty, value);
}
DependencyProperty^ MyCustomClass::_backgroundColorProperty =
DependencyProperty::Register("BackgroundColor",
Brush::typeid,
MyCustomClass::typeid,
ref new PropertyMetadata(ref new SolidColorBrush(Windows::UI::ColorHelper::FromArgb(255, 255, 255, 255)), ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));
void MyCustomClass::OnBackgroundColorChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
{
MyCustomClass^ myClass = static_cast<MyCustomClass^>(d);
// Do whatever needs to be done
}
This all works. Visual Studio's XAML Designer recognizes that property and everything works as expected.
But lets say I want a property that uses a float instead of the Brush. How would I need to specify the necessary type in the DependencyProperty::Register method? I mean the second argument in the code below.
DependencyProperty^ MyCustomClass::_backgroundColorProperty =
DependencyProperty::Register("BackgroundColor",
/* How do I specify the type of the float here?*/,
MyCustomClass::typeid,
ref new PropertyMetadata(1.0f, ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));
I have tried the following which didn't work:
ref new TypeKind(TypeKind::Primitive, float32)
typeof(float32)
typeof(float)
typeid(float)
ref new Windows::UI::Xaml::Interop::TypeKind(TypeKind::Primitive, float)
ref new Windows::UI::Xaml::Interop::TypeKind(TypeKind::Primitive, typeid(float))

You can directly use float::typeid to register float type.
DependencyProperty^ MyCustomClass::_backgroundColorProperty =
DependencyProperty::Register("BackgroundColor",
float::typeid,
MyCustomClass::typeid,
ref new PropertyMetadata(1.0f, ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));
In addition, from this document, it mentions:
If you want a DP with a floating-point type, then make it double
(Double in MIDL 3.0). Declaring and implementing a DP of type float
(Single in MIDL), and then setting a value for that DP in XAML markup,
results in the error Failed to create a 'Windows.Foundation.Single'
from the text ''.
So I suggest you to use double dependency property instead of float.

Related

Assigning default value to T:Float type parameter

I have a class that I wanted dynamic on what type to accept, but still be of type float. I have added an example class below. Simply put, I want a class that can contain either Ints or Floats (or abstracts(Float)), but the type parameter doesn't like being assigned something that should actually fit it.
class Container<T:Float>
{
public function new(aValue:T = 0.0)
{
}
public function example():T
{
return 16.0;
}
In this example, I get two compiler errors. The fist one is the default value of the constructor new(aValue:T = 0.0. A simple fix is to set the value as dynamic, but I like my code neater than this. The second error is in the return value of example(). It won't let me return 16.0, as it is not a T instance.
My question: Is this doable and, if not, should I either use different class definitions for every type?
I think the issue here is that you don't really need the generic type "T".
Here's what I came up with given your constraints. The class "Container" is not generic, and merely contains a Float constructor. This still allows it, however, to accept any value that can be implicitly cast to Float, which includes any abstract as long as they define casting rules.
package ;
class Main
{
public static function main()
{
new Container(); // default
new Container(1); // Int
new Container(2.3); // Float
new Container(new UnifiesWithFloat(4.5)); // Float abstract
}
}
class Container
{
public function new(aValue:Float = 0.8)
{
trace('aValue is $aValue');
}
}
abstract UnifiesWithFloat(Float) from Float to Float
{
inline public function new(value:Float)
{
this = value;
}
}
The only way I could come up for this issue with a cast and with own resolving of the optional parameters.
class Test {
static function main() {
$type(new Container(1));
$type(new Container(1).example());
new Container(1).example();
$type(new Container(1.0));
$type(new Container(1.0).example());
new Container(1.0).example();
}
}
class Container<T:Float> {
public var value:T;
public function new(aValue:T) {
this.value = cast (aValue != null ? aValue : 0);
}
public function example():T {
return cast 16;
}
}
This logs:
Test.hx:3: characters 14-30 : Warning : Container<Int>
Test.hx:4: characters 14-40 : Warning : Int
Test.hx:7: characters 14-32 : Warning : Container<Float>
Test.hx:8: characters 14-42 : Warning : Float

Type parameters - get concrete type from type T : IMyInterface

Suppose I have a List<IMyInterface>...
I have three classes which implement IMyInterface: MyClass1, MyClass2, and MyClass3
I have a readonly Dictionary:
private static readonly Dictionary<Type, Type> DeclarationTypes = new Dictionary<Type, Type>
{
{ typeof(MyClass1), typeof(FunnyClass1) },
{ typeof(MyClass2), typeof(FunnyClass2) },
{ typeof(MyClass3), typeof(FunnyClass3) },
};
I have another interface, IFunnyInteface<T> where T : IMyInterface
I have a method:
public static IFunnyInterface<T> ConvertToFunnyClass<T>(this T node) where T : IMyInterface
{
if (DeclarationTypes.ContainsKey(node.GetType())) {
IFunnyInterface<T> otherClassInstance = (FunnyInterface<T>) Activator.CreateInstance(DeclarationTypes[node.GetType()], node);
return otherClassInstance;
}
return null;
}
I'm trying to call the constructor of FunnyClasses and insert as parameter my MyClass object. I don't want to know which object it is: I just want to instantiate some FunnyClass with MyClass as a parameter.
What happens when I call ConvertToFunnyClass, T is of type IMyInterface, and when I try to cast it to FunnyInterface<T>, it says I can't convert FunnyClass1, for instance, to FunnyInterface<IMyInterface>
My current workaround (not a beautiful one), is this:
public static dynamic ConvertToFunnyClass<T>(this T node) where T : IMyInterface
{
if (DeclarationTypes.ContainsKey(node.GetType())) {
var otherClassInstance = (FunnyInterface<T>) Activator.CreateInstance(DeclarationTypes[node.GetType()], node);
return otherClassInstance;
}
return null;
}
And I don't like it because the return type is dynamic, so when I access it from somewhere else, I have no idea what type it is, and I lose intellisense, and stuff. I don't know about any performance implications either.
Any clues?
Thanks in Advance!
Resolution
As I'm using C# 4.0, I could stop casting errors using covariance (output positions only), and so I changed my IFunnyInterface to
IFunnyInteface<out T> where T : IMyInterface
Thank you all for the replies.
Essentially, your problem is that you are trying to convert FunnyInterface<T> to FunnyInterface<IMyInterface>. As has been mentioned several times (one example is here, more information here), this is not valid in most circumstances. Only in .NET 4, when the generic type is an interface or delegate, and the type parameter has been explicitly declared as variant with in or out, can you perform this conversion.
Is FunnyInterface actually an interface?
thecoop answer points you exactly to why you can't do it.
A cleaner solution to the problem (besides using dynamic) would be a base non-Generics Interface:
public interface IFunnyInterfaceBase
{
}
public interface IFunnyInteface<T> : IFunnyInterfaceBase
where T : IMyInterface
{
}
And you need to move methods signature you use in that code from IFunnyInteface to IFunnyInterfaceBase.
This way you would be able to write something like this:
MyClass2 c2 = new MyClass2();
IFunnyInterfaceBase funnyInstance = c2.ConvertToFunnyClass();
The Exception you said you got in your code is not due to the extension method signature itself (the method is fine)..it is originated by the type of your lvalue (the type of the variable you use to store its return value)!
Obviously this solution applies only if you can modify IFunnyInterface source code!

How to return a vector of objects to managed code efficiently?

I have a ref class that contains a pointer to an unmanaged class. the class has some basic types and also a vector of objects of another class. I would like to know the best way to get and set the vector from managed code. Will a memcpy between unmangedb objects be efficient or setting each member variable of unmanagedb?
for ex (assume the class is complete. I am writing what is relevant to the question)
Assume we already have a managed wrapped for struct UnmanagedB called B.
struct UnmanagedA
{
int a;
vector<UnmanagedB> list;
};
public ref class A : public System::IDisposable
{
public:
// properties
property System::UInt32 a
{
System::UInt32 get();
void set(System::UInt32 value);
}
property array<B^>^ list
{
System::array<B^>^ get(); // what is the best way to set and get the vector
void set(array<B^>^ value);
}
private:
UnmanagedA* obj1;
};
This obviously won't be cleanly possible, since UnmanagedA contains a vector of UnmanagedB values, while A exposes an property of type array<B^>. If this is intended and not a typo, you will need to marshall the content of B^ into instances of UnmanagedB. Otherwise, let UnmanagedA hold a std::vector< B* > and take care of proper lifetime management.

C# Func(T) not accepting ref type input parameter

Can Func<...> accept arguments passed by reference in C#?
static void Main()
{
Func<string,int, int> method = Work;
method.BeginInvoke("test",0, Done, method);
// ...
//
}
static int Work(ref string s,int a) { return s.Length; }
static void Done(IAsyncResult cookie)
{
var target = (Func<string, int>)cookie.AsyncState;
int result = target.EndInvoke(cookie);
Console.WriteLine("String length is: " + result);
}
I am not able define a Func<...> which can accept the ref input parameter.
The Func<T> delegates cannot take ref parameters.
You need to create your own delegate type which takes ref parameters.
However, you shouldn't be using ref here in the first place.
Expanding on SLaks answers.
The Func<T> family of delegates are generic and allow you to customize the type of the arguments and returns. While ref contributes to C#`s type system it's not actually a type at the CLR level: it's a storage location modifier. Hence it's not possible to use a generic instantiation to control whether or not a particular location is ref or not.
If this was possible it would be very easy to produce completely invalid code. Consider the following
T Method<T>() {
T local = ...;
...
return local;
}
Now consider what happens if the developer called Method<ref int>(). It would produce both a local and return value which are ref. This would result in invalid C# code.

Can extension methods modify extended class values?

I was just trying to code the following extension method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4Testing
{
static class ExtensionMethods
{
public static void AssignMe(this int me, int value)
{
me = value;
}
}
}
But it is not working, i mean, can I use an extension method to alter values from extended classes? I don't want to change void return type to int, just changing extended class value. Thanks in advance
Your example uses int, which is a value type. Classes are reference types and behaves a bit differently in this case.
While you could make a method that takes another reference like AssignMe(this MyClass me, MyClass other), the method would work on a copy of the reference, so if you assign other to me it would only affect the local copy of the reference.
Also, keep in mind that extension methods are just static methods in disguise. I.e. they can only access public members of the extended types.
public sealed class Foo {
public int PublicValue;
private int PrivateValue;
}
public static class FooExtensions {
public static void Bar(this Foo f) {
f.PublicValue = 42;
// Doesn't compile as the extension method doesn't have access to Foo's internals
f.PrivateValue = 42;
}
}
// a work around for extension to a wrapping reference type is following ....
using System;
static class Program
{
static void Main(string[] args)
{
var me = new Integer { value = 5 };
int y = 2;
me.AssignMe(y);
Console.WriteLine(me); // prints 2
Console.ReadLine();
}
public static void AssignMe(this Integer me, int value)
{
me.value = value;
}
}
class Integer
{
public int value { get; set; }
public Integer()
{
value = 0;
}
public override string ToString()
{
return value.ToString();
}
}
Ramon what you really need is a ref modifier on the first (i.e. int me ) parameter of the extension method, but C# does not allow ref modifier on parameters having 'this' modifiers.
[Update]
No workaround should be possible for your particular case of an extension method for a value type. Here is the "reductio ad absurdum" that you are asking for if you are allowed to do what you want to do; consider the C# statement:
5.AssignMe(10);
... now what on earth do you think its suppose to do ? Are you trying to assign 10 to 5 ??
Operator overloading cannot help you either.
This is an old post but I ran into a similar problem trying to implement an extender for the String class.
My original code was this:
public static void Revert(this string s)
{
char[] xc = s.ToCharArray();
s = new string(xc.Reverse());
}
By using the new keyword I am creating a new object and since s is not passed by reference it will not be modified.
I changed it to the following which provides a solution to Ramon's problem:
public static string Reverse(this string s)
{
char[] xc = s.ToCharArray();
Array.Reverse(xc);
return new string(xc);
}
In which case the calling code will be:
s = s.Reverse();
To manipulate integers you can do something like:
public static int Increment(this int i)
{
return i++;
}
i = i.Increment();

Resources