2017-02-01 90 views
1


對不起!我不會說英語!
我在wpf應用程序中有2個窗口。 window1中有一個框架。我想從window2更改幀源。你可以幫我嗎?更改wpf中其他窗口的框架源代碼

例如: 窗口1:

<frame x:name="frame1"/> 

window2.cs:

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

回答

1

Frame的FieldModifier屬性設置爲internalpublic或暴露穿過窗口1的屬性的框架:

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

You ca n然後獲得對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); 
    } 
} 
+0

哇!非常感謝你! – amirhossein