gpt4 book ai didi

c# - Flyout 更改页面的主题

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:25 26 4
gpt4 key购买 nike

我在 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# - Flyout 更改页面的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25529624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com