Winrt apps, unhandled exception at random occasions - xaml

During the development I experience unhandled exceptions at random occasions, but mostly after closing the application. Setting all options on 'break on exceptions' does not trigger any code. Anyone who experience the same behaviour. I am developing on the release preview build and visual studio RC build.

First steps I did to resolve the problem is to:
Enable native debugging in the project properties (Debug tab,
debugger type) (thnx James! I know now what you mean :-) )
Set symbol server to the right location (tools, options, Debugging, Symbols, all modules)
And start debugging. This will take a few minutes to start to load all symbols....
I traced it down to several exceptions when binding to an Bitmap of which the URL did not resolve. The binding was of a GridView to the SmallImageSource bitmap property of a class. When debugging this will give an exception in native code, but doesn’t trace to the output window or give an exception in the application. Eventually my application crashed… After fixing these resources my application did not crash anymore. Although this is not reproducible in a small scenario. Below a small bit of application code on which it crashed. the GridView was embedded in a SemanticZoom control
XAML (used in a semanticzoom control)
<GridView.ItemTemplate>
<DataTemplate>
<Grid Background="Gray" Width="300" Height="80">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="{Binding Group.Key.SmallImageSource}" Stretch="Uniform" Margin="15" Grid.Column="0"/>
<TextBlock Text="{Binding Group.Key.Name}" Margin="0,10" TextAlignment="Center" Grid.Column="1" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
C# (here is an URI which does not resolve to a local resource)
public Uri SmallImage
{
get
{
return new Uri("ms-appx:///Resources/Images/SubCategories/" + Id + "_" + FunctionHelper.StripCharacter(Name) + ".png", UriKind.Absolute);
}
}
public BitmapImage SmallImageSource
{
get
{
return new BitmapImage(SmallImage);
}
}
}

Related

Unable to add MediaPlayerElement in XAML

I'm trying to use the Windows.Media.Playback MediaPlayer. I am attempting to follow the information here Play audio and video with MediaPlayer. I am able to hear the audio of a video by calling it from C#.
public MainPage()
{
InitializeComponent();
var mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4"));
mediaPlayer.Play();
}
However, when I attempt to add a MediaPlayerElement in XAML I get an 'MediaPlayerElement' cannot be added to a collection or dictionary of type 'IList'1'. error
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Windows.UI.Xaml.Controls;assembly=Windows.Foundation.UniversalApiContract"
x:Class="XamarinTest.MainPage">
<StackLayout>
<controls:MediaPlayerElement x:Name="_mediaPlayerElement" AreTransportControlsEnabled="False" HorizontalAlignment="Stretch" Grid.Row="0"/>
</StackLayout>
</ContentPage>
If I remove the StackLayout and have the MediaPlayerElement directly in the Content I get a 'Content' does not support values of type 'MediaPlayerElement'.
I saw this question so I assume I'm missing some kind of DLL or reference but I am unable to locate what reference would be needed.
You are trying to apply UWP controls to a cross platform Xamarin Forms project
Windows.Media.Playback is only for Windows, and would not work on Android or iOS. There are techniques you can use to include platform specific controls in a Xamarin project, or you can use a cross-platform control like MediaElement

Metro modal flyout disables itself

I have used metro mahhapps flyout like this:
<Grid>
<Controls:FlyoutsControl>
<Controls:Flyout IsModal="True" IsOpen="True" Header="Flyout" Position="Right" Width="200">
<!-- Your custom content here -->
</Controls:Flyout>
</Controls:FlyoutsControl>
</Grid>
and it disables everything, even itself - I can not close it, nor can I close my application.
Am I doing something wrong?
The Flyouts container must be located on MetroWindow level.
<Controls:MetroWindow.Flyouts>
<Controls:FlyoutsControl>
<Controls:Flyout IsModal="True" IsOpen="True" Header="Flyout" Position="Right" Width="200">
<!-- Your custom content here -->
</Controls:Flyout>
</Controls:FlyoutsControl>
</Controls:MetroWindow.Flyouts>

Xamarin Forms image binding on url with default on embedded resource

i have a Xamarin ListView with images from internet. I would to show a default image (from embedded resource), and, when present, the image from an url.
I've tryied with this code but works only with UriImageSource, not for the embedded resource:
<ListView ItemsSource="{Binding Data}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Image>
<Image.Source>
<!-- does not works
<ImageSource>
{utils:ImageResource Namespace.Assets.img-placeholder.png}
</ImageSource>
-->
<UriImageSource Uri="{Binding ImageUri}"></UriImageSource>
</Image.Source>
</Image>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
if i use
<Image Source="{utils:ImageResource Namespace.Assets.img-placeholder.png}" />
this works showing the embedded resource default image
The model:
public Uri ImageUri => Image != null ?
new Uri($"http://www.example.com/{Image}")
: null;
Any help? Thanks
You can use FFImageLoading library.
According to its documentation:
LoadingPlaceholder
ImageSource property. If set, a loading placeholder is shown while
loading. It supports UriImageSource, FileImageSource and
StreamImageSource.
ErrorPlaceholder
ImageSource property. If set, if error occurs while loading image, an
error placeholder is shown. It supports UriImageSource,
FileImageSource and StreamImageSource.
You don't have to make it that complicated. From your viewmodel you simply provide a string ImageUri and bind it to Image.Source
<Image Source="{Binding ImageUri}" />
this works for images from the web as well as resources, but they have to live in your platform projects. For Android you'll have to put them to Resources\drawable (or the respective folders for other resolutions) and set the build action to AndroidResource, for iOS to Resources (in different sizes with the #2x, #3x suffices, e.g. img-placeholder#2x.png) and set the build action to bundle resource. You can now set the placeholder from your viewmodel:
if(!HasRemoteImage(response))
{
ImageUri = "img-placeholder.png";
}
Xamarin.Forms will create the ImageSource automatically for you.
One main advantage over resources you load directly is, that you don't have to care which image is the right one for the current resolution. Xamarin/Xamarin.Forms does that automatically for you.

XAML MediaElement Play() method not working in code

Hi I have a Windows Phone app with a MediaElement window defined in XAML to play a video file:
<MediaElement x:Name="mediaWindow" MediaEnded="mediaWindow_MediaEnded" Stretch="UniformToFill" HorizontalAlignment="Left" Height="225" Margin="15,127,0,0" VerticalAlignment="Top" Width="450" AutoPlay="True"/>
When I call this in C# code it play fine:?
mediaWindow.Source = new Uri(loc, UriKind.Relative);
But something strange is happening eg when the user navigates to a new page a MediaElement on that page should kickin and play an audio file but I doesn't. If I comment out the code above which play the video then the audio file on the navigated to page works OK.
I've tried using in XAML:?
<MediaElement x:Name="mediaWindow" MediaEnded="mediaWindow_MediaEnded" Stretch="UniformToFill" HorizontalAlignment="Left" Height="225" Margin="15,127,0,0" VerticalAlignment="Top" Width="450" AutoPlay="False"/>
and in C#:
mediaWindow.Source = new Uri(loc, UriKind.Relative);
mediaWindow.Play();
But this just shows a black screen!?
These is another MediaElement defined in XAML on the original page which I use to play an audio file, could this be the problem? If so what can I do to play the audio file and a video file on the same page? I've read I could use MediaPlayer for the audio file but I tried defining that in c# code but it does not exixt in namespace even though I have included:
using System.Windows.Media;
This is driving me crazy, new to C#, help appreciated here.
Many thanks
Did you try giving the source of the video within the XAML? and make the AutoPlay to false.
<MediaElement x:Name="mediaSimple"
Source="Videos/video1.mp4"
Stretch="UniformToFill" AutoPlay="False"/>
and give the mediaSimple.Play() within a button click event handler.

Metro ui: how to play audio stream in background mode

I am implementing audio player that will should be able play real time PCM stream while application goes to background mode. All solutions (XAudio2, IAudioClient2) I have tried so far stop playback as soon as app goes into background.
Any help is appriciated. Thanks.
i am sure this should be possible.. it is in WP7 so don't see why Win8 would be impossible. quick search yields http://blogs.msdn.com/b/windowsappdev/archive/2012/05/16/being-productive-when-your-app-is-offscreen.aspx which has a link to Build (yes Build) to Background audio.
Here's a link to WinRT c# sample http://code.msdn.microsoft.com/windowsapps/Background-Audio-in-WinRT-344bcf4d
Another one here by which shows you how easy it is to configure Media Element to do background audio
http://babaandthepigman.wordpress.com/2012/03/17/metro-background-audio-c-consumer-preview/
You need to use a MediaElement with the AudioCategory=BackgroundCapable property. It needs to be instantiated on a XAML page.
Apply this style to your root frame:
<Style x:Key="RootFrameStyle" TargetType="Frame">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Frame">
<Grid>
<MediaElement x:Name="MediaPlayer" AudioCategory="BackgroundCapableMedia" AutoPlay="True" />
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And then you can play media like this:
var rootGrid = VisualTreeHelper.GetChild(Window.Current.Content, 0);
var mediaElement = (MediaElement)VisualTreeHelper.GetChild(rootGrid, 0);
mediaElement.Source = new Uri("ms-appx:///relative/path/to/file.mp3");
You then need to declare your app capable of background audio in the app manifest.
Full explanation on my blog here: http://rikk.it/2012/10/background-audio-in-a-metro-c-sharp-app/