【问题标题】:How to set background image globally windows phone 8.1?windows phone 8.1如何全局设置背景图片?
【发布时间】:2015-03-30 11:56:46
【问题描述】:

我是 windows phone 的新手,与 windows phone 8 相比,windows phone 8.1 似乎有很多变化。如何在全局范围内更改我的应用程序的背景图像,而不是在每个视图中一次又一次地声明它.

【问题讨论】:

    标签: xaml background windows-phone-8.1


    【解决方案1】:

    在 App.xaml 中声明 ImageBrush

    <Application x:Class="App1.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1">
        <Application.Resources>
            <ImageBrush x:Key="BgImage" ImageSource="Assets/Logo.png" />
        </Application.Resources>
    </Application>
    

    在 App.xaml.cs 中设置 rootFrame 的背景 -> OnLaunched 方法:

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        #if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
        #endif
    
        Frame rootFrame = Window.Current.Content as Frame;
    
        if (rootFrame == null)
        {
            rootFrame = new Frame();
    
            // TODO: diesen Wert auf eine Cachegröße ändern, die für Ihre Anwendung geeignet ist
            rootFrame.CacheSize = 1;
    
            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // TODO: Zustand von zuvor angehaltener Anwendung laden
            }
    
            Window.Current.Content = rootFrame;
        }
    
        if (rootFrame.Content == null)
        {
            if (rootFrame.ContentTransitions != null)
            {
                this.transitions = new TransitionCollection();
                foreach (var c in rootFrame.ContentTransitions)
                {
                    this.transitions.Add(c);
                }
            }
    
            rootFrame.ContentTransitions = null;
            rootFrame.Navigated += this.RootFrame_FirstNavigated;
    
            if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
        }
    
        rootFrame.Background = (ImageBrush)Resources["BgImage"];
        Window.Current.Activate();
    }
    

    比让你的页面背景透明。

    这是我的 Page.xaml:

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid>
            <TextBlock Text="Hello World !"/>
        </Grid>
    </Page>
    

    希望对你有帮助

    【讨论】:

    • 我正在关注本主题的第一个答案,但不知何故背景没有出现stackoverflow.com/questions/22013441/…
    • 确保您没有在页面中设置背景,这是默认设置:Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    • 那是你我试图删除行 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 但它给了我错误作为无效标记
    • 编辑了答案并添加了我的 Page.xaml
    • 请检查我编辑的问题。我已经删除了 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"。背景仍然没有出现。 :(((((
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多