Binding to x:Name in XAML breaks my app in Mango - windows-phone-7

This line works in Windows Phone 7.0 but not in 7.1 (NullReferenceException thrown). Note that I checked that Name is never null or empty.
<CheckBox x:Name="{Binding Name}" Content="{Binding Label}" />
Removing x:Name, all things being equal, prevent the app to crash. Why is that ?

You can't bind x:Name , it's the equivalent of declaring a variable in C#. And it doesn't make sense to do either, as it's not a visual property.
Surely you meant to bind the Header property of the CheckBox?

My openion is Name property didn't take at runtime.

When you upgraded from 7.0 to 7.1 the xmlns:x in your header might be wrong. create a brand new page and make sure that that line is the same.

Related

Name from XAML file not in current context in cs file

I'm running Visual Studio 2015 Enterprise and I am trying to build a Xamarin project. I created a Blank XAML App project. I went went to MainPage.xaml file and added an x.Name to the boilerplate Label like so:
In the MainPage.xaml.cs file I attempt to change the text of that Label by referencing that name hello:
VS does not seem to like this as I get an error:
The name "hello" does not exist in the current context.
How can I access the names in the XAML file? I've tried cleaning the solution and rebuilding with no success.
You have to use x:Name instead of x.Name. x is the namespace you have declared with xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" and if you want to use something declared in this namespace, you have to use x followed by a colon and than the class or attribute name.
<Label x:Name="hello"
Text="Welcome to Stackoverflow"
VerticalOptions="Center"
HorizontalOptions="Center"/>
As #Sven-Michael Stübe noticed first you need to swap your x.Name declaration (which is incorrect) with x:Name.
Then, right click on your project and click build (or ⌘K shortcut on Mac) to allow the complier see your name declaration. The red underline should disappear afterwards.

AlternateContent Tags causing issues with IDE, but not compiler

I am working on a legacy product. I need to make regions of a complex UI optional, based on build constants. It is not feasible to move these regions into controls, so I am using AlternateContent Tags (mc:AlternateContent).
This works perfectly, at compilation and the application runs as expected.
However, the IDE claims one error for each AlternateContent, and enclosed Choice Tag, and will not load the Design window/preview (in VS or Blend):
The name "AlternateContent" does not exist in the namespace "http://schemas.openxmlformats.org/markup-compatibility/2006"
The name "Choice" does not exist in the namespace "http://schemas.openxmlformats.org/markup-compatibility/2006"
I have tried, rebuilding, cleaning and rebuilding, changing build settings between release, debug, x86, x64, and rebooting. Nothing helps. It even give the same errors in Blend.
I am hoping that this is just something stupid that I am doing, and I can fix it; or possibly a newer Namespace URI I should be using. If I cannot resolve the errors, I am hoping someone knows a trick to suppress these errors in the IDE, so can use it.
I have a fully updated VS2013 Premium installation. However, It has the glitch on a test machine running VS14 CTP, and another running VS2012 (fully updated), both without any add-ins; so I have to assume it is not a problem with my PCs install.
A little late to the party, but if you add the mc tag to your Ignorable attribute the error goes away. Your content won't show in the designer but worked for me when I compiled the different flavours of my project.
<UserControl...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:conditional="defined-in-assembly.cs"
mc:Ignorable="d mc"/>
What about this:
public class BuildConstants
{
public bool IsDebug
{
get
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
}
and the xaml:
<Application.Resources>
<BooleanToVisibilityConverter x:Name="BooleanToVisibilityConverter"></BooleanToVisibilityConverter>
<l:BuildConstants x:Key="BuildConstants" />
</Application.Resources>
<Grid Visibility="{Binding IsDebug, Source={StaticResource BuildConstants}, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock Text="This will be visible only when DEBUG build constant is present"></TextBlock>
</Grid>

Detecting and Implementing Scrollbar Style in Window 8/RT Application

I am developing a Custom Control to be placed inside a ScrollViewer, I have Custom coded a generic.xaml for applying visual style for the Scrollviewer.
<ScrollBar x:Name="VerticalScrollBar"
Grid.Row="0"
Grid.Column="1"
Width="18"
Margin="0,-1,-1,-1"
IndicatorMode="TouchIndicator"
IsTabStop="False"
Maximum="{TemplateBinding ScrollableHeight}"
The Problem I am facing is, I need to set the IndicatorMode to TouchIndicator/MouseIndicator based on whether the user is using Touch Devices or not.
Is there a way to do it?
Thanks
Arun Selva Kumar. B
Used Visual State Manager to achieve the requirement.

Microsoft.Phone.Controls.Toolkit ListPicker throws XamlParseException

After being advised that the native ComboBox was not the way to go I was told to look at the Silverlight Toolkit ListPicker. So I did and have got a problem.
I opened a new project and pulled a new ListPicker onto the MainPage. The ListPicker looks like:
<toolkit:ListPicker x:Name="Result">
<toolkit:ListPickerItem Content="Win" />
<toolkit:ListPickerItem Content="Place" />
<toolkit:ListPickerItem Content="Lose" />
</toolkit:ListPicker>
When trying to run this I get an XamlParseException with InnerException of InvalidProgramException. All I did was drag the control on, and add some ListPickerItem. Removing the items still results in the error, removing the ListPicker control completely allows the page to be shown with no error.
I'm sure I've missed something, but any documentation I have read seems to point towards this markup being fine, including http://windowsphonegeek.com/articles/listpicker-for-wp7-in-depth
I can provide any other info required.
ListPickerItem is a class used internally by the ListPicker and should not be used directly.
If you just want to add a list of items, you can use strings to do it, like this:
Add a new namespace on top of the Page to access the String class:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Then, just change the ListPicker code to this:
<toolkit:ListPicker>
<sys:String>Win</sys:String>
<sys:String>Place</sys:String>
<sys:String>Lose</sys:String>
</toolkit:ListPicker>
The problem was that the wrong dlls had been registered, i.e. 7 and not 7.1. I had to uninstall and reinstall to get it to update correctly and it worked.

dotfuscator and localization in windows phone 7 app

i have a problem with the dotfuscor.
I'm using a app with localization and i bind the data like:
Text="{Binding Path=Localizedresources.Foo, Source={StaticResource LocalizedStrings}}"
or
Header="{Binding Localizedresources.Today, Mode=OneWay}" DataContext="{StaticResource LocalizedStrings}"
the problem is, that dotfuscator dont likes this and i get the message e.g.:
XamlAnalysis flagged something in Resource Main.g.resources\mainpage.xaml at (111, 55) for the following reasons: Binding expression {Binding Localizedresources.Today, Mode=OneWay} :Could not resolve, may have to manually exclude.
but i dont know how to fix it or even how to exclude this because i dont know in which step the error is like "string encryption".
how to solve this?

Resources