我在 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 (更改后的主题在我们关闭弹出窗口后仍然存在,您将必须重新加载页面)。它看起来或多或少像下面的图片所示:
有人知道是什么原因导致这个问题吗?有什么解决方法吗?
如果有人想尝试 - 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/