0

这是主页的 XAML。

<StackPanel x:Name="menuStackPanel" Grid.Row="4" Grid.ColumnSpan="2"
    Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">

    <StackPanel.DataContext>
        <Model:GameSettings/>
    </StackPanel.DataContext>

    <uilibrary:CustomImageButton x:Name="btnPlay" Scale="1.5" 
        ReadyImage="/App;component/Images/Menus/menu_play.png" 
        SelectedImage="/App;component/Images/Menus/menu_play_selected.png" Margin="5"/>

    <uilibrary:CustomImageButton x:Name="btnPlanet" StickyState="True" Scale="1.5" 
         ReadyImage="/App;component/Images/Menus/menu_planet.png" 
         SelectedImage="/App;component/Images/Menus/menu_planet_selected.png" 
         AIsSelected="{Binding Path=UsePlanet, Mode=TwoWay, ElementName=menuStackPanel}" 
         Margin="5">                    
    </uilibrary:CustomImageButton>

    <uilibrary:CustomImageButton x:Name="btnGravity" StickyState="True" Scale="1.5" 
        ReadyImage="/App;component/Images/Menus/menu_gravity.png"
        SelectedImage="/App;component/Images/Menus/menu_gravity_selected.png" 
        AIsSelected="True" Margin="5">
    </uilibrary:CustomImageButton>
</StackPanel>

这是 UserControl 'CustomImageButon' 的 XAML

<UserControl x:Class="UILibrary.CustomImageButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

     <Grid x:Name="LayoutRoot" Background="Transparent">
        <Image x:Name="MyImage" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Image.RenderTransform>
                <CompositeTransform x:Name="buttonTransform"/>
            </Image.RenderTransform>
        </Image>
     </Grid>
</UserControl>

这是 UsePlanet 属性的代码,“MyUsePlanet”的值默认为 false。

public bool UsePlanet
{
    get
    {
        return MyUsePlanet;
    }

    set
    {
        MyUsePlanet = value;
        IsolatedStorageSettings.ApplicationSettings[GameSettings.key_planet] = value;
        IsolatedStorageSettings.ApplicationSettings.Save();

        this.OnPropertyChanged("UsePlanet");
    }
}

这是我的异常信息

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
'UI Task' (Managed): Loaded 'System.SR.dll'
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'MS.Internal.NativeParseException' occurred in System.Windows.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll

Additional information: Set property 'UILibrary.CustomImageButton.AIsSelected' threw an exception. [Line: 95 Position: 46]

我知道这与它有关,AIsSelected="{Binding Path=UsePlanet, Mode=TwoWay, ElementName=menuStackPanel}"因为如果我将其设置为“True”,那么问题就会消失,但我希望它绑定到我的模型。其他绑定(例如此绑定)也可以正常工作。

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Text="{Binding ShipAScore}">
    <TextBlock.DataContext>
        <Model:Scores/>
    </TextBlock.DataContext>
</TextBlock>

UDATE 4/28/2013 在我的 UserControl 后面的代码中添加依赖属性解决了这个问题。

public static readonly DependencyProperty AIsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CustomImageButton), new PropertyMetadata(false));
4

0 回答 0