locked
What is [Windows::UI::Xaml::Data::Bindable] in front of a class definition

    Question

  • In the file SampleDataSource.h of the "XAML ListView and GridView customizing interactivity sample" sample, the class item is defined as

    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHiddenAttribute]
    public ref class Item sealed
    {
    	Platform::String^  _Title;
    	Platform::String^  _Subtitle;
    	Platform::String^  _Link;
    	Platform::String^  _Category;
    	Platform::String^ _Description;
    	Platform::String^ _Content;
    	ImageSource^ _Image;
    	event PropertyChangedEventHandler^ _PropertyChanged; 
              // lots of other stuff omitted
    };
    

    What is the role of first two lines in this code snippet?
    Sunday, October 14, 2012 10:01 PM

Answers

All replies

  • The WebHostHidden will prevent the class from being seen from JavaScript.

    The Bindable attribute denotes that this class will be able to be used via XAML Binding in markup.  See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.bindableattribute.aspx and http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh758320.aspx


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    • Proposed as answer by Jesse Jiang Monday, October 15, 2012 6:26 AM
    • Marked as answer by Leonard Monday, October 15, 2012 11:27 AM
    Sunday, October 14, 2012 11:27 PM
  • Thanks Tim.

    I have one more question. The code sample in your second link is

    // Create a class that implements INotifyPropertyChanged.
    public class MyColors : INotifyPropertyChanged
    

    But in the "XAML ListView and GridView customizing interactivity sample" sample, the class Item is defined without INotifyPropertyChanged (see code attached in the first post). Why is the keyword "INotifyPropertyChanged" missing?

    Monday, October 15, 2012 11:26 AM
  • Another issue, whenever I add the line [Windows::UI::Xaml::Data::Bindable] before class definition, the VS express 2012 compiler reports errors:

    1>  XamlTypeInfo.g.cpp
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(99): error C2039: 'GraphicsItem' : is not a member of 'metouch'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(99): error C2061: syntax error : identifier 'GraphicsItem'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(118): error C2039: 'GraphicsItem' : is not a member of 'metouch'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(118): error C2065: 'GraphicsItem' : undeclared identifier
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(118): error C2059: syntax error : ')'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(119): error C2227: left of '->Image' must point to class/struct/union/generic type

    My code is

    namespace metouch
    {
        // Uncomment the line below will result in compiler errors C2039
        //[Windows::UI::Xaml::Data::Bindable]
        public ref class GraphicsItem sealed
        {
    	Platform::String^  _Title;
    	SurfaceImageSource^ _Image;
    
        public:
            GraphicsItem();
    
    	event PropertyChangedEventHandler^ PropertyChanged;
    
    	// propagate the changes to the source object to  to the target
            void RaisePropertyChanged(Platform::String^ propertyName)
    	{
    		PropertyChangedEventArgs^ pcea = ref new  PropertyChangedEventArgs(propertyName);
    		PropertyChanged(this,pcea);
    	}
    
    	property Platform::String^ Title
    	{
    		Platform::String^ get()
    		{
    			return _Title;
    		}
    		void set(Platform::String^ value)
    		{
    			_Title = value;
    			RaisePropertyChanged("Title");
    		}
    	}
    
    	property SurfaceImageSource^ Image
    	{
    		SurfaceImageSource^ get()
    		{
    			return _Image;
    		}
    		void set(SurfaceImageSource^ value)
    		{
    			_Image = value;
    			RaisePropertyChanged("Image");
    		}
    	}
    
        };
    }
    

    Monday, October 15, 2012 11:51 AM
  • the INPC interface isn't always needed depending on your requirements of your application.  It provides an interface for implementing change notification on bound-property changes.

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Monday, October 15, 2012 4:11 PM
  • Another issue, whenever I add the line [Windows::UI::Xaml::Data::Bindable] before class definition, the VS express 2012 compiler reports errors:

    1>  XamlTypeInfo.g.cpp
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(99): error C2039: 'GraphicsItem' : is not a member of 'metouch'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(99): error C2061: syntax error : identifier 'GraphicsItem'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(118): error C2039: 'GraphicsItem' : is not a member of 'metouch'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(118): error C2065: 'GraphicsItem' : undeclared identifier
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(118): error C2059: syntax error : ')'
    1>e:\projects\metouch\generated files\xamltypeinfo.g.cpp(119): error C2227: left of '->Image' must point to class/struct/union/generic type

    My code is

    namespace metouch
    {
        // Uncomment the line below will result in compiler errors C2039
        //[Windows::UI::Xaml::Data::Bindable]
        public ref class GraphicsItem sealed
        {
    	Platform::String^  _Title;
        };
    }

    #include "GraphicsItem.h" in your app.xaml.h or pch.h file should resolve this issue.   MikeBmcl explains why on this thread http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/e1859871-0e3f-445f-a347-4e5ea69016e8


    MCAD.NET C# - http://www.Global-webnet.com/Blog

    Thursday, March 21, 2013 9:49 AM