custom attached property not found - xaml

I want to implement a new property (named "MenuForeground") for the pivot element, in order change the color of the PivotItem header through a defined ControlTemplate.
Therefore I created a new class for the custom property, added the #include in the needed code-behind xaml.h file and defined a new namespace ("xamlns:cap") according to the namespace of the custom property.
PivotProperties.h
#pragma once
using namespace Windows::UI::Xaml;
namespace CustomAttachedProperties
{
public ref class PivotProperties sealed : Windows::UI::Xaml::DependencyObject
{
public:
static Windows::UI::Color GetMenuForeground(UIElement^ obj);
static void SetMenuForeground(UIElement^ obj, Windows::UI::Color value);
static property DependencyProperty^ MenuForegroundProperty
{
DependencyProperty^ get() { return _menuForegroundProperty; }
}
private:
static DependencyProperty^ _menuForegroundProperty;
};
}
PivotProperties.cpp
#include "pch.h"
#include "PivotProperties.h"
using namespace CustomAttachedProperties;
DependencyProperty^ PivotProperties::_menuForegroundProperty = DependencyProperty::RegisterAttached(
"MenuForeground",
Windows::UI::Color::typeid,
Windows::UI::Xaml::Controls::Pivot::typeid,
ref new PropertyMetadata(false));
Windows::UI::Color PivotProperties::GetMenuForeground(UIElement^ obj)
{
return (Windows::UI::Color)obj->GetValue(_menuForegroundProperty);
}
void PivotProperties::SetMenuForeground(UIElement^ obj, Windows::UI::Color value)
{
obj->SetValue(_menuForegroundProperty, value);
}
In order to use the new property for a pivot element I declared a new xml namespace in the root element like the following
<Page
// ...
xmlns:cap="clr-namespace:CustomAttachedProperties">
But if I try to use the new property ...
<Pivot x:Name="pivot" cap:PivotProperties.MenuForeground="Red">...</Pivot>
... an error pops up, saying: "The attachable property 'MenuForeground' was not found in type 'PivotProperties'.
How to fix that?

The third parameter ownerType of the RegisterAttached method must be
The owner type that is registering the dependency property
not the type of the object where you want to set the property.
So your declaration should look like this:
DependencyProperty^ PivotProperties::_menuForegroundProperty =
DependencyProperty::RegisterAttached(
"MenuForeground",
Windows::UI::Color::typeid,
PivotProperties::typeid, // here
ref new PropertyMetadata(false));
Please note also that it is not necessary that your PivotProperties class derives from DependencyObject, as long as it only declares attached properties.
You may also consider using Windows.UI.Xaml.Media.Brush as property type to make it conforming with other properties like Background and Foreground.

Related

How to use other class enum properties in Qml

Given is a class ClassA which contains an enum:
class ClassA : public QObject
{
Q_OBJECT
QML_ELEMENT
public:
enum EnumClassA {
};
Q_ENUM(EnumClassA)
};
A second class has a property of ClassA::EnumClassA:
#include <ClassA.h>
class ClassB : public QObject
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(ClassA::EnumClassA value READ ...)
public:
};
This works in Qml (meaning the property value can be used in Qml), but when using qmlint/qmlsc compiler the following message is shown:
error: Could not compile binding for value: Cannot generate efficient code for content pointer of non-QVariant wrapper type double of ClassB::value with type ClassA::EnumClassA
A dirty fix could be this:
// replace "Q_PROPERTY(ClassA::EnumClassA value READ ...)" with:
Q_PROPERTY(int value READ ...)
but it would be much nicer to keep the type in the property.
Is it possible to modify the enum so that it can be compiled?
Regards,

Can I use a bool type with XAML X:Static?

I am trying to make a menu item's visibility conditioned on the DEBUG macro. (It's a debug menu item and so should not be visible in a release build.) I am trying to follow the x:Static example in https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions
I have the following in my AppShell Class:
namespace myappname
{
public partial class AppShell : Xamarin.Forms.Shell
{
#if DEBUG
public static readonly bool IsDebug = true;
#else
public static readonly bool IsDebug = false;
#endif
In the AppShell XAML I have tried numerous variations of
<FlyoutItem Title="Debug" Icon="DebugCheckedTests_16x.png" IsVisible="{x:Static local:bool.IsDebug}">
But everything I have tried either gives me the error XFC0000 Cannot resolve type "bool" -- or some other error.
Am I way off base, or is bool not a valid type for x:Static? How do I code this?
From the docs you linked what you are trying to accomplish comes under "a public static field", thus you need to specify the class where the field IsDebug is defined, in the docs example the class name is AppConstants, it is not a matter of "bool" type you can use any type as long as it is the type expected by the property.
xmlns:localRoot="clr-namespace:myappname"
<FlyoutItem Title="Debug" Icon="DebugCheckedTests_16x.png"
IsVisible="{x:Static localRoot:AppShell.IsDebug}>

Ignore the Binding initialization

The inital problem is coming from a personal project about the polyline of the Xamarin.Forms.Map where the initialization is realized by a binding from the XAML part..
Let me be clear by an example :
I have an object CustomMap.cs which inherit from Xamarin.Forms.Map (This file is in the PCL part -> CustomControl/CustomMap.cs)
public class CustomMap : Map, INotifyPropertyChanged
{
public static readonly BindableProperty PolylineAddressPointsProperty =
BindableProperty.Create(nameof(PolylineAddressPoints), typeof(List<string>), typeof(CustomMap), null);
public List<string> PolylineAddressPoints
{
get { return (List<string>)GetValue(PolylineAddressPointsProperty); }
set
{
SetValue(PolylineAddressPointsProperty, value);
this.GeneratePolylineCoordinatesInner();
}
}
// ...
}
As you can see, I have a bindable property with an assessor and the XAML doesn't seem to use this assessor..
So the MainPge.xaml part of the page, where the control is called, looks like that:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:control="clr-namespace:MapPolylineProject.CustomControl;assembly=MapPolylineProject"
x:Class="MapPolylineProject.Page.MainPage">
<ContentPage.Content>
<control:CustomMap x:Name="MapTest" PolylineAddressPoints="{Binding AddressPointList}"
VerticalOptions="Fill" HorizontalOptions="Fill"/>
</ContentPage.Content>
</ContentPage>
The MainPge.xaml.cs part:
public partial class MainPage : ContentPage
{
public List<string> AddressPointList { get; set; }
public MainPage()
{
base.BindingContext = this;
AddressPointList = new List<string>()
{
"72230 Ruaudin, France",
"72100 Le Mans, France",
"77500 Chelles, France"
};
InitializeComponent();
//MapTest.PolylineAddressPoints = AddressPointList;
}
}
So, everything is fine if I edit the PolylineAddressPoints from the object instance (if the commented part isnt' commented..), but if I init the value from the XAML (from the InitializeComponent();), it doesn't work, the SetValue, in the Set {}, of the CustomMap.PolylineAddressPoints, isn't called..
I then searched on the web about it and get something about the Dependency Properties? or something like that. So I tried some solutions but, from WPF, so some methods, such as DependencyProperty.Register();. So yeah, I can't find the way to solve my problem..
I also though about something, if DependencyProperty.Register(); would exists in Xamarin.Forms, then it means I would have to do it for each values? Because, if every value has to be set by a XAML binding logic, it would not work, I would have to register every value, doesn't it?
I'm sorry if I'm not clear, but I'm so lost about this problem.. Please, do not hesitate to ask for more details, thank in advance !
Finaly, the initial problem is that I'm trying to set a value of an object/control, from the XAML. Doing this by a Binding doesn't work, it seems like it ignored.. However, it does work if I do the following:
MapTest.PolylineAddressPoints = AddressPointList;
There are multiple questions in this:
Why is the property setter never called when using Xaml ?
Am I properly defining my BindableProperty ?
Why is my binding failing ?
Let me answer them in a different order.
Am I properly defining my BindableProperty ?
The BindableProperty declaration is right, but could be improved by using an IList<string>:
public static readonly BindableProperty PolylineAddressPointsProperty =
BindableProperty.Create(nameof(PolylineAddressPoints), typeof(IList<string>), typeof(CustomMap), null);
but the property accessor is wrong, and should only contains this:
public IList<string> PolylineAddressPoints
{
get { return (IList<string>)GetValue(PolylineAddressPointsProperty); }
set { SetValue(PolylineAddressPointsProperty, value); }
}
I'll tell you why while answering the next question. But you want to invoke a method when the property has changed. In order to do that, you have to reference a propertyChanged delegate to CreateBindableProperty, like this:
public static readonly BindableProperty PolylineAddressPointsProperty =
BindableProperty.Create(nameof(PolylineAddressPoints), typeof(IList<string>), typeof(CustomMap), null,
propertyChanged: OnPolyLineAddressPointsPropertyChanged);
And you have to declare that method too:
static void OnPolyLineAddressPointsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((CustomMap)bindable).OnPolyLineAddressPointsPropertyChanged((IList<string>)oldValue, (IList<string>)newValue);
}
void OnPolyLineAddressPointsPropertyChanged(IList<string> oldValue, IList<string> newValue)
{
GeneratePolylineCoordinatesInner();
}
Why is the property setter never called when using Xaml ?
The property, and the property accessors, are only meant to be invoked when accessing the property by code. C# code.
When setting a property with a BindablePrperty backing store from Xaml, the property accessors are bypassed and SetValue() is used directly.
When defining a Binding, both from code or from Xaml, property accessors are again bypassed and SetValue() is used when the property needs to be modified. And when SetValue() is invoked, the propertyChanged delegate is executed after the property has changed (to be complete here, propertyChanging is invoked before the property change).
You might wonder why bother defining the property if the bindable property is only used by xaml, or used in the context of Binding. Well, I said the property accessors weren't invoked, but they are used in the context of Xaml and XamlC:
a [TypeConverter] attribute can be defined on the property, and will be used
with XamlC on, the property signature can be used to infer, at compile time, the Type of the BindableProperty.
So it's a good habit to always declare property accessors for public BindableProperties. ALWAYS.
Why is my binding failing ?
As you're using CustomMap as both View and ViewModel (I won't tell the Mvvm Police), doing this in your constructor should be enough:
BindingContext = this; //no need to prefix it with base.
As you're doing it already, your Binding should work once you've modified the BindableProperty declaration in the way I explained earlier.

How to properly use a forward declared class in c++ cli?

I am getting error code C2512 which is no appropriate default constructor available. However it appears to me that everything should be working so I am a bit confused on how to get this to compile.
Here is the header file code:
namespace MyNamespace
{
ref class ForwadDeclaredClass;
public ref class MyClass
{
public:
MyClass();
// ...
private:
ForwadDeclaredClass^ fc;
}
}
Now in my cpp file I define the forwarded class and try to use it in the constructor for MyClass.
using namespace MyNamespace;
//...
public ref class ForwardDeclaredClass
{
public:
ForwardDeclaredClass()
{
}
}
MyClass::MyClass()
{
// Compiler complains with error code here
fc = gcnew ForwardDeclaredClass();
}
I know I have to be missing something simple but I am just not seeing it. Any help would be appreciated.
At first glance, it looks like your namespaces are off.
In the header file, you're declaring ForwardDeclaredClass and MyClass inside MyNamespace.
In the cpp file, you're using MyNamespace, but the code you're writing isn't inside the namespace.
It looks like you need to enclose most of the contents of the cpp file in a namespace MyNamespace { block.

VisualStateManager and MVVM

I'm working on a Windows 8 Store App (using the Grid App Template) and while I'm loading data from a server I want to show a ProgressRing and hide the GridView or ListView (depends on if the app is snapped or not) that will display the data once it is fully loaded.
The issue is that when the ViewModel is loading data I need to be able to change the VisualState.
I found what I thought was a solution Here, but this code will not build.
public class StateManager : DependencyObject
{
public static string GetVisualStateProperty(DependencyObject obj)
{
return (string)obj.GetValue(VisualStatePropertyProperty);
}
public static void SetVisualStateProperty(DependencyObject obj, string value)
{
obj.SetValue(VisualStatePropertyProperty, value);
}
public static readonly DependencyProperty VisualStatePropertyProperty =
DependencyProperty.RegisterAttached(
"VisualStateProperty",
typeof(string),
typeof(StateManager),
new PropertyMetadata((s, e) => //this throws the error
{
var propertyName = (string)e.NewValue;
var ctrl = s as Control;
if (ctrl == null)
throw new InvalidOperationException("This attached property only supports types derived from Control.");
System.Windows.VisualStateManager.GoToState(ctrl, (string)e.NewValue, true);
}));
}
ERROR: Cannot convert lambda expression to type 'object' because it is
not a delegate type
Does anyone know how to get the linked solution to work? Or is there a simpler method that I am completely missing (I'm a XAML newbie!)?
I'm not even sure if the listed solution will work because the "Snapped" vs "Full" states are managed by the base LayoutAwarePage class included with the template.
why not simply use a datatrigger bind to a viewmodel property like IsBusy {get;set;} to enable your Progressring?