我正在尝试以某种方式自定义PhoneApplicationFrame,但我不知道该如何进行。

让我解释一下:我正在开发WP 8应用程序,我希望同时具有背景声音和音效。在this post上寻求帮助后,建议我使用MediaElement并将其添加到XAMLPhoneApplicationFrame中(这正确吗?)。

我在这件事上做了什么:

我创建了一个框架(称为IntroFrame)并将其设置为RootFrame文件中的App.xaml.cs。就是这样:

public partial class App
{
    public static IntroFrame RootFrame {get; private set;}
    ...
}


InitializePhoneApplication方法中,我修改了代码:

private void InitializePhoneApplication()
{
   ...
   RootFrame = new IntroFrame();
   RootFrame.Navigated += CompleteInitializePhoneApplication;
   ...
}


在XAML中,我尝试添加MediaElement

<phone:PhoneApplicationFrame
    x:Class="PazaakPhone.IntroFrame"
    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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <MediaElement x:Name="BackgroundMedia" Source="Assets/Audio/bgm.mp3" AutoPlay="True" />
</phone:PhoneApplicationFrame>


框架看起来不错,我尝试了here之类的高度技巧,它确实有效。但是,我尝试自定义该框架,添加视觉元素,更改该MediaElement,但没有任何效果……没有效果。我猜PhoneApplicationFrame不能像PhoneApplicationPage这样的内容?我以为可以在Frame托管所有其他页面的同时在后台执行此操作。但是我走错了路,或者我错过了一些东西。会是什么呢?

最佳答案

您可以通过重新模板PhoneApplicationFrame来实现:

<Application.Resources>
    <Style x:Key="myPhoneApplicationFrameStyle" TargetType="phone:PhoneApplicationFrame">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="phone:PhoneApplicationFrame">
                    <Grid x:Name="MediaElementContainer" Margin="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <MediaElement Grid.Row="0" x:Name="MediaElement" Source="Assets/Audio/bgm.mp3" AutoPlay="True" Volume="1.0" Visibility="Collapsed" />
                        <Grid Grid.Row="1" x:Name="ClientArea">
                            <ContentPresenter />
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>


然后在app.cs中设置样式:

private void InitializePhoneApplication()
{
    if (phoneApplicationInitialized)
        return;

    RootFrame = new PhoneApplicationFrame { Style = Resources["myPhoneApplicationFrameStyle"] as Style };
    RootFrame.Navigated += CompleteInitializePhoneApplication;
    RootFrame.NavigationFailed += RootFrame_NavigationFailed;

    // Ensure we don't initialize again
    phoneApplicationInitialized = true;
}

关于c# - PhoneApplicationFrame自定义无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17569403/

10-10 14:57