0

In a UWP project, I put an InkCanvas in each ListViewItem. But when the items count exceed 126, I got an XamlParseException, which complained: Cannot create instance of type 'Windows.UI.Xaml.Controls.InkCanvas'

Below is the naïve code snippets:

<Page x:Class="App9.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{x:Bind Items, Mode=OneWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <InkCanvas />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        </ListView>
    </Grid>
</Page>

code behind:

public sealed partial class MainPage : Page
{
    public ObservableCollection<int> Items { get;  }
    public MainPage()
    {
        this.Items = new ObservableCollection<int>();
        for (int i = 0; i < 130; i++)
        {
            this.Items.Add(i);
        }
        InitializeComponent();

    }
}

(Please note: to test the upper limit, I've changed the default ItemsPanel of ListView) These above code is pretty simple, so it seems nothing is wrong with the code. Could it be there is a upper limit one can create InkCanvas?

Edit: I've tested to create the InkCanvas in code behind like this:

  for (int i = 0; i < 130; i++)
    {
        this.lvw.Items.Add(new InkCanvas());
    }

which I got a different runtime error: System.OutOfMemoryException: 'Insufficient memory to continue the execution of the program.'

But by inspect Task Manager, I can see an app contains 100 instance of InkCanvas only take no more than 40mb memory. It is very confusing.

Clemens
  • 123,504
  • 12
  • 155
  • 268
NaiveCoder
  • 97
  • 2
  • 6
  • 3
    That is a BAD idea. Why would you ever want to create that many items? You should at least enable virtualization. Not to mention `InkCanvas` is a heavy control... – Justin XL Jul 06 '17 at 11:56
  • Thanks, Justin!In my working app, I've enabled virtualization. I've put them in a ListView which will dynamically create about 10 to 60 ListViewItems. But after I refresh the ListView about 3 times, the app will crashing. Got a runtime error like this. – NaiveCoder Jul 06 '17 at 12:06
  • What is each InkCanvas supposed to display? – Clemens Jul 06 '17 at 12:15
  • There seem lots of "InkCanvas + out of Memory" issues. Seems InkCanvas is a heavy control, so I'll dynamically create InkCanvas only when needed and use Image to render the ink strokes. Thanks, guys! FYI:https://stackoverflow.com/questions/30621375/universal-windows-inkcanvas-strokes-dissapear-on-rendertargetbitmap-renderasync – NaiveCoder Jul 06 '17 at 13:41

0 Answers0