WP7 - Issues with delegates whilst using a storyboard - windows-phone-7

I have introduced some animation into my WP7 app via a storyboard.
It is all working as expected the first time through - the event occurs, and I call the code to do the animation in the code behind and I get exactly the behaviour I want.
But when I trigger the second time, it is like the the delegate code involved in this is being called twice. And then I trigger the same event and the delegate code is called 3 times. Its cumulative.
Code that is called is as follows
private void WeekForward()
{
FadeTitleOut.Begin();
FadeTitleOut.Completed += delegate
{
StartingMonday = StartingMonday.AddDays(7);
BuildPage();
FadeTitleIn.Begin();
};
}
FadeTitleOut and FadeTitleIn are my storyboards and the animation effect is fine. It's just that the first time the event happens StartingMonday is incremented by 7 days. The next time the event fires its incremented by 14 days and so on.
Am I doing something really dumb with my delegate code?
Probably should add that I am doing most of the stuff here in the code behind, including the generation and deletion of controls dynamicaly, but the Storyboards are defined in the XAML and they reference a control that is defined in the XAML (and not generated in the code behind)
Storyboard XAML is
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="FadeTitleOut">
<DoubleAnimation Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:0.3" />
</Storyboard>
<Storyboard x:Name="FadeTitleIn">
<DoubleAnimation Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity" From="0.0" To="1.0" Duration="0:0:0.3" />
</Storyboard>
</phone:PhoneApplicationPage.Resources>
Any ideas?

Have a look through your code, my guess is that "WeekForward()" is getting called more often than you want it to and you are subscribing the FadeTitleOut.Completed each time it is called (hence the behaviour you are describing). One way to fix this would be to have this code else where so it only subscribed to the Completed event once.
Also, you should write up your events before firing the Begin method as you have a potential race condition. eg,
FadeTitleOut.Completed += delegate
{
StartingMonday = StartingMonday.AddDays(7);
BuildPage();
FadeTitleIn.Begin();
};
FadeTitleOut.Begin();
Put this code where it will be fired once during the life of the XAML page and it should be fine. Hope that helps.

Related

At what offset does swiping trigger invoked event?

Using Xamarin Forms, I'm trying to understand at which offset during swiping will my SwipeView trigger the Invoked event when using mode Execute and if it's possible to programmatically get this value. So far what I've seen is it seems to be around 60% of Threshold (at least for Android) but I'm curious to know if this value is available somewhere in the component. I originally thought it wouldn't be triggered until it hit Threshold but I found that it fires before it meets that value. Below is my XAML:
<SwipeView
SwipeChanging="SwipeView_SwipeChanging"
SwipeEnded="SwipeView_SwipeEnded"
SwipeStarted="SwipeView_SwipeStarted"
Threshold="80">
<SwipeView.TopItems>
<SwipeItems Mode="Execute" SwipeBehaviorOnInvoked="Close">
<SwipeItemView BackgroundColor="Transparent" Invoked="SwipeItemView_Invoked">
<Label Text="Create New Task" />
</SwipeItemView>
</SwipeItems>
</SwipeView.TopItems>
<SwipeView.Content>
...
</SwipeView.Content>
</SwipeView>
With the above example with Threshold at 80 it seems to trigger around 49/50.

Xamarin Forms Android application throwing ANR Popup After 90 seconds

We have developed an application in Xamarin Forms. In Android application when the user uses the app in a low-network for about 90 seconds we are getting app not responding(ANR) Popup. Here my question is, is there any way to avoid this ANR popup in my application? In other words, is there any way to force the android system to wait for longer time?
In our Application when the user launching the application we are doing multiple tasks on threads which are majorly running on the secondary threads like:
Initialize Google Map & Creating Pins & Polyline Drawing
Firebase Listener Register
REST API calls
Loading List Items
So, before the device completes the above the list of task, if the users keep on touching the screen, then this causes multiple events to be queued in the main thread due to which we are getting ANR(App Not Responding Popup).
Here, we intend to disable the touch event until we complete the main thread in the existing task.
You can use the below logic to restrict touches to the content of the page below the activity indicator to resolve your issue.
However, the touches for the Grid below will still be listened to by the Android system and it will be processed. Here you can only restrict the interaction to your application views. However, the touch listened by the Android system for the Grid, ContentView, or ActivityIndicator in the below code can never be ignored.
Ideally, no user will try to touch more number of times after realizing there is no interaction when the loader is loading. So I guess you can safely ignore this case considering the general user thought process.
<Grid Grid.RowSpan="2"
InputTransparent="{Binding IsPageInteractable}"
IsVisible="{Binding IsPageBusy}">
<ContentView Opacity="0.2"
BackgroundColor="#4B4B4B"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
<ActivityIndicator IsRunning="{Binding IsPageBusy}"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand" />
</Grid>
public bool IsPageInteractable
{
get { return _isPageInteractable; }
set { _isPageInteractable = value; }
}
public bool IsPageBusy
{
get { return _isPageBusy; }
set
{
_isPageBusy = value;
this.IsPageInteractable = !value;
}
}

Do you know how to ALWAYS show the keayboard on a ContentPage

I got a small question for you. I am currently developing an application, based on Xamarin.Forms, and, on a ContentPage, i would like to always (yes i mean always always) show the keyboard. There is a textfield and some buttons or list, and when I click/tap on it, keayboard is hidden (and that is the standard behavior).
But here, I do not want if possible, to set the focus on the field for every action possible on the page. And even if I do that, we can see that the keyboard disappear and is shown back just after (trust me, me eyes are crying when it blink :)).
So, is it possible to set somewhere that we want to always show/display the keyboard ?
thanks a lot
Actually , it is not a good decision to keep the keyboard on the ContentPage , it will block other event like scroll and tap .
If you do want to implement it. You could define a default entry and set it onfocus when you finish editing other enrties .
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<Entry x:Name="defaultEntry" HeightRequest="0.1" WidthRequest="0.1" Unfocused="Entry_Unfocused" />
<Entry HeightRequest="100" WidthRequest="40" Unfocused="Entry_Unfocused" />
</StackLayout>
bool IsNeedShowKeyBoard=true ;
public MainPage()
{
InitializeComponent();
}
private void Entry_Unfocused(object sender, FocusEventArgs e)
{
if(IsNeedShowKeyBoard)
{
defaultEntry.Focus();
}
}
Change the property IsNeedShowKeyBoard to false when start to scroll or you want to edit other entry .
In fact, I will develop my own custom keyboard. With that solution, I will be able to let it be shown every time.

Is it possible to change a ListBox' ItemTemplate from a Storyboard?

I have a behavior that changes the visual state based on the page's orientation (portrait/landscape). I have a ListBox with a somewhat complex DataTemplate for its ItemTemplate. Is it possible to change the ItemTemplate from a VisualState's Storyboard? My XAML karma is low, and Blend doesn't let me do it (it changes the original ItemTemplate, it doesn't add a storyboard entry).
And remember, this is for Windows Phone 7 (thus Silverlight 3).
I probably didn't get an answer because it was too obvious. I told you my XAML karma was low. Here is the solution. Simply add this ObjectAnimationUsingKeyFrames to your storyboard:
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(ItemsControl.ItemTemplate)"
Storyboard.TargetName="PartakersListBox">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource LandscapePartakerDataTemplate}" />
</ObjectAnimationUsingKeyFrames>
I didn't return to Blend yet, to see if the template was editable. Will update below.

Is It Possible to Use TemplateBinding in a Storyboard in Silverlight?

I'm building a custom control in Silverlight and I want one of the fields to animate to the value of a DependencyProperty when that property is changed. More specifically, I have particular item in my Control Template that I want to animate to the color of the Background whenever the background changes color. So, what I have is:
<ControlTemplate TargetType="local:MyType">
<Grid x:Name="PART_RootElement">
<Grid.Resources>
<Storyboard x:Name="PART_FillAnimation">
<ColorAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="PART_MainPath"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<EasingColorKeyFrame
x:Name="PATH_FillKeyframe"
KeyTime="00:00:01"
Value="{TemplateBinding Background}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<!-- the rest of the template -->
I'm triggering the animation in the custom control code, but when the animation starts, it doesn't look like the Value is updating. I was just wondering if I'm missing something or if it is at all possible to apply TemplateBinding to resources in my ControlTemplate.
(I'm currently using a work-around of manually assigning the Background to the EasingColorKeyFrame Value, but the TemplateBinding solution would be so much cleaner.)
Have a look at Expression Blend Samples as a possible solution to your problem. There are a number of Interactivity classes that you could use within your ControlTemplate to create the effect your looking for. The documentation is not great, but descriptions in the Object Browser should give you some more clues :)
For example, I have a ListBox ItemTemplate that contains a ControlStoryboardAction Behaviour. The trigger for this Behaviour is a DataTrigger which fires when a DataContext field contains a specific value. (In my case when Severity=="High") The trigger then Plays a Storyboard within the ItemTemplate.
<i:Interaction.Triggers>
<is:DataTrigger Binding="{Binding Severity, Mode=OneWay}" Value="High">
<im:ControlStoryboardAction Storyboard="{StaticResource flashLight}" IsEnabled="True" />
</is:DataTrigger>
The following namespaces are referenced:
<i: - System.Windows.Interactivity
<is: - Expression.Samples.Interactivity (available from the link above. I am using the July 2009 release for SL3)
<im: - Microsoft.Expression.Interactivity.Media

Resources