Crash in production when using a WebBrowser inside a Pivot - c#

I know it is a bad idea to put a WebBrowser inside a Pivot/RadSlideView control.
I did so anyway:
<phone:PhoneApplicationPage
x:Class="**.HtmlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
mc:Ignorable="d"
Style="{StaticResource LeafPageNavigationStyle}">
<controls:Pivot x:Name="Html" ItemsSource="{Binding Items}"
Style="{StaticResource HeaderlessPivot}">
<controls:Pivot.ItemTemplate>
<DataTemplate>
<phone:WebBrowser Source="{Binding}" />
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
</phone:PhoneApplicationPage>
Basically I want to use the Pivot to slide through an array of HTML docs at URIs I provide via my ViewModel, which just wraps an the array in a Caliburn.Micro OneActive Conductor:
namespace DSBMobile.ViewModels
{
public class HtmlViewModel : Conductor<Uri>.Collection.OneActive
{
private readonly IUnburyableState<Uri[], HtmlViewModel> _state;
public HtmlViewModel(IUnburyableState<Uri[], HtmlViewModel> state)
{
_state = state;
Items.AddRange(_state.State.ForceGetValue());
}
}
}
That runs pretty well in debug and release versions I deploy manually. The App passes all tests imposed by the Store, but as soon as I try to open this specific view within the app, it crashes without any chance to redirect to a Telerik MessageBox.
As soon as I remove the outer Pivot and adjust the ViewModel accordingly, it runs smoothely. As I a said, the crash only happens in production. The Application.UnhandledException handler can't get the app to swallow the exception and display the error.
This is really intricate and bugs me since months. Can anyone resolve this error or point me in a worthwhile direction? I would also appreciate a more WP-ish suggestion for displaying multiple Web links that works.

It turns out that I got an UnauthorizedAccessException, explaining that I was missing the ID_CAP_WEBBROWSERCOMPONENT capability, which I wasn't. That confused me, until I finally had a look at the docs:
When you create a WebBrowser control in XAML, you must specify a value for the P:System.Windows.FrameworkElement.Name property for that control so that the Windows Phone Capability Detection Tool can properly detect and grant the right capabilities for your app. For more info about the Windows Phone Capability Detection Tool, see How to determine app capabilities.
With the x:Name set, I could finally have a crashless experience.
For me personally, this was the most annoying bug, ever. It helped that I could upload beta versions in the store although I didn't pay any developer fee, which I hadn't known in beforehand.
TLDR: RTFM.

Related

Content Dialog with Map Control Windows Phone

Content Dialog in windows Phone 8.1 doesn't allow to add map control in XAML , when you added it and run the project the app crashing with Catastrophic failure
exception.
<ContentDialog
xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps"
x:Class="example.ContentDialogexample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="">
<StackPanel>
<Maps:MapControl MapServiceToken="MapKey" />
</StackPanel>
</ContentDialog>
Not sure why it crashes, but the reality is that you shouldn't load the map inside of any kind of dialog or popup. The map uses a lot of resources and also generates a billable transaction every time it is loaded. Using it in a dialog or popup likely means that the map will be loaded and disposed a lot. This would generate a lot of billable transactions and you will end up exceeding the free limits very fast and then licensing would likely cost a lot as the number of transactions would likely be very high.

How do I tab between text boxes on a form in C#?

I'm trying to get the functionality to tab to the next text box once the user has input their data for the previous text box. For example, once they filled in a company name I'd like to be able to hit Tab and set the focus on the next text box "Job Name". Is this done in the code or the form properties?
Here is some of my code. I'm unsure how to nest a KeyEventsArgs within these, which is how I've seen others set the focus to the next text boxes using the KeyPress function.
private void textBox1_TextChanged(object sender, EventArgs e)
{
CompanyName = textBox1.Text;
textBox1.AcceptsTab = true;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
JobName = textBox2.Text;
textBox2.AcceptsTab = true;
}
From the question you've asked and the code sample provided, there seems to be somewhat of a disconnect between your approach and the desired functionality.
As you would like the user to be able to use the Tab key in order to shift keyboard focus between elements in the window, you need only provide a TabIndex attribute on each of your TextBox controls. There is no need to use the TextChanged events to achieve this and it can be done completely in XAML for simplicity's sake.
From how I interpret your question, your next follow-on will likely be:
How do I initially give focus to a control when the application
starts?
To address this, there are a couple of alternatives available, simplest of which comes in the form of the FocusManager, which again I've illustrated usage of in XAML.
For convenience, here is a XAML-only implementation with TabIndex and FocusManager implemented:
<Window x:Class="tab_navigation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:tab_navigation"
mc:Ignorable="d"
Title="MainWindow" ResizeMode="NoResize" SizeToContent="WidthAndHeight" FocusManager.FocusedElement="{Binding ElementName=TbxCompanyName}">
<Grid Margin="10">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical" Margin="0,0,0,10">
<Label Content="Company Name:" Target="{Binding ElementName=TbxCompanyName}" />
<TextBox Name="TbxCompanyName" TabIndex="0" Width="160" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<Label Content="Job Description:" Target="{Binding ElementName=TbxJobDescription}"/>
<TextBox Name="TbxJobDescription" TabIndex="1" Width="160" HorizontalAlignment="Left"/>
</StackPanel>
</StackPanel>
</Grid>
Give me a shout if you need any further help, although I would strongly recommend checking out some of the MSDN resources first, particularly those concerning Focus
UPDATE: In response to comment regarding implementing the solution,
WPF has a different design and best practices from that of WinForms.
I would strongly make the case that you cease using Forms and instead use a Window or UserControl derived class in place of a Form in your WPF project unless there is a very, very good reason for doing so. If you continue to use a Form inside of your WPF project, you will indeed need to implement your own keyboard navigation logic inside that form, and bridge various other gaps you'll inevitably run into when trying to get a Form behave in a commonly acceptable way.
I'll instead show you how you can achieve your request using an objectively better and more suitable approach in WPF-only, using Window or UserControl elements. There is also a complete solution zip downloadable here.
WPF is by design a lot more modular than WinForms and splits the areas of concerns nicely by default, although most developers implement a design pattern ontop of this; MVVM is the current darling of WPF, and does add quite a lot of value to a project, although it is outside the scope of your question, so I shall instead address the question itself on the grounds of how to achieve the request in its most basic forms. Do please be aware though that this is not the entirely ideal solution and I would strongly recommend you learn and implement the MVVM pattern for WPF if you are not already familiar with it.
With that disclaimer out of the way, instead of using a Form in WPF, its more useful for us to make a class which derives from Window. An even more common scenario in WPF would be that you would want to have a single window whose content changes between different views, rather than say creating multiple windows, although again that is outside the scope of the question and would rely upon reading into Binding and MVVM. I'm going to be showing you a quick and easy way to get the functionality you've asked for, I'm just trying to iterate here that this is not the norm almost all of the time.
To make a working solution, do the following to your project:
Right click your project in the solution explorer (presuming you are using Visual Studio)
'Add' a 'New Item...'.
Choose the 'Window (WPF)' template and name it. I'm going to call it CustomerInformationEntry from here out.
Open the CustomerInformationEntry.xaml file that has been created for us, remove the <Grid></Grid> tags and copy/paste this excerpt from the XAML I've already provided from above in their place:
<Grid Margin="10">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical" Margin="0,0,0,10">
<Label Content="Company Name:" Target="{Binding ElementName=TbxCompanyName}" />
<TextBox Name="TbxCompanyName" TabIndex="0" Width="160" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<Label Content="Job Description:" Target="{Binding ElementName=TbxJobDescription}"/>
<TextBox Name="TbxJobDescription" TabIndex="1" Width="160" HorizontalAlignment="Left"/>
</StackPanel>
</StackPanel>
</Grid>
Add FocusManager.FocusedElement="{Binding ElementName=TbxCompanyName} to the Window element in CustomerInformationEntry.xaml.
This is our view or visual representation finished with now, and all that remains is to instanciate a new CustomerInformationEntry from our other Window or UserControl, and to then display it. In this case I'm going to be putting a button onto the MainWindow.xaml, and providing it a click event which will create the instance of our new Window:
In MainWindow.xaml add <Button Name="BtnOpenCustomerInformationEntry" Content="Enter Customer Information" Click="OpenCustomerInformationEntry"/>. In my case I'll be adding the button inside my object, although you can put it wherever you like if you've already created your initial window.
In MainWindow.xaml.cs we'll add a new private method which will be used by the Click event of your new button. Adding the following code:
private void OpenCustomerInformationEntry(object sender, RoutedEventArgs e)
{
CustomerInformationEntry myWindow = new CustomerInformationEntry();
myWindow.Show();
}
That's it, you now have a button in your MainWindow.xaml which when clicked uses the OpenCustomerInformationEntry method defined in MainWindow.xaml.cs, which in turn makes an instance of your CustomerInformationEntry window and displays it.
If you would still rather stick with the Forms approach, you can do that by using WindowsFormsHost, usage of which is discussed here.
Best Regards,
JC

Disable UI Automation for Silverlight controls (combobox, etc.)

I've been getting memory leaks in Silverlight because of the UI automation framework as documented here
We implement custom control libraries (controls which inherit basic Silverlight controls and extend their functionality). e.g.
<local:BaseControl x:Name="DropDownControl" x:Class="xxx.xxx.Views.DropDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local ="clr-namespace:xxx.CustomControls.Views"
mc:Ignorable="d"
d:DesignHeight="23" d:DesignWidth="120" Background="#FF1F3B53">
<Border x:Name="bdrMain" BorderThickness="1" BorderBrush="Transparent">
<Grid x:Name="LayoutRoot">
<ComboBox x:Name="ddlMainControl" Canvas.ZIndex="1"/>
</Grid>
</Border>
I can disable automation for the Custom Control itself by overriding the OnCreateAutomationPeer method in the class itself.
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
return null;
}
However, the Combobox iteself must still respond to requests from the Automation Provider for its Automation Peer as I can still see it leaking in WinDBG. Any ideas on how to disable Automation for the Combobox in this scenario?
P.S. - I know you disable automation application wide by setting the windowless param = true but it's a mature application and I'd prefer not to change anything application wide that might add risk
The default panel is the VirtualisingStackPanel which may be the cause of your problem.

MVVM Binding not working in design-time

I tried to bind the visibility of a few buttons to some booleans in a viewmodel and make it work in design-time. I did this several times and never had a problem, but now it does not work and I don't have a clue why. Note that everything works
fine when I run the application.
I extracted the essence into a separate app and it still doesn't work!
My XAML:
<Window x:Class="BindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:bindingTest="clr-namespace:BindingTest"
Title="MainWindow" Height="350" Width="525"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance bindingTest:TestViewModel, d:IsDesignTimeCreatable=true}">
<Grid>
<Grid.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Grid.Resources>
<StackPanel>
<Button Visibility="{Binding IsButton1Visible, Converter={StaticResource BoolToVis}}">Hallo 1</Button>
<Button Visibility="{Binding Button2Visibility}">Hallo 2</Button>
<Button>Hallo 3</Button>
</StackPanel>
</Grid>
</Window>
My ViewModel:
public class TestViewModel
{
public bool IsButton1Visible
{
get { return true; }
}
public Visibility Button2Visibility
{
get { return Visibility.Hidden; }
}
}
To make it work in the real app, i added to the constructor of the XAML:
DataContext = new TestViewModel();
This is all pretty simple, but why is it not working in the designer?? I always got this to work in the past and now I tried for hours... It's not the only binding not working, but why is not even this working??
Expected Result:
Button1 visible
Button2 hidden
Button3 visible
Result in the designer:
Button1 collapsed
Button2 visible
Button3 visible
UPDATE: The problem with design-time DataContext bindings not working is due to mistakenly using the d: namespace prefix for the IsDesignTimeCreatable property.
So, change
d:IsDesignTimeCreatable=True
to
IsDesignTimeCreatable=True
and everything should be fine. (My bug report filed with MS will be updated with this information as well.)
As this problem with the design-time DataContext bindings not working was consistently reproducable in VS2015 as well as VS2013 when IsDesignTimeCreatable is prefixed with the d: namespace (see comments below the question), i decided to file a bug report with Microsoft: Issue #1651633 "WPF designer: Designtime DataContext bindings broken?".
Lets see how Microsofts will follow-up with regard to this issue. It should be addressed, since such an issue can catch a developer off-guard and make him reinstall Visual Studio for no benefit... ;)
If anything is OK with the code, there could be an other tricky problem:
In the Designer of Visual Studio a new button was introduced in 2019, which enables / disables the preview of design data.
It's a very tiny button on botton left of the designer window. It's tooltip is "Enable Project Code".
Click this important toggle button to see the design data. (But be aware that you don't disable it, if it's already enabled.)

WP8 Map control with custom tiles

I'm trying to display a map control in my app targeting Windows Phone 8 but I want to use custom tiles from open street map.
I'm using this line :
mapControl.TileSources.Add(new TileSource("http://a.tile.openstreetmap.org/{zoomLevel}/{x}/{y}.png"));
When I launch the app the custom tiles are properly displayed, but there's still the default map tiles underneath them.
I tried to hide them, but I counld'nt even find where they exist in the map control and what is displaying them exactly.
I would like to know how I can remove those default tiles when loading custom tiles.
I tried to hide them, but I counld'nt even find where they exist in the map control and what is displaying them exactly.
PS : Here's the link to the "old" bing map control if you cannot find it in the latest WP Tools : http://www.microsoft.com/en-us/download/confirmation.aspx?id=2949
I tried this myself with the new WP8 "Nokia HERE maps" map control but was unable to achieve this. I had to resort to falling back to the older "Bing" based map control in Microsoft.Phone.Controls.Maps (marked as obsolete).
Here's how to remove the other layers in the older Microsoft.Phone.Controls.Maps control:
for (var i = Map.Children.Count - 1; i >= 0; i--)
{
MapTileLayer tileLayer = Map.Children[i] as MapTileLayer;
if (tileLayer != null)
{
Map.Children.RemoveAt(i);
}
}
Even though this older map control has been superseded in WP8 the newer control doesn't seem to support the same flexibility with layers and the "obsolete" control still works happily under WP8.1 if used in your app.
Here's my app which still uses the older control which is probably achieving what you're trying to do - NZ Topo Map app for Windows Phone.
Cut down Xaml for using the older map control in your app (you'll probably want to ignore my data bindings and replace them with your own):
<UserControl x:Class="TopoMap.Controls.Map"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid x:Name="LayoutRoot" Background="Transparent">
<m:Map x:Name="MapBase" LogoVisibility="Collapsed" ScaleVisibility="Visible"
Loaded="Map_Loaded"
LayoutUpdated="Map_LayoutUpdated"
ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}"
Center="{Binding Center, Mode=TwoWay}">
</m:Map>
</Grid>
</UserControl>
The important reference that you need it:
xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"

Categories