Flyout 更改页面的主题 [英] Flyout changes Page's theme

查看:27
本文介绍了Flyout 更改页面的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WP8.1 上遇到了一些小问题 - 花了一些时间,但最终我设法对其进行了本地化 - 假设我们有一个带有弹出按钮的按钮:

I'm fighting with little problem on WP8.1 - it took some time, but finally I've managed to localize it - let say that we have a button with a flyout:

<Grid x:Name="LayoutRoot">
    <Button Content="reset" VerticalAlignment="Center">
        <Button.Flyout>
            <MenuFlyout Placement="Top">
                <MenuFlyoutItem Text="first item"/>
                <MenuFlyoutItem Text="second item"/>
            </MenuFlyout>
        </Button.Flyout>
    </Button>
</Grid>

它工作正常,但如果我们设置页面的DataContext:

It works fine, but if we set the DataContext of a page:

public MainPage()
{
    this.InitializeComponent();
    this.DataContext = this; // without this works fine every button click
}

然后有一个问题 - 我们第一次单击按钮时 - 工作正常,但是当我们第二次单击它时,随着弹出窗口,页面的主题更改为 Light(更改的主题在我们关闭弹出窗口后仍然存在,您将不得不重新加载页面).它看起来或多或少像下面的图片:

then there is a problem - the first time we click our button - works fine, but when we click it the second time, along with flyout, the page's theme changes to Light (the changed theme persists after we dismiss the flyout, you will have to reload the page). It looks more or less like in the images below:

有谁知道是什么导致了这个问题?有什么解决方法吗?

Does anybody know what can cause the problem? Any workarounds?

如果有人想尝试 - 这里是示例代码.

If somebody wants to try - here is a sample code.

推荐答案

我不知道为什么会这样,但是您可以在页面加载时强制使用页面的 RequestedTheme:

I don't know why it's happening, but you can force your page's RequestedTheme when the page is loaded:

XAML

<Page
...
x:Name="myPage">

C#

public MainPage()
{
    this.InitializeComponent();
    this.DataContext = this;

    if (App.Current.RequestedTheme == ApplicationTheme.Dark)
    {
        myPage.RequestedTheme = ElementTheme.Dark;
    }
    else
    {
        myPage.RequestedTheme = ElementTheme.Light;
    }
}

这篇关于Flyout 更改页面的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆