5

I am trying to create a Settings page for my app in Windows Phone 7. I created the AppSettings class and is referring it from my MainPage.xaml. This is my code:

<phone:PhoneApplicationPage 
    x:Class="Shapes4Kids.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ShapesSettings;assembly=Shapes4Kids" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
        <local:AppSettings x:Key="appSettings"></local:AppSettings>
    </phone:PhoneApplicationPage.Resources>

But on the line where I refer the AppSettings (local:AppSettings line) , I get an error message stating that " cannot create an instance of AppSettings".

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
Rajeev Nair
  • 759
  • 8
  • 20

3 Answers3

4

This is because instantiating ApplicationsSettings throws an exception. If you add the following to your constructor, you should be fine;

try
{
    settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (System.IO.IsolatedStorage.IsolatedStorageException e)
{
    // handle exception
}
Henrik Hartz
  • 3,677
  • 1
  • 27
  • 28
  • 1
    Microsoft needs to update their "How to" to reflect this! http://msdn.microsoft.com/en-us/library/ff769510(v=vs.92) – jedmao Jun 10 '12 at 03:49
3

For objects to be reference in xaml like this they need to have a default parameterless constructor. I'd double check this is the case.

Other potential issues could be an exception thrown in the constructor.

Nigel Sampson
  • 10,549
  • 1
  • 28
  • 31
0

One possible reason could also be failing dependency property initialization.

I had a following code in the class that I was trying to instantiate in XAML:

public static readonly DependencyProperty ListViewObjectProperty = DependencyProperty.Register(
                                                                                                "ListViewObject",
                                                                                                typeof(ListView),
                                                                                                typeof(WidthConverter),
                                                                                                new UIPropertyMetadata(0));

...where this dependency property was intended for holding a reference to ListView. But VS default "propdp" code snippet generated this "new UIPropertyMetadata(0)" which is a bit wrong in case of reference variable. It should be "new UIPropertyMetadata(null)".

Changing this fixed the issue for me. For some reason I wans't receiving any visible exception from this at runtime.

Ollikat
  • 81
  • 6