0

So I have a bit of an issue. I'll see how well I can explain it.

I have a class that is shared between my windows phone foreground app and the background as a link (they both use the same file) this is because data is serialized from this class and passed back in forth so it needs to be exactly the same.

In the foreground process I want to use IEquatable to be able to override GetHash and Equals so when I call ObservableCollection.Remove on an ItemModel it doesn't remove based on reference and I can have it manually compare properties as I am removing an item selected from another list (ie. 2 identical items each have their own spot in memory).

I've tested and it works fine, but I cannot implement IEquatable in a Windows Runtime interface (I don't quite get this anyway, but okay) "Error 10 Type 'Project.ViewModels.ItemModel ' implements interface 'System.IEquatable', which is not a Windows Runtime interface. All interfaces that are implemented by exported types must be Windows Runtime interfaces. "

So IEquatable is allowed in the foreground process, but not the background but because they are using the linked file. You might ask, why not just make a duplicate of this class in the background project and not implement IEquatable? Well because I started running into issues serializing and deserializing data when the two classes weren't defined exactly alike..

  public class ItemModel: INotifyPropertyChanged, IEquatable<ItemModel>
user2704766
  • 235
  • 1
  • 5
  • 14
  • 1
    [According to this answer](http://stackoverflow.com/a/18166230/3538012) (and if LarryO says you can't, I'm pretty sure you can't), you simply can't have an exported type in WinRT that implements `IEquatable`. That said, XML serialization shouldn't require exact type-matching, nor should it care about interface declarations. So I think you should be able to get this to work, by only implementing `IEquatable` in the foreground process, and leaving it out of the background (use `#if` to conditionally include `IEquatable` and any other problematic interface implementation). – Peter Duniho Nov 29 '14 at 04:20
  • Thanks I will try, how can I use #if to include this only in the foreground project? I realized while testing I added a DataContractAttribute which may have nulled my data out when serialzing. – user2704766 Nov 29 '14 at 04:37
  • 1
    Something like `class Foo\n#if IEQUATABLE\n : IEquatable\n#endif`. And then of course a similar `#if` around the actual implementation. By `\n` I simply mean a line-break...of course you wouldn't literally enter `\n` in your code. You can either define `EQUATABLE` in your project settings for that build, or use a different `#define` that's already conveniently defined in the foreground process but not the background or vice a versa. – Peter Duniho Nov 29 '14 at 06:28
  • Thanks, I believe this is the answer. – user2704766 Nov 29 '14 at 19:24

0 Answers0