Windows Phone:动态添加到PhoneApplicationFrame的图像不会显示

时间:2015-11-11 22:49:32

标签: windows-phone-8 controltemplate

我会使用PhoneAplicationFrame的一部分在我的应用程序的每个页面上显示一组控件。

其中一个控件是Image控件,它将显示在运行时获得的图像。

但图像永远不会显示其内容。

通过在PhonePage的网格中添加相同的控件,将显示该控件。

我该怎么做?

的App.xaml:

    <Application
        x:Class="PhoneApp1.App"
        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">
        <Application.Resources>
            <local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
            <ControlTemplate x:Name="TheControlTemplate">
                <Grid>
                    <ContentPresenter/>
                    <Grid x:Name="TheGridAbove"/>
                </Grid>
            </ControlTemplate>
        </Application.Resources>
        <Application.ApplicationLifetimeObjects>
            <shell:PhoneApplicationService
                Launching="Application_Launching" Closing="Application_Closing"
                Activated="Application_Activated" Deactivated="Application_Deactivated"/>
        </Application.ApplicationLifetimeObjects>
    </Application>

APP.XAML.CS CONTRUCTOR

    static public Grid TheGridAbove = null;
    public App()
    {
        UnhandledException += Application_UnhandledException;
        InitializeComponent();
        InitializePhoneApplication();
        InitializeLanguage();
        if (Debugger.IsAttached){
            Application.Current.Host.Settings.EnableFrameRateCounter = true;
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;}
        RootFrame.Template = Resources["TheControlTemplate"] as ControlTemplate;
        RootFrame.ApplyTemplate();
        TheGridAbove = (VisualTreeHelper.GetChild(RootFrame, 0) as FrameworkElement).FindName("TheGridAbove") as Grid;
    }

MainPage.xaml中

    <phone:PhoneApplicationPage
        x:Class="PhoneApp1.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"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True">
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
                <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="163,172,0,0" VerticalAlignment="Top" Click="button_Click"/>
        </Grid>
    </phone:PhoneApplicationPage>

MAINPAGE.XAML.CS button_Click

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Grid grid = new Grid();
        grid.Background = new SolidColorBrush(Colors.Red);
        Image img = new Image();
        img.Source = new BitmapImage(new Uri("/Common/Lenna.jpg", UriKind.Relative));
        grid.Children.Add(img);

        PhoneApp1.App.TheGridAbove.Children.Add(grid); // DON'T DISPLAY
        //LayoutRoot.Children.Add(grid); // THIS DISPLAY
    }

可以在这里下载代码:http://www.filedropper.com/code

0 个答案:

没有答案