TemplateStudio icon indicating copy to clipboard operation
TemplateStudio copied to clipboard

Multiple views feature exception when trying to open a Map Page

Open dawsongh opened this issue 7 years ago • 7 comments

When trying to use multiple view feature opening a Map page that has been created in WTS get the following exception:

System.Exception HResult=0x8001010E Message=The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

Repro steps

Create a new project using template (Nav Pane, Code behind), add Map page and add "Multiple Views" from Features.

From the main page, add a button then add the following code to try and open the page: await WindowManagerService.Current.TryShowAsStandaloneAsync("Secondary_Title.Text", typeof(MapPage));

Just wondering if this call should be away from the UI thread?

TIA

System

  • VS Version: 15.9.4
  • WTS Wizard Version: 2.5.18319.1
  • WTS Template Version: 2.5.18334.1
  • Windows Build: 1809 17763.195

dawsongh avatar Jan 04 '19 16:01 dawsongh

@dawsongh can you attach a zip with a repro here, i want to be sure we're 100% capturing the issue with the given framework and nav pattern.

crutkas avatar Jan 04 '19 17:01 crutkas

@crutkas Have attached a zip as requested with just two pages (main and map) - click the button on main page to get the thread exception. App3.zip

dawsongh avatar Jan 04 '19 18:01 dawsongh

There are two different problems here:

  • MapPage should not include NavigationViewHeaderBehavior HeaderMode property when it is not shown in a NavigationView.
  • LocationService_PositionChanged should wrap the code in the new window Dispatcher RunAsyncCode.

We are going to add some dev guidance in NavigationView and MultiView docs.

Remove this in MapPage.xaml

xmlns:behaviors="using:App3.Behaviors"
behaviors:NavigationViewHeaderBehavior.HeaderMode="Never"

Add ViewLifetimeControl release management and update LocationService_PositionChanged in MapPage.xaml.cs

// Add this property
private ViewLifetimeControl _viewLifetimeControl;

// Update this method
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
   _viewLifetimeControl = e.Parameter as ViewLifetimeControl;
   _viewLifetimeControl.Released += OnReleased;
   await InitializeAsync();
}

// Add this method
private void OnReleased(object sender, EventArgs e)
{
   _viewLifetimeControl.Released -= OnReleased;
   WindowManagerService.Current.SecondaryViews.Remove(_viewLifetimeControl);
}

//Update this method
private async void LocationService_PositionChanged(object sender, Geoposition geoposition)
{
   await _viewLifetimeControl.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
   {
      if (geoposition != null)
      {
         Center = geoposition.Coordinate.Point;
      }
   });
}

mvegaca avatar Jan 08 '19 10:01 mvegaca

@mvegaca would this affect every page or just map page. i am a bit worried about having custom page logic where one page has crazy different logic

what is your suggested changes here for the actual templates due to this bug?

crutkas avatar Jan 08 '19 18:01 crutkas

We can't include the changes in the page templates as this only has to be done when the page is shown in a new window.

When a developer wants to show pages in separated views they must take care of some things like:

  • Use the new window dispatcher to run code on the UI thread.
  • Remove attached properties used to configure the Main Window NavigationView header.
  • Use MainWindow Dispatcher to run code on the main window from new windows.

These kind of considerations are not necessary when pages are shown as part of the app in the main window.

As we don't know if the developer wants to show a page in a new window or not, the best approach here is adding more info in Multiview doc. We also have a look at other pages.

mvegaca avatar Jan 09 '19 08:01 mvegaca

Thoughts on having a docs page and link in actually page that would have the instructions for shifting page to multiple view? It is used by 4% based on the telem data

crutkas avatar Jan 09 '19 17:01 crutkas

We update the multiview documentation and added things to take into account when showing a page in the new window.

dgomezc avatar Jan 25 '19 09:01 dgomezc