从主页面设置嵌套控件按钮的背景在Windows Phone中不起作用

时间:2012-11-02 13:59:29

标签: windows-phone-7.1

我正在开发Windows Phone 7.5应用程序。我有一个自定义标签栏,我在我的一些应用程序页面中使用。我已经为这个标签栏创建了一个带有3个按钮的自定义控件。

  1. 我已经定义了3个类型为ImageSource的DependencyProperties来设置我的主页面中的teh按钮的背景,我实际上使用了我的Tabbar控件。

  2. 这三个DependencyProperties绑定到我的自定义控件xaml中的3个按钮的ImageSource属性

  3. 然后我从主页面设置这些属性,我完全使用底部标签栏控件。

  4. 问题是图像可以在视觉工作室设计器上看到,但不能在模拟器上看到。模拟器显示黑色按钮。

    以下是我的标签栏控件的BottomTabBar.xaml:

    <UserControl x:Class="SafeCell.Views.BottomTabBar"
        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="100" d:DesignWidth="480">
    
        <Grid x:Name="BottomTabBarGrid" Width="{Binding DesignWidth}" Height="{Binding DesignHeight}" Background="Black">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="417*" />
                <ColumnDefinition Width="63*" />
            </Grid.ColumnDefinitions>
            <TextBlock Width="Auto" Text="{Binding Text}"></TextBlock>
            <Button Command="{Binding HomeButtoncommand}"   x:Name="home"  Height="125" Width="194"  HorizontalAlignment="Left" Margin="-11,-12,0,0" VerticalAlignment="Top">
                <Button.Background >
                    <ImageBrush ImageSource="{Binding HomeButtonImage}" Stretch="Fill" />
                </Button.Background>
            </Button>
            <Button Command="{Binding TripButtoncommand}"  x:Name="trips" Width="172"  HorizontalAlignment="Right" Margin="0,-12,85,-13" >
                <Button.Background>
                    <ImageBrush ImageSource="{Binding TripButtonImage}"  AlignmentX="Center" Stretch="Fill"/>
                </Button.Background>
            </Button>
            <Button Command="{Binding RulesButtoncommand}"   x:Name="rules" Width="183" HorizontalAlignment="Right" Grid.ColumnSpan="2" Margin="0,-12,-12,-13">
                <Button.Background>
                    <ImageBrush ImageSource="{Binding RulesButtonImage}"  />
                </Button.Background>
            </Button>
        </Grid>
    </UserControl>
    

    下面是我的标签栏控件的BottomTabBar.xaml.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Windows.Data;
    using System.Windows.Media.Imaging;
    
    namespace SafeCell.Views
    {
        public partial class BottomTabBar : UserControl,INotifyPropertyChanged
        {
    
    
            /// <summary>
            /// Command Dependency Property
            /// </summary>
            private static  DependencyProperty  mTripButtonImage=  DependencyProperty.Register("TripButtonImage", typeof(ImageSource), typeof(BottomTabBar),new PropertyMetadata(null));
            public  ImageSource TripButtonImage
            {
                 get
                {
    
                    return (ImageSource)GetValue(mTripButtonImage);
                }
                 set
                {
                    SetValue(mTripButtonImage, value);                
                   NotifyPropertyChanged("TripButtonImage");
                }
            }
    
            /// <summary>
            /// Command Dependency Property
            /// </summary>
            private static DependencyProperty mHomeButtonImage = DependencyProperty.Register("HomeButtonImage", typeof(ImageSource), typeof(BottomTabBar), new PropertyMetadata(null));
            public ImageSource HomeButtonImage
            {
                get
                {
    
                    return (ImageSource)GetValue(mHomeButtonImage);
                }
                set
                {
                    SetValue(mHomeButtonImage, value);
                    NotifyPropertyChanged("HomeButtonImage");
                }
            }
    
            /// <summary>
            /// Command Dependency Property
            /// </summary>
            private static DependencyProperty mRulesButtonImage = DependencyProperty.Register("RulesButtonImage", typeof(ImageSource), typeof(BottomTabBar), new PropertyMetadata(null));
            public ImageSource RulesButtonImage
            {
                get
                {
    
                    return (ImageSource)GetValue(mRulesButtonImage);
                }
                set
                {
                    SetValue(mRulesButtonImage, value);
                    NotifyPropertyChanged("RulesButtonImage");
                }
            }
    
    
            /// <summary>
            /// Command Dependency Property
            /// </summary>
            public static readonly DependencyProperty mTripButtoncommand =
                DependencyProperty.Register("TripButtoncommand", typeof(ICommand), typeof(BottomTabBar),
                    new PropertyMetadata(null));
            public ICommand TripButtoncommand
            {
                get
                {
                    return (ICommand)GetValue(mTripButtoncommand);
                }
                private set
                {
                    SetValue(mTripButtoncommand, value);
                  //  NotifyPropertyChanged("TripButtoncommand");
                }
            }
    
    
            /// <summary>
            /// Command Dependency Property
            /// </summary>
            public static readonly DependencyProperty mHomeButtoncommand =
                DependencyProperty.Register("HomeButtoncommand", typeof(ICommand), typeof(BottomTabBar),
                    new PropertyMetadata(null));
    
            public ICommand HomeButtoncommand
            {
                get
                {
                    return (ICommand)GetValue(mHomeButtoncommand);
                }
                private set
                {
                    SetValue(mHomeButtoncommand, value);
                   // NotifyPropertyChanged("HomeButtoncommand");
                }
            }
    
            /// <summary>
            /// Command Dependency Property
            /// </summary>
            public static readonly DependencyProperty mRulesButtoncommand =
                DependencyProperty.Register("RulesButtoncommand", typeof(ICommand), typeof(BottomTabBar),
                    new PropertyMetadata(null));
            public ICommand RulesButtoncommand
            {
                get
                {
                    return (ICommand)GetValue(mRulesButtoncommand);
                }
                private set
                {
                    SetValue(mRulesButtoncommand, value);
                   // NotifyPropertyChanged("RulesButtoncommand");
                }
            }
    
           // ImageBrush mTripActiveImage;
            //ImageBrush mTripInActiveImage;
    
            //ImageBrush mHomeActiveImage;
            ///ImageBrush mHomeInActiveImage;
    
    
            //ImageBrush mRulesActiveImage;
            //ImageBrush mRulesInActiveImage;
    
            public BottomTabBar()
            {
                try
                {
                    InitializeComponent();
                    BottomTabBarGrid.DataContext = this;
    
                    //if (mTripActiveImage == null)
                    //{
                    //    mTripActiveImage = new ImageBrush();
                    //    mTripInActiveImage = new ImageBrush();
                     //   mTripActiveImage.ImageSource = new BitmapImage(new Uri("/SafeCell;component/ImgResources/menu-my-trip-active.png", UriKind.RelativeOrAbsolute));
                    //    mTripInActiveImage.ImageSource = new BitmapImage(new Uri("/SafeCell;component/ImgResources/menu-my-trip.png", UriKind.RelativeOrAbsolute));
                    //}
                    //if (mHomeActiveImage == null)
                    //{
                    //    mHomeActiveImage = new ImageBrush();
                    //    mHomeInActiveImage = new ImageBrush();
                    //    mHomeActiveImage.ImageSource = new BitmapImage(new Uri("/SafeCell;component/ImgResources/menu-home-active.png", UriKind.RelativeOrAbsolute));
                    //    mHomeInActiveImage.ImageSource = new BitmapImage(new Uri("/SafeCell;component/ImgResources/menu-home.png", UriKind.RelativeOrAbsolute));
                    //}
                    //if (mRulesActiveImage == null)
                    //{
                    //    mRulesActiveImage = new ImageBrush();
                    //    mRulesInActiveImage = new ImageBrush();
                    //    mRulesActiveImage.ImageSource = new BitmapImage(new Uri("/SafeCell;component/ImgResources/menu-rules-active.png", UriKind.RelativeOrAbsolute));
                    //    mRulesInActiveImage.ImageSource = new BitmapImage(new Uri("/SafeCell;component/ImgResources/menu-rules.png", UriKind.RelativeOrAbsolute));
                    //}
                    //ImageBrush imageBrush = new ImageBrush();
                    //Uri uri = new Uri("/SafeCell;component/ImgResources/menu-my-trip-active.png", UriKind.RelativeOrAbsolute);
                    //imageBrush.ImageSource = new BitmapImage(uri);
                    //this.myTrip.Background = imageBrush;        
    
                   // trips.Click += new RoutedEventHandler(trips_Click);
                }
                catch (Exception ex)
                {
    
    
                }
    
            }
    
            void trips_Click(object sender, RoutedEventArgs e)
            {
              //  trips.Background = mTripActiveImage;
               // home.Background = mHomeInActiveImage;
               // rules.Background = mRulesInActiveImage;
    
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void NotifyPropertyChanged(String propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (null != handler)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
        }
    }
    

    ...以下是我在页面中使用此控件的方法:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0" Background="White">
        <TextBlock Name="locationLabel"  Margin="0,0,0,0" FontSize="16" FontWeight="Black"  Text="Gurgaon,Haryana" Foreground="Black"></TextBlock>
    
        <TabBar:BottomTabBar HomeButtonImage ="/SafeCell;component/ImgResources/menu-home.png"  RulesButtonImage="/SafeCell;component/ImgResources/menu-rules.png"  TripButtonImage="/SafeCell;component/ImgResources/menu-my-trip.png" RulesButtoncommand="{Binding AppRulesTabCmd}"  HomeButtoncommand="{Binding AppHomeTabCmd}"  TripButtoncommand="{Binding AppTripsTabCmd}"  Height="76" VerticalAlignment="Bottom" ></TabBar:BottomTabBar>
    </Grid>
    

0 个答案:

没有答案