Exception - "Value does not fall within the expected range.inPage.xaml" - silverlight

I create app on Windows Phone 8 use MVVMCross and async Sqlite.net. Sometimes when you go to a page I get an exception:
System.ArgumentException: Value does not fall within the expected range.inPage.xaml'.
at System.Windows.Navigation.PageResourceContentLoader.EndLoad(IAsyncResult asyncResult)
at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
How do I determine the source of the problem?

Had those errors too. Usually it was a VisualState value that could not be converted.
I copied the example from my answer here
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="True"/>
versus
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
So when you say it does not happen every time it may for example be happening only when coincidentally you hover your mouse over a certain area and the problem sits somewhere in a mouseover state definition.

The problem was in my xaml. I used this user control:
<UserControl>
<controls:Pivot >
<controls:PivotItem >
<UserControl2/>
</controls:PivotItem>
<controls:PivotItem >
<UserControl2/>
</controls:PivotItem>
</controls:Pivot>
<UserControl>
I removed Pivot and error disappeared.

Related

Binding of DiscreteStringKeyFrame does not work

I have a Storyboard with StringAnimationUsingKeyFrames:
<Storyboard>
<StringAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.ToolTip)" Storyboard.TargetName="xButton">
<DiscreteStringKeyFrame KeyTime="0" Value="{Binding State,Converter={StaticResource StateToStringConverter},StringFormat={x:Static props:Resources.xToolTipText}}"/>
</StringAnimationUsingKeyFrames>
....
<Storyboard>
xToolTipText comes from Resources:
Press ({0} State)
The value received for resources is variable State (int) from Model.
The whole thing can not work because binding of DiscreteStringKeyFrame does not work (I think the reason is that he does not belong to the visual tree).
Which is another way I can do this?
I used proxy binding as this post:
How to bind to data when the DataContext is not inherited
And it worked.

wpf dynamic change image source using timer

In WPF, I have 2 images, and I need to create a blinking effect (not using opacity).
Assume that I have a.png and b.png, first step showing a.png, after 0.5 seconds, it will show b.png, then after 0.5 seconds, it will show a.png, and repeat non-stop.
I've go through the forum, but I still have no luck to get example in vb, please help.
You may use an appropriate animation without any code behind:
<Window.Resources>
<BitmapImage x:Key="Image1" UriSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
<BitmapImage x:Key="Image2" UriSource="C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"/>
</Window.Resources>
...
<Image x:Name="image" Source="{StaticResource Image1}">
<Image.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source"
Duration="0:0:1"
RepeatBehavior="Forever">
<DiscreteObjectKeyFrame Value="{StaticResource Image2}"
KeyTime="0:0:0.5"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
Perhaps replace the Loaded event by one that better suits your needs.
If you really want to do it in code behind, you may create a DispatcherTimer with an Interval set to half a second, and in the timer's Tick event handler alternately set the image's Source property to one of the two images.
I'm not fit in vb.net, but in c# you can do with something like
public ImageSource GetImageSourceFromImage(Bitmap pngFile)
{
MemoryStream imageStream = new MemoryStream();
pngFile.Save(imageStream, ImageFormat.Png);
imageStream.Seek(0, SeekOrigin.Begin);
return BitmapFrame.Create(imageStream);
}
This function gives you an imagesource which you can just assign to your image-object.

Is there a way to assign a binding to a value rather than bind the value in XAML?

So, let's say I want to modify the DataContext of several visual elements in a dynamic fashion and that I want to do so as part of a Storyboard. I might do that like so:
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SomeVisualElement" Storyboard.TargetProperty="(FrameworkElement.DataContext)" Duration="0:0:0" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" >
<DiscreteObjectKeyFrame.Value>
<Binding Path="Repository.GeneratedThings[0].Element" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
This has the effect of binding the Value of the DiscreteObjectKeyFrame to Repository.GeneratedThings[0].Element. When this Storyboard is applied, the DataContext of SomeVisualElement is set to Repository.GeneratedThings[0].Element. This is the equivalent, I think, of saying:
<UserControl x:Name="SomeVisualElement" DataContext="{Binding Path="Repository.GeneratedThings[0].Element"} />
This is functional, but what I'd like to do would be to have some way that the binding is maintained only in the visual element but not in the key frame. We have few visual elements but many Storyboards and KeyFrames, and we notice a performance hit proportional to the number of KeyFrames when, e.g., we update the GeneratedThings object (not the individual elements of the collection).
How can I set up the DiscreteObjectKeyFrame so that the DataContext ends up bound correctly but that the value of the DiscreteObjectKeyFrame isn't bound? Is this even a reasonable semantic distinction in the context of WPF/XAML between a value being a Binding and a value being bound?
Alternatively, is there another way to change the DataContext of several visual elements from within a Storyboard and that doesn't involve having each Storyboard maintaining a binding?
Imagine that you have the following code:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SomeVisualElement" Storyboard.TargetProperty="SomeProperty" Duration="0:0:0" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value={Binding Foo} />
</ObjectAnimationUsingKeyFrames>
One should notice two problems here.
Every Storyboard will have to maintain this Binding.
When running this Storyboard actually the current value of Foo will be set to SomeProperty. Latter changes in Foo won't be affected.
I suggest the following. Create some dependency object with Foo as dependency property. For example:
<Grid.Resources>
<viewModel:FooContainer Foo={Binding Foo} x:Key="FooContainer" />
</Grid.Resources>
Use this container in Storyboard(s).
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SomeVisualElement" Storyboard.TargetProperty="SomeProperty" Duration="0:0:0" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value={StaticResource FooContainer} />
</ObjectAnimationUsingKeyFrames>
Modify ContentTemplate of a Control so you can bind to Foo property itself. Thus you can transfer binding and not value of Foo directly to a Control. Also Storyboard won't be affected by Foo value change.

Display dot-dot-dot progress in a WPF button

I found quite a few examples for showing progress where the progress bars and wheels are used however; I could find only one javascript example to show an ellipsis (dot-dot-dot) to refer progress hence I thought of asking this question. My app is not very complex - it only has a few check-boxes and one button. Recently my team requested for an enhancement and want to keep it simple as well.
There is a button named 'GO' that the user clicks after configuring the required settings. The code behind it is also really straightforward - it disables the button after the click event and call's a standalone exe using ProcessStartInfo that performs three actions 'reconcle', 'post' and 'publish'. I use the WaitForExist() method to re-enable the button.
I was requested by my team to show the 'current process' on the button. They simply want the button text to show Reconciling. Reconciling.. Reconciling... (at regular intervals say, 1 sec) followed by Posting and Publishing in a similar fashion.
It would be nice to know the most appropriate way to achieve this. Thanks in advance.
The simplest way of doing that is using an ObjectAnimationUsingKeyFrames. Set the TargetProperty on the Content and set the Value of each DiscreteObjectKeyFrame to Reconciling. Reconciling.. Reconciling....
Example for a ControlTemplate with a ContentPresenter named PART_Content:
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Content" Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
<DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>

How can I share a VisualStateManager between two (or more) XAML files?

We're writing a Prism based Silverlight application and we've got a whole bunch of pages in separate modules.
The transition between the pages is handled via navigation events and each module has the following methods implemented to show the page when navigated to and hide it when navigated from:
public void Show()
{
VisualStateManager.GoToState(this, "ShowState", true);
}
public void Hide()
{
VisualStateManager.GoToState(this, "HideState", true);
}
At the moment "ShowState" and "HideState" are defined in each module's XAML file so are duplicated far too many times.
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStates">
<VisualState x:Name="ShowState">
...
</VisualState>
<VisualState x:Name="HideState">
...
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
Where ... represents the Storyboard for each transition.
I've just spotted an error in the Storyboard definitions and at the moment I'm going to have to replicate the fix across all the files. It would be better if there was only one definition of the Storyboard which could be referenced in each file.
I've searched all morning for the right syntax but have had no luck what so ever.
How can I share this VisualStateManager between all our XAML files?
<Storyboard x:Key="ShowStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<VisualState x:Name="ShowState">
<BeginStoryboard Storyboard="{StaticResource ShowStoryboard}"/>
</VisualState>
Referencing your Storyboard within XAML can be done as seen above. With the top most portion being a Storyboard stored as a Resource somewhere. After that you should be able to use the BeginStoryboard reference within your VisualState.
EDIT: The above appears possible within WPF however it is not possible in SL. As of current it does not appear the abilty to reuse a Storyboard or VisualState is possible in SL. You should still be able to achieve what you are trying to do by encapsulating the VisualStateManager behavior within a style applied to a custom control. This would provide you the single point of failure you are looking for.

Resources