Xamarin Forms DataTrigger causes NullReferenceException - xamarin

I am attempting to use a simple DataTrigger element within the XAML of a Xamarin Forms page:
<Frame BackgroundColor="Red" HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
<Frame.Triggers>
<DataTrigger TargetType="Frame" Binding="{Binding IsValid}" Value="True">
<Setter Property="BackgroundColor" Value="{x:Static Color.Lime}" />
</DataTrigger>
</Frame.Triggers>
</Frame>
This configuration crashes the application with what is effectively a NullReferenceException:
Java.Lang.NullPointerExceptionAttempt to invoke virtual method 'boolean android.graphics.Bitmap.isMutable()' on a null object reference
If I comment out the Setter in the above sample, the application runs normally but, of course, the trigger doesn't work.
Can anyone please suggest what I am doing wrong?

Found it!
I had a look at the decompiled version. This is a bug that occurs when the frame has a height of 0.
And this causes the error in FrameOnPropertyChanged
Adding Padding="1" or HeightRequest="1" WidthRequest="1" to your Frame should fix it, except there is something forcing it to 0.

Related

A value of type 'StackLayout' cannot be added to a collection or dictionary of type 'IList'

I have the Error : A value of type 'StackLayout' cannot be added to a collection or dictionary of type 'IList.
<StackLayout x:Name="Contunie" Margin="0" HeightRequest="80" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" BackgroundColor="#24348b">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="Contunie_Tapped"></TapGestureRecognizer>
</StackLayout.GestureRecognizers>
<Label Font="20" Margin="0,15,0,0" HorizontalOptions="CenterAndExpand" TextColor="White"
FontFamily="{DynamicResource GothamRoundedMedium}" Text="{Translator:Translate SendMessage}" BackgroundColor="Transparent"></Label>
</StackLayout>
I had the same issue. A rebuild will get rid of the problem. If something was actually wrong with your xaml, your page would most likely fail to load.
If a re-build didn't work, restart your visual studio will do it.
I had the same problem after update the Xamarin Forms to 3.2, after downgrade back to 3.1.0.697729, the issue is gone.

Include controls in a ControlTemplate from another .xaml

I want to create a control which displays an image and a watermark (image or something else) on it.
But the watermark should be loaded from another XAML file in order to let people custom the way the watermark will appear : alignment, opacity, size, nature of the watermark (TextBlock, Image, ...).
For instance I could load my watermark with this appearance
<Border BorderThickness="5" BorderBrush="Aqua" Width="50" Height="50">
<Image Source="play.png" />
</Border>
This code is from my Themes/generic.xaml and MyWatermarkControl (inherits from Control) is the class which contain the code of the control (dependency properties).
<Style TargetType="local:MyWatermarkControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyWatermarkControl">
<Grid>
<Image Source="{TemplateBinding ImagePath}" />
<Image x:name="watermark" Source="play.png" /> <!--I want this to be loaded from another .xaml-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My search results lead me to add in my ControlTemplate stuff like ContentPresenter, ContentTemplate, DataTemplate : so many results and I cannot understand how do they work but the fact they are nested
You can add a Source property to your MyWatermarkControl then bind the Source property of your embedded Image to this property. For more details see the following tutorial that I wrote:
A Simple Pattern for Creating Re-useable UserControls in WPF / Silverlight

ListPicker crashes on full screen - Silverlight tool kit for WP7

When implementing a ListPicker, it will crash when there are enough items to make it full screen. It does not crash if there are only 2-3 items and it just expands. I get an ArgumentException, 'The parameter is incorrect'
<toolkit:ListPicker Grid.Row="1"
ItemTemplate="{Binding lpkItemTemplate}"
FullModeItemTemplate="{Binding lpkFullItemTemplate}">
<toolkit:ListPicker.Items>
<toolkit:ListPickerItem>1</toolkit:ListPickerItem>
<toolkit:ListPickerItem>5</toolkit:ListPickerItem>
<toolkit:ListPickerItem>10</toolkit:ListPickerItem>
<toolkit:ListPickerItem>15</toolkit:ListPickerItem>
<toolkit:ListPickerItem>20</toolkit:ListPickerItem>
<toolkit:ListPickerItem>30</toolkit:ListPickerItem>
</toolkit:ListPicker.Items>
</toolkit:ListPicker>
Templates are
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="lpkItemTemplate">
<TextBlock Text="{Binding Content}" />
</DataTemplate>
<DataTemplate x:Name="lpkFullItemTemplate">
<TextBlock Text="{Binding Content}" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
I've looked at examples and I havent seen anything different than what I have here and it works. The only difference is the examplesa re doing data binding. I've tried steping through the control's code but I don't see anything that pops. It throws the exception in the base class 'ItemsControl' after the 'OnManipulationCompleted' event handler has completed in the ListPicker.
Any ideas what i'm doing wrong?
FullMode only works with DataBinding, and not with static ListPickerItems.

custom control (generic.xaml) for WP7

I am currently following the steps http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps? in doing a custom control for WP7. I have created my control in a normal windows phone Portrait Page xaml file (combining some control), I am not sure how I can convert it to work in generic.xaml file (as ResourceDictionary). So far it didn't work.
I tried to use Expression Blend to do the converting but I am not sure how to do it.
Edit: I am posting my code, it is a box that displays dynamic time. I want also to add properties for providing the Date and another for the color of the box.
This is the code so far.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:CereTime">
<!-- Check xmlns:local im case of error -->
<Style TargetType="local:CereT1">
<!-- After specifing the custom properties in .cs file, implement them here -->
<Setter Property="Date" Value="{TemplateBinding Date}" /> <!-- Under check -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CereT1">
<Canvas Background="YellowGreen" Width="100" Height="100" Name="DateBox" HorizontalAlignment="Left" VerticalAlignment="Top">
<StackPanel Orientation="Vertical" Height="100" Width="100">
<TextBlock Name="Month" Text="Month" Foreground="White" TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" FontWeight="Bold" Margin="0,12,0,0" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,6,0,0">
<TextBlock Name="Date" Text="0" VerticalAlignment="Bottom" Margin="0,0,5,0" FontSize="26.667"/>
<TextBlock Name="No_Name" Text="|" FontSize="26.667" />
<TextBlock Name="Year" Text="Year" Margin="5,0,0,0" FontSize="26.667" />
</StackPanel>
</StackPanel>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Kindly Advise me.
Thanks,
While you can move the style of the user control to a ResourceDictionary, why bother when you can have that and the template in the corresponding XAML for your UserControl?
Simple set the inside MyUserControl.xaml , along with the other properties you wish to change.
But the whole part about separating a style of a custom control to a ResourceDictionary , haven't a awful lot to do with UserControls. Perhaps you should tell us what's really wrong, instead of asking meta-questions.
I had the same problem when trying to create control following to link you give.
Solution is to add
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="themes/generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
into Application.Resources tag in App.xaml

Event to Command in a style

Can someone tell me how to code an event to command in a style for a tabitem? I have no problems using it elsewhere but I can not figure it out for a style.
I am trying to do this in Xaml for WPF using MVVM-Light toolkit.
Here is an example of what I am trying to do:
<DataTemplate x:Key="WorkspacesTemplate">
<igWindows:XamTabControl
AllowTabClosing="True"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}" TabItemCloseButtonVisibility="Visible"
Margin="4"
Theme="Office2k7Black" >
<igWindows:XamTabControl.Resources>
<Style TargetType="{x:Type igWindows:TabItemEx}"
BasedOn="{StaticResource {x:Type igWindows:TabItemEx}}" >
<Setter Property="Header" Value="{Binding Path=DisplayName}" />
<Style.Triggers>
<i:EventTrigger >
<cmd:EventToCommand Command="{Binding Path=CloseCommand}" />
</i:EventTrigger>
</Style.Triggers>
</Style>
</igWindows:XamTabControl.Resources>
</igWindows:XamTabControl>
</DataTemplate>
I am using an infragistics TabControl but it shouldnt be much different than a regular tab control.
In your example, you don't have an EventName attribute in the EventTrigger element. You need to specify an event on the tab that you want to trigger the command.

Resources