我有2个XAML文件:MainPage.xaml和Settings.XAML
我想从Settings.XAML更改MainPage.xaml LayoutRoot.Background属性。做这个的最好方式是什么?
答案 0 :(得分:0)
创建ResourceDictionary。使用这些内容在项目中创建一个新的xaml文件(例如Style.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework">
<Color x:Key="MainBackGroundColor">#F6F5E0</Color>
</ResourceDictionary>
像这样更新App.xaml。顺便说一下,如果要将设置拆分为不同的文件,可以将每个文件放在此MergedDictionaries部分中。
<Application x:Class="SonoCine.CineReader.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style.xaml" />
</ResourceDictionary.
</ResourceDictionary>
</Application.Resources>
</Application>
现在你应该可以在你的MainPage.xaml中使用MainBackGroundColor
Background =“{StaticResource MainBackGroundColor}”