Skip to content

Commit

Permalink
INotifyDataErrorInfo implementation (#419)
Browse files Browse the repository at this point in the history
* INotifyDataErrorInfo implementation

* test case
  • Loading branch information
Scottj1s authored Sep 22, 2020
1 parent 224ee35 commit 3686f7d
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 26 deletions.
91 changes: 69 additions & 22 deletions TestComponentCSharp/Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using namespace std::chrono;
using namespace winrt;
using namespace Windows::Foundation;
using namespace Collections;
using namespace Microsoft::UI::Xaml::Data;
using namespace Microsoft::UI::Xaml::Interop;
using Windows::UI::Xaml::Interop::TypeName;

Expand Down Expand Up @@ -102,6 +103,27 @@ namespace winrt::TestComponentCSharp::implementation
}
};

struct data_errors_changed_event_args : implements<data_errors_changed_event_args, IDataErrorsChangedEventArgs>
{
data_errors_changed_event_args(winrt::hstring name) :
m_name(name)
{
}

hstring PropertyName() const
{
return m_name;
}

void PropertyName(param::hstring const& name)
{
m_name = name;
}

private:
winrt::hstring m_name;
};

Class::Class() :
Class(0, L"")
{
Expand Down Expand Up @@ -393,11 +415,11 @@ namespace winrt::TestComponentCSharp::implementation
{
_objectChanged.remove(token);
}
Windows::Foundation::Collections::IIterable<Windows::Foundation::IInspectable> Class::ObjectIterableProperty()
IIterable<IInspectable> Class::ObjectIterableProperty()
{
return _objectIterable;
}
void Class::ObjectIterableProperty(Windows::Foundation::Collections::IIterable<Windows::Foundation::IInspectable> const& value)
void Class::ObjectIterableProperty(IIterable<IInspectable> const& value)
{
for (auto element : value)
{
Expand All @@ -412,7 +434,7 @@ namespace winrt::TestComponentCSharp::implementation
{
_objectIterable = provideObjectIterable();
}
winrt::event_token Class::ObjectIterablePropertyChanged(Windows::Foundation::EventHandler<Windows::Foundation::Collections::IIterable<Windows::Foundation::IInspectable>> const& handler)
winrt::event_token Class::ObjectIterablePropertyChanged(EventHandler<IIterable<IInspectable>> const& handler)
{
return _objectIterableChanged.add(handler);
}
Expand Down Expand Up @@ -789,22 +811,22 @@ namespace winrt::TestComponentCSharp::implementation
_point = value;
}

Windows::Foundation::Rect Class::RectProperty()
Rect Class::RectProperty()
{
return _rect;
}

void Class::RectProperty(Windows::Foundation::Rect const& value)
void Class::RectProperty(Rect const& value)
{
_rect = value;
}

Windows::Foundation::Size Class::SizeProperty()
Size Class::SizeProperty()
{
return _size;
}

void Class::SizeProperty(Windows::Foundation::Size const& value)
void Class::SizeProperty(Size const& value)
{
_size = value;
}
Expand Down Expand Up @@ -891,59 +913,59 @@ namespace winrt::TestComponentCSharp::implementation
{
_matrix3D = value;
}
Windows::Foundation::Numerics::float3x2 Class::Matrix3x2Property()
Numerics::float3x2 Class::Matrix3x2Property()
{
return _matrix3x2;
}
void Class::Matrix3x2Property(Windows::Foundation::Numerics::float3x2 const& value)
void Class::Matrix3x2Property(Numerics::float3x2 const& value)
{
_matrix3x2 = value;
}
Windows::Foundation::Numerics::float4x4 Class::Matrix4x4Property()
Numerics::float4x4 Class::Matrix4x4Property()
{
return _matrix4x4;
}
void Class::Matrix4x4Property(Windows::Foundation::Numerics::float4x4 const& value)
void Class::Matrix4x4Property(Numerics::float4x4 const& value)
{
_matrix4x4 = value;
}
Windows::Foundation::Numerics::plane Class::PlaneProperty()
Numerics::plane Class::PlaneProperty()
{
return _plane;
}
void Class::PlaneProperty(Windows::Foundation::Numerics::plane const& value)
void Class::PlaneProperty(Numerics::plane const& value)
{
_plane = value;
}
Windows::Foundation::Numerics::quaternion Class::QuaternionProperty()
Numerics::quaternion Class::QuaternionProperty()
{
return _quaternion;
}
void Class::QuaternionProperty(Windows::Foundation::Numerics::quaternion const& value)
void Class::QuaternionProperty(Numerics::quaternion const& value)
{
_quaternion = value;
}
Windows::Foundation::Numerics::float2 Class::Vector2Property()
Numerics::float2 Class::Vector2Property()
{
return _vector2;
}
void Class::Vector2Property(Windows::Foundation::Numerics::float2 const& value)
void Class::Vector2Property(Numerics::float2 const& value)
{
_vector2 = value;
}
Windows::Foundation::Numerics::float3 Class::Vector3Property()
Numerics::float3 Class::Vector3Property()
{
return _vector3;
}
void Class::Vector3Property(Windows::Foundation::Numerics::float3 const& value)
void Class::Vector3Property(Numerics::float3 const& value)
{
_vector3 = value;
}
Windows::Foundation::Numerics::float4 Class::Vector4Property()
Numerics::float4 Class::Vector4Property()
{
return _vector4;
}
void Class::Vector4Property(Windows::Foundation::Numerics::float4 const& value)
void Class::Vector4Property(Numerics::float4 const& value)
{
_vector4 = value;
}
Expand Down Expand Up @@ -1179,7 +1201,7 @@ namespace winrt::TestComponentCSharp::implementation
return type.Name;
}

Windows::Foundation::IInspectable Class::EmptyString()
IInspectable Class::EmptyString()
{
return winrt::box_value(hstring{});
}
Expand All @@ -1202,4 +1224,29 @@ namespace winrt::TestComponentCSharp::implementation
{
return winrt::make<native_properties1>();
}

// INotifyDataErrorInfo
bool Class::HasErrors()
{
return true;
}
winrt::event_token Class::ErrorsChanged(EventHandler<DataErrorsChangedEventArgs> const& handler)
{
return _dataErrorsChanged.add(handler);
}
void Class::ErrorsChanged(winrt::event_token const& token) noexcept
{
_dataErrorsChanged.remove(token);
}
IIterable<IInspectable> Class::GetErrors(hstring const& propertyName)
{
return _objectIterable;
}
void Class::RaiseDataErrorChanged()
{
auto mock = make<data_errors_changed_event_args>(L"name");
DataErrorsChangedEventArgs args(detach_abi(mock), take_ownership_from_abi_t());
_dataErrorsChanged(*this, args);
}
}

7 changes: 7 additions & 0 deletions TestComponentCSharp/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ namespace winrt::TestComponentCSharp::implementation
void ReadWriteProperty(int32_t value);
//hstring DistinctProperty();
//void DistinctProperty(hstring const& value);

winrt::event<Windows::Foundation::EventHandler<Microsoft::UI::Xaml::Data::DataErrorsChangedEventArgs>> _dataErrorsChanged;
bool HasErrors();
winrt::event_token ErrorsChanged(Windows::Foundation::EventHandler<Microsoft::UI::Xaml::Data::DataErrorsChangedEventArgs> const& handler);
void ErrorsChanged(winrt::event_token const& token) noexcept;
Windows::Foundation::Collections::IIterable<Windows::Foundation::IInspectable> GetErrors(hstring const& propertyName);
void RaiseDataErrorChanged();
};
}

Expand Down
4 changes: 4 additions & 0 deletions TestComponentCSharp/TestComponentCSharp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace TestComponentCSharp
//, IArtist
, IProperties1
, IProperties2
, Microsoft.UI.Xaml.Data.INotifyDataErrorInfo
//, Windows.Foundation.Collections.IVector<String>
//, Windows.Foundation.Collections.IMap<Int32, String>
{
Expand Down Expand Up @@ -312,6 +313,9 @@ namespace TestComponentCSharp

// Interface projections
static IProperties1 NativeProperties1 { get; };

// INotifyDataErrorInfo
void RaiseDataErrorChanged();
}

[threading(sta), marshaling_behavior(standard)]
Expand Down
12 changes: 10 additions & 2 deletions UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,17 @@ public void TestEvents()
TestObject.InvokeCollectionEvent(TestObject, collection0, collection1);
events_expected++;

Assert.Equal(events_received, events_expected);
}
TestObject.ErrorsChanged += (object sender, System.ComponentModel.DataErrorsChangedEventArgs e) =>
{
events_received++;
Assert.Equal("name", e.PropertyName);
};
TestObject.RaiseDataErrorChanged();
events_expected++;

Assert.Equal(events_received, events_expected);
}

[Fact]
public void TestKeyValuePair()
{
Expand Down
2 changes: 2 additions & 0 deletions WinRT.Runtime/Projections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ static Projections()
RegisterCustomAbiTypeMappingNoLock(typeof(Exception), typeof(ABI.System.Exception), "Windows.Foundation.HResult");
RegisterCustomAbiTypeMappingNoLock(typeof(TimeSpan), typeof(ABI.System.TimeSpan), "Windows.Foundation.TimeSpan");
RegisterCustomAbiTypeMappingNoLock(typeof(Uri), typeof(ABI.System.Uri), "Windows.Foundation.Uri", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(DataErrorsChangedEventArgs), typeof(ABI.System.ComponentModel.DataErrorsChangedEventArgs), "Microsoft.UI.Xaml.Data.DataErrorsChangedEventArgs", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(PropertyChangedEventArgs), typeof(ABI.System.ComponentModel.PropertyChangedEventArgs), "Microsoft.UI.Xaml.Data.PropertyChangedEventArgs", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(PropertyChangedEventHandler), typeof(ABI.System.ComponentModel.PropertyChangedEventHandler), "Microsoft.UI.Xaml.Data.PropertyChangedEventHandler");
RegisterCustomAbiTypeMappingNoLock(typeof(INotifyDataErrorInfo), typeof(ABI.System.ComponentModel.INotifyDataErrorInfo), "Microsoft.UI.Xaml.Data.INotifyDataErrorInfo");
RegisterCustomAbiTypeMappingNoLock(typeof(INotifyPropertyChanged), typeof(ABI.System.ComponentModel.INotifyPropertyChanged), "Microsoft.UI.Xaml.Data.INotifyPropertyChanged");
RegisterCustomAbiTypeMappingNoLock(typeof(ICommand), typeof(ABI.System.Windows.Input.ICommand), "Microsoft.UI.Xaml.Interop.ICommand");
RegisterCustomAbiTypeMappingNoLock(typeof(EventHandler<>), typeof(ABI.System.EventHandler<>), "Windows.Foundation.EventHandler`1");
Expand Down
Loading

0 comments on commit 3686f7d

Please sign in to comment.