所以,我走了,按照Edd的要求做了,并重写了这个内容。问题是带有动态资源作为其内容的文本块,当该文本块位于主窗口中时可以正确更新。但是,当我尝试将page1加载到主窗口的框架中时,它将加载但不更新动态资源。我是一个菜鸟,所以不确定我要寻找的是什么导致问题的原因,更不用说解决它了。任何帮助表示赞赏!
这是application.xaml:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
StartupUri="MainWindow.xaml">
<Application.Resources>
<sys:String x:Key="TestString">This string is for testing.</sys:String>
</Application.Resources>
</Application>
这是MainWindow.xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
<TextBlock HorizontalAlignment="Left" Margin="125,299,0,0" TextWrapping="Wrap" Text="{DynamicResource TestString}" VerticalAlignment="Top" Height="110" Width="515" FontSize="48"/>
<Button Content="Call Page" HorizontalAlignment="Left" Margin="165,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="Change Text" HorizontalAlignment="Left" Margin="490,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
</Window>
这是Page1.xaml:
<Page x:Class="Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="125,140,0,0" TextWrapping="Wrap" Text="{DynamicResource TestString}" VerticalAlignment="Top" Height="110" Width="515" FontSize="48"/>
</Grid>
</Page>
这是MainWindow.xaml.vb(隐藏代码):
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
MainFrame.Content = New Page1
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Resources("TestString") = "Test Completed."
End Sub
End Class
当我运行程序并按下两个按钮时,结果如下:
该字符串针对MainWindow上的Textblock进行更新,但不会针对页面1中调用该框架的字符串进行更新。