c# - 弹出按钮更改页面的主题

标签 c# windows-runtime windows-phone-8.1

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

<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:

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

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

enter image description here enter image description here

有人知道是什么原因导致这个问题吗?有什么解决方法吗?

如果有人想尝试 - here is a sample code .

最佳答案

我不知道为什么会发生这种情况,但您可以在加载页面时强制使用页面的 RequestedTheme:

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;
    }
}

关于c# - 弹出按钮更改页面的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25529624/

相关文章:

c# - 从 List<Class> 高效地创建 List<Class>?

c# - 我的 Lazy<> 值工厂中的 InvalidOperationException

windows-8 - 现代 UI 自定义 AppBar 按钮

javascript - 如何使 Windows 应用商店应用程序非全屏?

winrt-xaml - ListPickerFlyout忽略Windows Phone 8.1中父页面的RequestedTheme

c# - ADO.NET + 大量插入 + Excel + C# = "A bad time"吗?

c# - 模拟认证

c# - 克隆 Windows 应用商店应用程序 - 应该更改哪些内容?

icons - AppBar 按钮图标不适合椭圆形

windows-phone-8 - Windows Phone 8.1/8 SDK