gpt4 book ai didi

c# - 从 wpf 中的其他窗口更改框架源

转载 作者:行者123 更新时间:2023-11-30 16:45:31 24 4
gpt4 key购买 nike

我在 wpf 应用程序中有 2 个窗口。 window1 中有一个框架。我想从 window2 更改框架源。你能帮帮我吗?

例如:窗口 1:

<frame x:name="frame1"/>

window2.cs :

private void button1_click(object sender, RoutedEventArgs e){
window1.frame1.source = new Uri("page1.xaml",UriKind.Relative);
}

最佳答案

将 Frame 的 FieldModifier 属性设置为 internalpublic 或通过 Window1 中的属性公开 Frame:

<Frame x:Name="frame1" x:FieldModifier="public" />

然后您可以获得对 Window1 的引用并使用 Application.Current.Windows 集合访问该字段或属性:

private void button1_click(object sender, RoutedEventArgs e)
{
Window1 window1 = Application.Current.Windows.OfType<Window1>().FirstOrDefault();
if (window1 != null)
{
window1.frame1.Source = new Uri("page1.xaml", UriKind.Relative);
}
}

关于c# - 从 wpf 中的其他窗口更改框架源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41982058/

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