Note that there are some explanatory texts on larger screens.

plurals
  1. POCasting to templated classes from framework objects in C++/Cx
    primarykey
    data
    text
    <p>I'm currently working on creating a new container in my WinRT project. This container will behave similar to a ListView, so I've decided to use similar names in its properties.</p> <p>I created a DependencyProperty for ItemsSource to mimic that of the built-in ItemsControl (abstract parent to other lists). ItemsControl defines ItemsSource as a <code>Platform::Object^</code>, so I did the same. This works fine.</p> <p>Later on, it's time to create items to go in the list, populate them with instances of the specified ItemsTemplate, and set their data contexts. This means I need to actually cast ItemsSource from <code>Platform::Object^</code> into something iterable. Unfortunately, to cast it to something iterable, I need to specify the templated type in the cast (e.g. <code>IIterable&lt;UIElement^&gt;^</code> or <code>IIterable&lt;Object^&gt;^</code>). At this stage, I don't particularly care what type of object it is because I'm just using it as the DataContext for the new list item, so casting to <code>IIterable&lt;Object^&gt;^</code> would be fine. Unfortunately, safe_cast doesn't let me do this unless the thing originally set to ItemsSource was also templated as a <code>IIterable&lt;Object^&gt;^</code> or some child such as <code>IVectorView&lt;Object^&gt;^</code>.</p> <p>With existing containers such as ListView, you can set the ItemsSource to a <code>Vector&lt;MyCustomViewModel^&gt;^</code> without first converting it to a <code>Vector&lt;Object^&gt;^</code> and it works just fine. So how do they do this? Are they not using safe_cast? I'm pretty sure using a less-safe cast would have adverse effects here. Have any other ideas?</p> <p><strong>TL;DR:</strong></p> <p>I have some <code>Platform::Object^</code> reference that I know points to an IIterable of other references. Is it possible to cast it to <code>IIterable&lt;Platform::Object^&gt;^</code> somehow, even if the iterable was originally created with some other template (such as <code>IIterable&lt;MyCustomViewModel^&gt;^</code>)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload